2016-06-28 93 views
1

我想我的輸出ggplot2圖形有透明邊框(面板),但白色(不透明)情節背景。ggplot中的面板透明度

我想這兩個選項

d <- rnorm(100) 
df <- data.frame(y = d, x = 1) 
p <- ggplot(df) + stat_boxplot(aes(x = x, y = y)) 
# first option 
p <- p + theme(
    panel.background = element_rect(fill = "transparent", colour = NA), 
    panel.grid.minor = element_blank(), 
    panel.grid.major = element_blank() 
) 
# second option 
# p <- p + theme(
# panel.background = element_rect(fill = "transparent", colour = NA), 
# panel.grid.minor = element_blank(), 
# panel.grid.major = element_blank(), 
# plot.background = element_rect(fill = "transparent", colour = NA) 
#) 

png('plot.png', width = 300, height = 300, units = "px", bg = "transparent") 
print(p) 
dev.off() 

但我不滿意輸出 enter image description here

回答

2

好,訣竅是顯而易見的。我誤解了面板和情節背景。所以這一個應該工作:

# third option 
p <- p + theme(
    panel.grid.minor = element_blank(), 
    panel.grid.major = element_blank(), 
    plot.background = element_rect(fill = "transparent", colour = NA) 
)