ggplot 改变每一个分面的颜色
Posted bigbiglover123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ggplot 改变每一个分面的颜色相关的知识,希望对你有一定的参考价值。
### change ggplot strip colours
p <- ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(drv ~ cyl)
p
g <- ggplot_gtable(ggplot_build(p))
strip_both <- which(grepl(‘strip-‘, g$layout$name))
fills <- c("red","green","blue","yellow","red","green","blue","yellow")
k <- 1
for (i in strip_both) {
j <- which(grepl(‘rect‘, g$grobs[[i]]$grobs[[1]]$childrenOrder))
g$grobs[[i]]$grobs[[1]]$children[[j]]$gp$fill <- fills[k]
k <- k+1
}
grid.draw(g)
以上是关于ggplot 改变每一个分面的颜色的主要内容,如果未能解决你的问题,请参考以下文章