2016-06-28 12 views
1

Ich möchte meine Ausgabe ggplot2 Grafiken haben transparente Grenze (Panel), aber weiß (nicht transparent) plot Hintergrund.Panel Transparenz in ggplot

habe ich versucht, diese beiden Optionen

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() 

Aber ich habe Nun unbefriedigenden Ausgang enter image description here

Antwort

2

, der Trick ist offensichtlich. Ich habe missverstanden, was Panel und Hintergrund ist. So sollte das funktionieren:

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