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

如何使用ggplot2在R中制作具有透明背景的图形?,r,graphics,transparency,ggplot2,R,Graphics,Transparency,Ggplot2,我需要输出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)

我需要输出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()

d除了
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()

df更新了
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
  ) 
更受控制的方法是使用
主题的选项

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

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

MyTheme\u transparent对于不喜欢灰色背景的人,比如学术编辑,可以尝试以下方法:

p <- p + theme_bw()
p

Cairo包可用于将ggplot保存为具有透明背景的图像。


当使用Mac平台当前的PowerPoint进行测试时,我并不期望它能与Mac一起工作,但它的工作原理与广告一样。如果你删除单位并用英寸替换尺寸,它也适用于pdf。这对MS Powerpoint 2010非常有效。实际上,我正是出于这个目的才需要它的。如果您使用ggsave,请不要忘记添加
bg=“transparent”
以传递到png图形设备。如果您使用
knitr
包(或
slidify
等),则需要将
dev.args=list(bg='transparent')
作为块选项传递。更详细地看,当前的解决方案是添加<代码>主题(PANEL.Base= EntEngReCt(填充=“透明”,颜色=NA),PLOUT = EntEngReCt(填充=“透明”,颜色= NA))/>代码>请考虑标记第二答案(YRC)。由于“opts”已过时而被接受。如果不设置
绘图。背景
颜色,如上面的答案,绘图将有一个模糊的轮廓。啊。。。花了太多时间不知道最后一步。保存文件时,bg=“transparent”。
theme\u bw
提供白色背景,而不是透明背景。
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
)
p <- p + theme_bw()
p
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()