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

允我心安 提交于 2020-07-08 19:41:49

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!