Found 2038 Articles for R Programming

How to create the stacked bar plot using ggplot2 in R with labels on the plot?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 12:42:38

2K+ Views

The creation of stacked bar plot using ggplot2 can be done with the help of position="stack" argument inside geom_bar function. If we want to create the stacked bar plot then geom_text function will be used with the same position argument and the aes to define the labels as shown in the below example.Example Live DemoConsider the mtcars data −head(mtcars)Outputmpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1Example Live Demodf

How to find the R version you are using?

Nizamuddin Siddiqui
Updated on 13-Sep-2023 15:53:58

40K+ Views

Most of the times, we need to use packages in R and some packages are restricted to different versions in R, generally to newer versions. Therefore, we might need to find which version of R we are using. To find the R version, we can directly use the command R.Version().Example Live DemoR.Version()Output$platform [1] "x86_64−w64−mingw32" $arch [1] "x86_64" $os [1] "mingw32" $system [1] "x86_64, mingw32" $status [1] "" $major [1] "4" $minor [1] "0.2" $year [1] "2020" $month [1] "06" $day [1] "22" $`svn rev` [1] "78730" $language [1] "R" $version.string [1] "R version 4.0.2 (2020−06−22)" $nickname [1] "Taking Off Again"We can ... Read More

How to save a plot in pdf in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 12:07:20

4K+ Views

To save a plot in pdf, we can use the pdf function in base R. For example, if we want to save a plot with the name PDF then it can be done using the below command −pdf("PDF.pdf")After this we can create the plot and use dev.off().Examplepdf("pdfExample.pdf") plot(1:10)OutputExample Live Demodev.off()OutputTo check where it is saved, find the working directory using getwd().

How to select positive values in an R data frame column?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 12:06:22

1K+ Views

We know that positive values are greater thandf1$x[which(df1$x>0)]Example1 Live Demoset.seed(254) x

How to convert columns of an R data frame into a single vector?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 11:53:14

1K+ Views

Sometimes all the columns in a data frame have similar data characteristics representing a particular variable. For example, having a data frame containing five columns each with heights of people. To convert this type of data frame into a vector we can use as.vector function along with the as.matrix function. The as.matrix will read the data frame columns so that the array of values can be created.Example1 Live DemoConsider the below data frame −set.seed(101) x1

How to create a step histogram using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 11:37:15

275 Views

To create a step histogram using ggplot2, we can use geom="step" argument inside stat_bin function. For example, if we have a data frame that contains a single column then the step histogram can be created using the command − ggplot(df,aes(x))+stat_bin(geom="step",bins=30)Example Live DemoConsider the below data frame −set.seed(14) x

How to create a single data frame from data frames stored in a list with row names in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 11:36:59

112 Views

If we have multiples data frames of same size stored in a list and we believe that these data frames have similar characteristic then we can create a single data frame. This can be done with the help of do.call. For example, if we have a list defined with the name List containing data frames with equal number of rows with their names then a single data frame can be created do.call(rbind,unname(List)).Example Live Demodf1

How to multiply matrices elements if matrices are stored in a list in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 11:16:14

380 Views

To multiply matrices elements if matrices are stored in a list, we can make use of Reduce function. For example, if we have four matrices named as M1, M2, M3, and M4 stored in a list object called List then the multiplication each element in all the four matrices can be done by using the command Reduce("*",List).Example1 Live DemoM1

How to set the plot area to assign the plots manually in base R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 13:38:43

81 Views

We can split the screen to assign the plots manually in the plot area. The split.screen function can be used for this purpose. For example, if we want to create 4 plots in the plot window then we can use split.screen(c(2, 2)). Now to create the plot in first screen the command will be screen(1) then plot will be created. If we again create the plot then the original plot in screen(1) will be replaced with the new one. To create the plot in 3rd screen that is screen(3), we first need to use the command screen(3) and then create ... Read More

How to increase the X-axis length of histogram in base R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 11:11:14

5K+ Views

To increase the X-axis length of histogram in base R, we can use the axis function. The most important thing to remember while using the axis function is mentioning the appropriate sequence based on the values in the vector or in the column of the data frame if a data frame is used.Example Live Demox

Advertisements