2011-09-17 16 views
80

Ggplot2 grafiklerini R'den PNG dosyalarına şeffaf arka plana sahip olarak vermem gerekiyor. Her şey temel R grafiklerle Tamam, ama ggplot2 ile hiçbir saydamlık:Ggplot2'yi kullanarak R'de şeffaf arka planlı grafikler nasıl oluşturulur?

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 ile şeffaf bir arka plan almak için herhangi bir yolu var mı?

+0

bakınız [Bu yanıt] (http://stackoverflow.com/questions/41856399/how-plot-transparent-background-ggplot), mevcut sol ution, 'theme (panel.background = element_rect (fill = "transparent", color = NA), plot.background = element_rect (fill = "transparent", color = NA)) eklemek için' –

cevap

72

da panel.background ek olarak bir plot.background seçenek var: Nedense

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

, yüklenen görüntü farklı bilgisayarımda daha görüntülüyor, bu yüzden ihmal ettik. Ama benim için, hala beyaz olan boxplot kutusunun parçası dışında tamamen gri bir arka plan ile bir arsa olsun. Bu da boxplot coğrafyasında dolgu estetiği kullanılarak değiştirilebilir, inanıyorum.

Düzenleme

ggplot2 beri güncellendi ve opts() fonksiyon kullanımdan kaldırıldı. Şu anda, theme() yerine opts() yerine theme_rect() ait element_rect(), kullanacağı vb

+0

o platformun mevcut PowerPoint'le test edildiğinde Mac ile çalışın, ancak reklamı yapılan gibi çalışır. Ve birimleri çıkarırsanız ve ebatları inç olarak değiştirirseniz pdf ile çalışır. İyi iş. –

+1

Bu MS Powerpoint 2010 ile mükemmel çalışıyor. Aslında, bu amaç için doğru gerekiyordu. –

+12

ggsave kullanıyorsanız, png grafik aygıtına geçmek için 'bg =" transparent "' yazmayı unutmayın. – Tom

15

theme() fonksiyonu ggsave() ve efsane arka plan için kodu ile Güncelleme: Bütün olarak,

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 

veya kullanan rect'in dikdörtgen elemanlar rect'in devralan:

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

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