How to put several boxes in one graph in R?

Sorry, I do not have sample code for this question.

All I want to know is that you can create several side-by-side boxes in R that represent different columns / variables in my data frame. Each boxplot will also represent only one variable - I would like to set the y scale to a range (0.6).

If this is not possible, how can I use something like a panel parameter in ggplot2, if I only want to create boxplot using one variable? Thanks!

Ideally, I want something like the image below, but without grouping factors, as in ggplot2. Again, each boxplot will be completely separate and single columns.

enter image description here

+7
r rstudio plot ggplot2 boxplot
source share
2 answers

ggplot2 requires your data to be plotted along the y axis, are in the same column.

Here is an example:

 set.seed(1) df <- data.frame( value = runif(810,0,6), group = 1:9 ) df library(ggplot2) ggplot(df, aes(factor(group), value)) + geom_boxplot() + coord_cartesian(ylim = c(0,6) 

enter image description here

ylim(0,6) sets the y axis from 0 to 6

If your data is in columns, you can get it in longform using melt from reshape2 or gather from tidyr . (other methods available).

+5
source share

You can do this if reshape your data in a long format

 ## Some sample data dat <- data.frame(a=rnorm(100), b=rnorm(100), c=rnorm(100)) ## Reshape data wide -> long library(reshape2) long <- melt(dat) plot(value ~ variable, data=long) 

enter image description here

+5
source share

All Articles