Gio*_*ioG 5 r ggplot2 gridextra grob r-grid
我想用渐变着色生成 ggplot,填充绘图面板及其背景,如图所示。
如您所见,渐变背景色同时包含绘图面板及其背景。目前,我只知道所需解决方案的“近似值”:
library(ggplot2)
library(grid)
library(gridExtra)
reds <- c("#7B0664", "#E32219")
g <- rasterGrob(reds, width = unit(1, "npc"), height = unit(1, "npc"),
interpolate = TRUE)
ggplot(data = economics, aes(x = date, y = unemploy)) +
annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
geom_line( alpha=1, color = "white", size = 0.5 ) +
xlab("Years") + ylab("Unemployed [thousands]") +
theme(plot.background = element_rect(fill=reds[2]))
Run Code Online (Sandbox Code Playgroud)
使用上面显示的代码,绘图面板的结果是轴边界内的渐变色,但它不会用这种渐变色跨越整个背景。主题(plot.background =...) 能够填充剩余的背景,但它似乎无法利用渐变着色。进一步说明应该将相同的渐变着色应用于整个绘图背景。
任何建议将不胜感激。谢谢。
您可以在 rasterGrob 上打印/绘制绘图,
library(ggplot2)
library(grid)
library(ggthemes)
reds <- c("#7B0664", "#E32219")
g <- rasterGrob(reds, width = unit(1, "npc"), height = unit(1, "npc"), interpolate = TRUE)
p <- ggplot(data = economics, aes(x = date, y = unemploy)) +
geom_line( alpha=1, color = "white", size = 0.5 ) +
xlab("Years") + ylab("Unemployed [thousands]") +
theme_base() +
theme(panel.background=element_blank(),
panel.border = element_blank(),
plot.background=element_blank(),
text = element_text(colour="white"),
line = element_line(colour="white")) +
theme()
grid.newpage()
grid.draw(g)
print(p, newpage = FALSE)
Run Code Online (Sandbox Code Playgroud)