2016-09-22 187 views
2

我想用渐变着色生成ggplot,填充绘图面板和背景,如图所示。R:ggplot背景渐变着色

enter image description here

正如你可以看到渐变背景着色涵盖情节面板及其背景。目前,所需解决的只是一个“近似”被称为对我说:

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])) 

使用aboveshown码,剧情面板的结果梯度轴范围内着色,但它不跨越这样的大背景下渐变着色。主题(plot.background = ...)能够填充剩余的背景,但似乎无法利用渐变着色。要进一步评论,相同的梯度着色应该应用于总体情节背景。

任何建议,将不胜感激。谢谢。

回答

0

可以打印/再接上rasterGrob顶部的情节,

enter image description here

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)