我想为用作ggplot背景的rastergrob对象添加透明度。
这是我的代码
library(ggplot2)
library(grid)
library(ggthemes)
reds <- c("brown", "red","orange","green","orange","red","brown","grey")
g <- rasterGrob(reds, width = unit(1, "npc"), height = unit(1,"npc"),interpolate = TRUE)
p <- 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_base() +
theme(panel.background=element_blank(),
plot.background=element_blank(),
line = element_line(colour="white")) +
theme()
grid.newpage()
print(p, newpage = FALSE)
我无法在ratestgrob中添加alpha,也不能在annotation_custom中添加。我一直在寻找。
答案 0 :(得分:0)
我发现一种可能的方法是使用函数adjustcolor()来获取透明度参数&#34; alpha&#34;以及您的颜色列表并返回透明颜色列表
答案 1 :(得分:0)
scales::alpha()
是一个选项,
grid.newpage()
grid.text("background")
reds <- c("brown", "red","orange","green","orange","red","brown","grey")
grid.raster(scales::alpha(reds, 0.5), width = unit(1, "npc"), height = unit(1,"npc"),interpolate = TRUE)