如何在不使用颜色的情况下对Boxplots进行分组或填写ggplot2 [英] How to group Boxplots without use of color or fill in ggplot2
问题描述
我有一个数据集,我想使用 ggplot2
包创建两组箱线图,如下所示:
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()
但是,我想使用不同的美学效果为箱形图着色(不是显示)或根本不给它们上色。如何将 old
和 young
分组在一起,但是对于 f1仍然有单独的箱线图
变量。这是我想做的事情的简化版本,所以我要求您的答案可以扩展到多个示例(例如,不只是 old
和 young
,也许有20个不同的类别)。
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).
推荐答案
使用组
映射:
ggplot(aes(y = boxthis, x = f2, group = interaction(f1,f2)),
data = df) + geom_boxplot()
这篇关于如何在不使用颜色的情况下对Boxplots进行分组或填写ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!