2011-09-17 14 views
80

Devo stampare file ggplot2 da file R in PNG con sfondo trasparente. Tutto è ok con grafica di base R, ma nessuna trasparenza con ggplot2:Come creare grafica con sfondo trasparente in R usando 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() 

C'è un modo per ottenere sfondo trasparente con ggplot2?

+0

Vedi anche [questa risposta] (http://stackoverflow.com/questions/41856399/how-plot-transparent-background-ggplot), il sol corrente ution è aggiungere 'theme (panel.background = element_rect (fill =" transparent ", color = NA), plot.background = element_rect (fill =" transparent ", color = NA))' –

risposta

72

C'è anche un'opzione plot.background oltre a panel.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() 

Per qualche ragione, l'immagine caricata sta visualizzando in modo diverso rispetto sul mio computer, così ho omesso di esso. Ma per me, ottengo una trama con uno sfondo completamente grigio ad eccezione della parte scatolare del boxplot che è ancora bianca. Questo può essere cambiato usando l'estetica del riempimento nel geom boxplot, credo.

Modifica

ggplot2 da allora è stato aggiornato e la funzione opts() è stato sconsigliato. Attualmente, si usa al posto di theme()opts() e element_rect() invece di theme_rect(), ecc

+0

Non mi aspettavo che funziona con un Mac quando viene testato con l'attuale PowerPoint di quella piattaforma, ma funziona come pubblicizzato. E funziona anche con i pdf se rimuovi le unità e sostituisci le dimensioni in pollici Buon lavoro. –

+1

Funziona perfettamente con MS Powerpoint 2010. In realtà, avevo bisogno di farlo per questo scopo. –

+12

Se stai usando ggsave, non dimenticare di aggiungere in 'bg =" transparent "' per passare al dispositivo grafico png. – Tom

15

Aggiornato con la funzione theme(), ggsave() e il codice per lo sfondo leggenda:

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") # 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 

o utilizzando rect, come tutto il elementi rettangolo ereditano da rect:

p <- p + 
    theme(
    rect = element_rect(fill = "transparent") # bg of the panel 
) 
p 

ggsave(p, filename = "tr_tst2.png", bg = "transparent")