ggplot2图表上的绘图区域(但不是面板区域)可以有多种背景颜色吗?预感告诉我,有可能将轴作为某种背景颜色.
这就是我目前的图表:

这就是我想要实现的目标:

[最终的颜色肯定会有所不同.我只是以最简单的形式使用这个例子以便于讨论]
我尝试将"填充"参数传递给:
theme(axis.text.y = element_text(fill = "red"),
Run Code Online (Sandbox Code Playgroud)
但它显然失败了,因为这个论点是为了element_rect.
你可以在边距中添加grobs - 我不得不弄乱注释范围以使其适合 - 所以期望有一个更健壮的方法.改编自这个问题:如何在绘图区域的精确区域放置带有annotation_custom()的grobs?
library(grid)
data(mtcars)
#summary(mtcars)
myGrob <- grobTree(rectGrob(gp=gpar(fill="red", alpha=0.5)),
gTree(x0=0, x1=1, y0=0, y1=1, default.units="npc"))
myGrob2 <- grobTree(rectGrob(gp=gpar(fill="yellow", alpha=0.5)),
gTree(x0=0, x1=1, y0=0, y1=1, default.units="npc"))
p <- ggplot(mtcars , aes(wt , mpg)) +
geom_line() +
scale_y_continuous(expand=c(0,0)) +
scale_x_continuous(expand=c(0,0)) +
theme(plot.margin=unit(c(1, 1, 1,1), "cm")) +
annotation_custom(myGrob, xmin=-0.5, xmax=1.5, ymin=7.4, ymax=33.9 ) +
annotation_custom(myGrob2, xmin=1.5, xmax=5.4, ymin=7.4, ymax=10.4 )
g <- ggplotGrob(p)
g$layout$clip[g$layout$name=="panel"] <- "off"
grid.draw(g)
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
412 次 |
| 最近记录: |