如何在不使用颜色或填充ggplot2的情况下对箱线图进行分组

How to group Boxplots without use of color or fill in ggplot2

我有一个数据集,我想使用 ggplot2 包创建两组箱线图,如下所示:

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()

但是,我想使用不同的美学(未显示)为箱线图着色,或者根本不给它们着色。如何将 oldyoung 组合在一起,但仍然有 f1 变量的单独箱线图。这是我想做的事情的简化版本,所以我要求你的答案可以扩展到多个样本(例如,不仅仅是 oldyoung,可能有 20 个不同的类别)。

使用group映射:

ggplot(aes(y = boxthis, x = f2, group = interaction(f1,f2)),
  data = df) + geom_boxplot()