【发布时间】:2018-11-21 16:43:22
【问题描述】:
我有一个像这样的数据框:
value = runif(n = 1000)
type = rep(c("a","b","c","d"),250)
type2 = rep(c("a","b"),500)
number = sample(1:4, 1000, replace=TRUE, prob=c(0.25, 0.25, 0.25, 0.25) )
feature = c(rep("small",500),rep("big",500))
allResults <- data.frame(value,type,type2,number,feature)
我想用 type2 值为箱线图的背景着色。如果我使用填充和列,它不是很清楚。如果可能的话,我认为背景颜色更直观。
library("ggplot2")
ggplot(allResults, aes(y=value, x=type)) + geom_boxplot(alpha=.3, aes(fill = type,col=type2)) +
ggtitle("comparison") + facet_grid(feature ~ number) +
theme(legend.position = "bottom",axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_y_continuous(breaks = seq(0, 1, by = 0.05),limits = c(0,1))
这是我目前的结果:
【问题讨论】:
-
你只想用
type2填充每个箱线图的背景? -
是的,我想像这样给背景面板上色:i.stack.imgur.com/fqyah.jpg
-
这不是更有意义(所以背景颜色不会与
type重叠):ggplot(allResults, aes(y=value, x=type)) + geom_boxplot(alpha=.3, aes(fill = type2, col=type2)) + ggtitle("comparison") + facet_grid(feature ~ number) + theme(legend.position = bottom",axis.text.x = element_text(angle = 90, hjust = 1)) + scale_y_continuous(breaks = seq(0, 1, by = 0.05),limits = c(0,1)) -
为什么?如果我可以填充为什么我不能使用背景颜色?这是正确的例子:i.stack.imgur.com/CpM0p.gif