Nizamuddin Siddiqui

Nizamuddin Siddiqui

1,958 Articles Published

Articles by Nizamuddin Siddiqui

Page 91 of 196

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Feb-2021 3K+ 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

Read More

How to save a plot in pdf in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Feb-2021 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().

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Feb-2021 2K+ Views

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

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Feb-2021 2K+ 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

Read More

How to create a step histogram using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Feb-2021 410 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

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Feb-2021 213 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

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Feb-2021 6K+ 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

Read More

How to check for duplicates in data.table object in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Feb-2021 586 Views

The checking for duplicates in a data.table object can be easily done with the help of delta ($) operator that is used to access the columns and the duplicated function. For example, if a data.table object DT contains a column x then to check the duplicates in x, we can use the command duplicated(DT$x).Example1Loading data.table object and creating an object −library(data.table) set.seed(141) x

Read More

How to create a combination of pairs from a vector in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Feb-2021 3K+ Views

To create a combination of pairs, we can use the combn function. This function will be helpful to use the vector length and the value 2 that represents a pair to create the combinations. Although, this is enough but we can transpose the output to get a better−looking output.Example1 Live Demox1

Read More

How to multiply only one column in an R data frame with a number?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Feb-2021 4K+ Views

To multiply only one column with a number, we can simply use the multiplication operator * but need to replace the original column with the new values. For example, if we have a data frame called df that contains three columns x1, x2, and x3, and we want to multiply the second column x2 with 2 then it can be done as df$x2

Read More
Showing 901–910 of 1,958 articles
« Prev 1 89 90 91 92 93 196 Next »
Advertisements