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

Yur*_*kiy 113 graphics transparency r ggplot2

我需要从透明背景输出从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()
Run Code Online (Sandbox Code Playgroud)

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

jor*_*ran 85

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()
Run Code Online (Sandbox Code Playgroud)

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

编辑

ggplot2已经更新,该opts()函数已被弃用.目前,你可以使用theme(),而不是opts()element_rect()替代theme_rect()

  • 如果您正在使用ggsave,请不要忘记添加`bg ="transparent"`以传递给png图形设备. (12认同)
  • 如果你使用`knitr`包(或`slidify`等),你需要传递`dev.args = list(bg ='transparent')`作为块选项.更多详细信息,请访问http://stackoverflow.com/a/13826154/561698 (12认同)

YCR*_*YCR 58

更新了该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
  ) 
Run Code Online (Sandbox Code Playgroud)

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

p <- p +
  theme(
        rect = element_rect(fill = "transparent") # all rectangles
      )
    p
Run Code Online (Sandbox Code Playgroud)

更可控的方式是使用以下选项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
Run Code Online (Sandbox Code Playgroud)

要保存:

ggsave(p, filename = "tr_tst2.png",  bg = "transparent")
Run Code Online (Sandbox Code Playgroud)

  • 如果你没有像上面的答案那样设置`plot.background` 颜色,你的图就会有一个模糊的轮廓。 (2认同)

Rti*_*ist 5

只是为了改进 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
)
Run Code Online (Sandbox Code Playgroud)