ggplot2:facet_wrap基于数据集中变量的条纹颜色 [英] ggplot2: facet_wrap strip color based on variable in data set
问题描述
有没有一种方法可以根据数据框提供的变量来填充由facet_wrap创建的构面?
示例数据:
MYdata < - data.frame(fruit = rep(c(apple,orange,plum,banana,pear,葡萄)),farm = rep(c(0,1,3,6,9,12),each = 6),weight = rnorm(36,10000,2500),size = rep(c(small large)))
示例图:
<$ c $ (=农场,y =权重))+ geom_jitter(position = position_jitter(width = 0.3),aes(color = factor(农场)),size = 2.5,alpha = 1 )+ facet_wrap(〜fruit)
我知道如何改变条的背景颜色(例如橙色):
$ b
p1 + theme(strip.background = element_rect(fill =orange))
<有没有一种方法可以传递变量
size
i中的值 element_rect
?中的参数 MYdata
p> 基本上,我不希望所有小条的颜色都是1色,我希望小水果(苹果,李子,梨)的条纹背景颜色为绿色,大水果的背景颜色为橙色,香蕉,葡萄)变成红色。
通过一点工作, gtable拥有正确的grobs,
d < - data.frame(fruit = rep(c(apple,orange,plum ,香蕉,梨,葡萄)),
farm = rep(c(0,1,3,6,9,12),每个= 6),
weight = rnorm(36,10000,2500),
size = rep(c(small,large)))
p1 = ggplot(data = d,aes(x = farm ,y = weight))+
geom_jitter(position = position_jitter(width = 0.3),
aes(colo (农场),size = 2.5,alpha = 1)+
facet_wrap(〜fruit)
dummy < - ggplot y = weight))+ facet_wrap(〜fruit)+
geom_rect(aes(fill = size),xmin = -Inf,xmax = Inf,ymin = -Inf,ymax = Inf)+
theme_minimal )
library(gtable)
g1 < - ggplotGrob(p1)
g2 < - ggplotGrob(dummy)
gtable_select < - function(x,...)
{
匹配< -c(...)
x $ layout< - x $ layout [matches,,drop = FALSE]
x $ grobs< - x $ grobs [matches]
x
}
面板< - grepl(pattern =panel,g2 $ layout $ name)
strips< - grepl(pattern =strip_t,g2 $ layout $ name)
g2 $ layout $ t [panels]< - g2 $ layout $ t [panels] - 1
g2 $ layout $ b [面板]< - g2 $ layout $ b [面板] - 1
new_strips< - gtable_select(g2, strip)
grid.newpage()
grid.draw(new_strips)
gtable_stack< - 函数(g1,g2){
g1 $ grobs< - c(g1 $ grobs,g2 $ grobs)
g1 $ layout< - transform(g1 $ layout,z = z-max(z),name =g2)
g1 $ layout< - rbind(g1 $ layout,g2 $ layout)
g1
}
##理想情况下,您将删除旧的条带,现在它们只覆盖
new_plot< - gtable_stack(g1,new_strips)
grid.newpage()
grid.draw(new_plot)
Is there a way to fill the strips of facets created with facet_wrap based on a variable supplied with the data frame?
Example data:
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")))
Example plot:
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)
I know how to change the background color of the strips (e.g. to orange):
p1 + theme(strip.background = element_rect(fill="orange"))
Is there a way to pass on the values in the variable size
in MYdata
to the parameter fill
in element_rect
?
Basically, instead of 1 color for all strips I would like the strip background color of small fruits (apple, plum, pear) to be green and the background color of large fruits (orange, banana, grape) to be red.
With a little bit of work, you can combine your plot with a dummy gtable that has the right grobs,
d <- 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 = d, 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)
dummy <- ggplot(data = d, aes(x = farm, y = weight))+ facet_wrap(~fruit) +
geom_rect(aes(fill=size), xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
theme_minimal()
library(gtable)
g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(dummy)
gtable_select <- function (x, ...)
{
matches <- c(...)
x$layout <- x$layout[matches, , drop = FALSE]
x$grobs <- x$grobs[matches]
x
}
panels <- grepl(pattern="panel", g2$layout$name)
strips <- grepl(pattern="strip_t", g2$layout$name)
g2$layout$t[panels] <- g2$layout$t[panels] - 1
g2$layout$b[panels] <- g2$layout$b[panels] - 1
new_strips <- gtable_select(g2, panels | strips)
grid.newpage()
grid.draw(new_strips)
gtable_stack <- function(g1, g2){
g1$grobs <- c(g1$grobs, g2$grobs)
g1$layout <- transform(g1$layout, z= z-max(z), name="g2")
g1$layout <- rbind(g1$layout, g2$layout)
g1
}
## ideally you'd remove the old strips, for now they're just covered
new_plot <- gtable_stack(g1, new_strips)
grid.newpage()
grid.draw(new_plot)
这篇关于ggplot2:facet_wrap基于数据集中变量的条纹颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!