【发布时间】:2014-06-11 18:07:21
【问题描述】:
我想根据组修改构面背景的颜色。我不确定这是否可能。具体来说,我正在使用具有多个层的facet_grid(不是facet_wrap)。
## Sample data
dat <- mtcars
## Add in some colors based on the data
dat$facet_fill_color <- c("red", "green", "blue", "yellow", "orange")[dat$gear]
## Create main plot
library(ggplot2)
P <- ggplot(dat, aes(x=cyl, y=wt)) + geom_point(aes(fill=hp)) + facet_grid(gear+carb ~ .)
## I can easily cahnge the background using:
P + theme(strip.background = element_rect(fill="red"))
但是,我想为不同的组更改不同的颜色。 理想情况下,如下所示(当然不起作用)
P + theme(strip.background = element_rect(fill=dat$facet_fill_color))
P + theme(strip.background = element_rect(aes(fill=facet_fill_color)))
刻面背景可以有不止一种颜色吗?
(与上述相关,但不是实际答案:ggplot2: facet_wrap strip color based on variable in data set)
【问题讨论】:
-
您可能会发现 this answer 和链接的答案很有用。