相关疑难解决方法(0)

ggplot2:facet_wrap基于数据集中变量的条带颜色

有没有办法根据随数据框提供的变量填充使用facet_wrap创建的facet条?

示例数据:

MYdata <- data.frame(fruit = rep(c("apple", "orange", "plum", "banana", "pear", "grape")), farm = rep(c(0,1,3,6,9,12), each=6), weight = rnorm(36, 10000, 2500), size=rep(c("small", "large")))

示例图:

p1 = ggplot(data = MYdata, aes(x = farm, y = weight)) + geom_jitter(position = position_jitter(width = 0.3), aes(color = factor(farm)), size = 2.5, alpha = 1) + facet_wrap(~fruit)

我知道如何更改条带的背景颜色(例如橙色):

p1 + theme(strip.background = element_rect(fill="orange"))

facet_wrap和橙色条纹颜色

有没有办法转嫁值的变量sizeMYdata的参数fillelement_rect

基本上,对于所有条带而不是1种颜色,我希望小水果(苹果,李子,梨)的条带背景颜色为绿色,大果实(橙色,香蕉,葡萄)的背景颜色为红色.

r ggplot2 facet-wrap

52
推荐指数
4
解决办法
3万
查看次数

标签 统计

facet-wrap ×1

ggplot2 ×1

r ×1