R-将透明度添加到ggplot的光栅背景

R-将透明度添加到ggplot的光栅背景,r,ggplot2,background,transparency,R,Ggplot2,Background,Transparency,我想为用作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 =

我想为用作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)
库(ggplot2)
图书馆(网格)
图书馆(主题)

红色我发现一种可能的方法是使用函数adjustcolor(),该函数接受透明度“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)
grid.newpage()
网格文本(“背景”)

你试过用数值吗?例如,使用“#F5262688”代替“红色”。是的,我找到了“adjustcolor”函数,它可以将向量的所有颜色转换为透明的等效颜色。谢谢也许你可以发布你自己问题的答案,这样人们就会知道答案已经被回答了。