我需要從透明背景的R到PNG文件輸出ggplot2圖形。一切都確定了基本的R圖形,但GGPLOT2沒有透明度:如何使用ggplot2在R中使用透明背景製作圖形?
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?
參見[此答案](http://stackoverflow.com/questions/41856399/how-plot-transparent-background-ggplot),當前溶膠ution是添加'theme(panel.background = element_rect(fill =「transparent」,color = NA),plot.background = element_rect(fill =「transparent」,color = NA))' –