【问题标题】:How to make graphics with transparent background in R using ggplot2?如何使用 ggplot2 在 R 中制作具有透明背景的图形?
【发布时间】:2011-11-19 06:45:42
【问题描述】:

我需要将 ggplot2 图形从 R 输出到具有透明背景的 PNG 文件。基本的 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获得透明背景?

【问题讨论】:

  • 另见this answer,目前的解决办法是加theme(panel.background = element_rect(fill = "transparent", colour = NA), plot.background = element_rect(fill = "transparent", colour = NA))
  • 由于“选择”已过时,请考虑将第二个答案(由 YRC)标记为已接受。

标签: r graphics transparency ggplot2


【解决方案1】:

更新了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")

【讨论】:

  • 如果你没有像上面的答案那样设置plot.background颜色,你的情节将会有一个模糊的轮廓。
  • 啊……花了太多时间不知道最后一步。用 , bg = "transparent" 保存文件。
  • 我不敢相信 2022 年 ggsave 的单位是英寸而不是像素。
【解决方案2】:

除了panel.background之外,还有一个plot.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()

由于某种原因,上传的图像与我的计算机上显示的不同,所以我省略了它。但对我来说,我得到一个完全灰色背景的情节,除了箱形图的方框部分仍然是白色的。我相信,这也可以使用箱线图几何中的填充美学来改变。

编辑

ggplot2 已更新,opts() 函数已被弃用。目前,您将使用theme() 代替opts()element_rect() 代替theme_rect() 等。

【讨论】:

  • 在使用 Mac 平台的当前 PowerPoint 进行测试时,我没想到它可以与 Mac 一起使用,但它可以像宣传的那样工作。如果您删除单位并以英寸为单位替换尺寸,它也适用于 pdf 干得好。
  • 这适用于 MS Powerpoint 2010。实际上,我需要它来达到这个目的。
  • 如果您使用的是 ggsave,请不要忘记添加 bg = "transparent" 以传递给 png 图形设备。
  • 如果您使用knitr 包(或slidify 等),您需要将dev.args = list(bg = 'transparent') 作为块选项传递。更多细节在这里stackoverflow.com/a/13826154/561698
【解决方案3】:

只是为了改进 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
)

【讨论】:

    【解决方案4】:

    至于像学术编辑这样不喜欢灰色背景的人,试试这个:

    p <- p + theme_bw()
    p
    

    【讨论】:

    • theme_bw 提供白色背景,而不是透明背景。
    【解决方案5】:

    Cairo 包可用于将 ggplots 保存为具有透明背景的图像。 https://cran.r-project.org/web/packages/Cairo/Cairo.pdf

    CaiorPNG(filename = "TEST.png", bg = "transparent")
    
    ggplot(mtcars, aes(wt, mpg))+
       geom_point()+
       theme(panel.background = element_rect(fill = "transparent"),
          plot.background = element_rect(fill = "transparent", colour = NA))
    
    dev.off()
    

    【讨论】:

      【解决方案6】:

      我相信这适用于那些在 R Markdown 中工作并且不想使用 ggsave 保存单独文件的人。

      您执行以下操作,只需添加此块选项:{r, dev.args = list(bg = 'transparent')}:

      ggplot(mtcars, aes(wt, mpg)) +
         geom_point() +
         theme(
           # makes background transparent:
           plot.background = element_rect(fill = "transparent",colour = NA),
           # gets rid of white border around plot: 
           panel.border = element_blank() 
         )
      

      例如,我在 R Markdown 中使用 ioslides 演示文稿,但请注意,我没有在此上下文之外进行测试。

      【讨论】:

        猜你喜欢
        • 2020-10-18
        • 2020-05-05
        • 2012-07-07
        • 2011-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-22
        相关资源
        最近更新 更多