I have a question on how to stratify making multiple box plots per group. This is what I have for a sample code
library(ggplot2)
mtcars$vs <- as.character(as.numeric(mtcars$vs))
y6 <- ggplot(mtcars, aes(x=vs,y=hp)) +
geom_boxplot(aes(group = vs),outlier.shape=NA, size=1, width = 0.6, fatten = 1) +
geom_jitter(aes(x=vs, y=hp, pch = factor(cyl)), position=position_jitter(width=.1, height=0), size = 2) +
scale_shape_manual(name ="X", values = c(1,2,3)) +
coord_cartesian(ylim=c(0, 350))
This is what I obtain from the graph. I hope to stratify the graphs per X axis by the legend making a total of 6 box plots (3 per X axis; 3 for "1" and 3 for "2"). Is there a way to do this? I have attached an image of it below:
Here is the code for you:
library(ggplot2)
ggplot(mtcars, aes(x=vs,y=hp,fill = factor(cyl))) +
geom_boxplot(aes(fill = factor(cyl)),outlier.shape=NA, size=1, width = 0.6, fatten = 1) +
coord_cartesian(ylim=c(0, 350))
I have used fill=
argument in ggplot()
to split/group the data by column cyl
.
If you look closer at mtcars
data and your plot, you actually do not have 3 unique values of cyl
for vs = 1
, just two (cyl
4 & 8)..Therefore you get total of 5 boxes