如何使用 ggplot 自定义多面图中的背景颜色
How to customize background colors in faceted plots with ggplot
我想单独控制多面图中面板和绘图的背景颜色。在中使用提示
this answer,我能够创建一些几乎可以工作的东西,但仍然有两个问题:
- 颜色没有按照我定义的方式显示 - 颜色因单元格而异,但颜色看起来 'strange' - 因为它们是一些叠加层
- 我无法单独控制面板和绘制背景颜色
到目前为止,这是我的代码:
ann_text <- data.frame(mpg = c(22, 15, 30),
wt = c(4, 5, 2),
cyl = factor(c(4, 6, 8), levels = c("4","6","8")),
lab = c("Text 1", "Text 2", "Text 3"),
hue = c("pink", "yellow", "lightblue"))
ggplot(mtcars, aes(mpg, wt)) +
geom_rect(data = ann_text, aes(fill = hue),
xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, alpha = 0.3) +
geom_text(data = ann_text, mapping = aes(x = mpg, y = wt, label = lab)) +
theme(panel.background = element_blank(),
panel.grid.major.y = element_line(color = "grey")) +
facet_grid(. ~ cyl) +
geom_point()
result of my code with 'Strange' colors
我本来想在 theme() 中使用类似于下面这行的内容,但是当然(?!)这行不通:
plot.background = element_rect(data = ann_text, mapping = aes(fill = hue))
有什么想法可以让我有机会指定面板和绘图的背景颜色?
使用scale_fill_identity
.
ggplot(mtcars, aes(mpg, wt)) +
geom_rect(data = ann_text, aes(fill = hue),
xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, alpha = 0.3) +
geom_text(data = ann_text, mapping = aes(x = mpg, y = wt, label = lab)) +
theme(panel.background = element_blank(),
panel.grid.major.y = element_line(color = "grey")) +
facet_grid(. ~ cyl) +
geom_point() +
scale_fill_identity()
或者,您可以将审美包裹在I
ggplot(mtcars, aes(mpg, wt)) +
geom_rect(data = ann_text, aes(fill = I(hue)),
xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, alpha = 0.3) +
geom_text(data = ann_text, mapping = aes(x = mpg, y = wt, label = lab)) +
theme(panel.background = element_blank(),
panel.grid.major.y = element_line(color = "grey")) +
facet_grid(. ~ cyl) +
geom_point()
我想单独控制多面图中面板和绘图的背景颜色。在中使用提示 this answer,我能够创建一些几乎可以工作的东西,但仍然有两个问题:
- 颜色没有按照我定义的方式显示 - 颜色因单元格而异,但颜色看起来 'strange' - 因为它们是一些叠加层
- 我无法单独控制面板和绘制背景颜色
到目前为止,这是我的代码:
ann_text <- data.frame(mpg = c(22, 15, 30),
wt = c(4, 5, 2),
cyl = factor(c(4, 6, 8), levels = c("4","6","8")),
lab = c("Text 1", "Text 2", "Text 3"),
hue = c("pink", "yellow", "lightblue"))
ggplot(mtcars, aes(mpg, wt)) +
geom_rect(data = ann_text, aes(fill = hue),
xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, alpha = 0.3) +
geom_text(data = ann_text, mapping = aes(x = mpg, y = wt, label = lab)) +
theme(panel.background = element_blank(),
panel.grid.major.y = element_line(color = "grey")) +
facet_grid(. ~ cyl) +
geom_point()
result of my code with 'Strange' colors
我本来想在 theme() 中使用类似于下面这行的内容,但是当然(?!)这行不通:
plot.background = element_rect(data = ann_text, mapping = aes(fill = hue))
有什么想法可以让我有机会指定面板和绘图的背景颜色?
使用scale_fill_identity
.
ggplot(mtcars, aes(mpg, wt)) +
geom_rect(data = ann_text, aes(fill = hue),
xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, alpha = 0.3) +
geom_text(data = ann_text, mapping = aes(x = mpg, y = wt, label = lab)) +
theme(panel.background = element_blank(),
panel.grid.major.y = element_line(color = "grey")) +
facet_grid(. ~ cyl) +
geom_point() +
scale_fill_identity()
或者,您可以将审美包裹在I
ggplot(mtcars, aes(mpg, wt)) +
geom_rect(data = ann_text, aes(fill = I(hue)),
xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, alpha = 0.3) +
geom_text(data = ann_text, mapping = aes(x = mpg, y = wt, label = lab)) +
theme(panel.background = element_blank(),
panel.grid.major.y = element_line(color = "grey")) +
facet_grid(. ~ cyl) +
geom_point()