it-swarm.cn

如何使用ggplot2在R中制作具有透明背景的图形?

我需要从透明背景输出从R到PNG文件的ggplot2图形。基本的R图形一切都很好,但ggplot2没有透明度:

d <- rnorm(100) #generating random data

#this returns transparent png
png('tr_tst1.png',width=300,height=300,units="px",bg = "transparent")
boxplot(d)
dev.off()

df <- data.frame(y=d,x=1)
p <- ggplot(df) + stat_boxplot(aes(x = x,y=y)) 
p <- p + opts(
    panel.background = theme_rect(fill = "transparent",colour = NA), # or theme_blank()
    panel.grid.minor = theme_blank(), 
    panel.grid.major = theme_blank()
)
#returns white background
png('tr_tst2.png',width=300,height=300,units="px",bg = "transparent")
p
dev.off()

有没有办法让ggplot2获得透明背景?

103
Yuriy Petrovskiy

更新了theme()函数,ggsave()和图例背景的代码:

df <- data.frame(y = d, x = 1, group = rep(c("gr1", "gr2"), 50))
p <- ggplot(df) +
  stat_boxplot(aes(x = x, y = y, color = group)
    , fill = "transparent" # for the inside of the boxplot
  ) 

最快的方法是使用rect,因为所有矩形元素都从rect继承:

p <- p +
  theme(
        rect = element_rect(fill = "transparent") # all rectangles
      )
    p

更受控制的方式是使用theme的选项:

p <- p +
  theme(
    panel.background = element_rect(fill = "transparent") # bg of the panel
    , plot.background = element_rect(fill = "transparent", color = NA) # bg of the plot
    , panel.grid.major = element_blank() # get rid of major grid
    , panel.grid.minor = element_blank() # get rid of minor grid
    , legend.background = element_rect(fill = "transparent") # get rid of legend bg
    , legend.box.background = element_rect(fill = "transparent") # get rid of legend panel bg
  )
p

要保存:

ggsave(p, filename = "tr_tst2.png",  bg = "transparent")
45
YCR

除了plot.background之外,还有一个panel.background选项:

df <- data.frame(y=d,x=1)
p <- ggplot(df) + stat_boxplot(aes(x = x,y=y)) 
p <- p + opts(
    panel.background = theme_rect(fill = "transparent",colour = NA), # or theme_blank()
    panel.grid.minor = theme_blank(), 
    panel.grid.major = theme_blank(),
    plot.background = theme_rect(fill = "transparent",colour = NA)
)
#returns white background
png('tr_tst2.png',width=300,height=300,units="px",bg = "transparent")
print(p)
dev.off()

出于某种原因,上传的图像显示与我的计算机上的显示不同,所以我省略了它。但对我来说,我得到一个完全灰色背景的情节,除了箱形图的盒子部分仍然是白色的。我相信,这可以通过boxplot geom中的填充美学来改变。

编辑

ggplot2 已经更新,并且不推荐使用opts()函数。目前,您将使用theme()而不是opts()element_rect()而不是theme_rect()等。

84
joran

只是为了改善YCR的答案:

1)我在x和y轴上添加了黑线。否则它们也会变得透明。

2)我在图例键中添加了透明主题。否则,你会在那里得到一个填充,这将不是很美观。

最后,请注意所有这些仅适用于pdf和png格式。 jpeg无法生成透明图。

MyTheme_transparent <- theme(
    panel.background = element_rect(fill = "transparent"), # bg of the panel
    plot.background = element_rect(fill = "transparent", color = NA), # bg of the plot
    panel.grid.major = element_blank(), # get rid of major grid
    panel.grid.minor = element_blank(), # get rid of minor grid
    legend.background = element_rect(fill = "transparent"), # get rid of legend bg
    legend.box.background = element_rect(fill = "transparent"), # get rid of legend panel bg
    legend.key = element_rect(fill = "transparent", colour = NA), # get rid of key legend fill, and of the surrounding
    axis.line = element_line(colour = "black") # adding a black line for x and y axis
)
2
Rtist