问题
I have a dataset that I would like to create two groups of boxplots using the ggplot2
package as shown below:
library(ggplot2)
df <- data.frame(f1=factor(rbinom(100, 1, 0.45), label=c("m","w")),
f2=factor(rbinom(100, 1, 0.45), label=c("young","old")),
boxthis=rnorm(100))
ggplot(aes(y = boxthis, x = f2, fill = f1), data = df) + geom_boxplot()
BUT, I want to color the boxplots using a different aesthetic (not shown) or not color them at all. How can I group the old
and young
together, but still have separate boxplots for the f1
variable. This is a simplified version of what I want to do, so I ask that your answer be expandable to multiple samples (e.g. more than just old
and young
, maybe 20 different categories).
回答1:
Use the group
mapping:
ggplot(aes(y = boxthis, x = f2, group = interaction(f1,f2)),
data = df) + geom_boxplot()
来源:https://stackoverflow.com/questions/41389630/how-to-group-boxplots-without-use-of-color-or-fill-in-ggplot2