Jak zrobić grafikę z przezroczystym tłem w R za pomocą ggplot2?
Muszę wypuścić grafikę ggplot2 z plików R do PNG z przezroczystym tłem. Wszystko jest ok z podstawową grafiką R, ale bez przezroczystości z 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()
Czy Jest jakiś sposób na uzyskanie przezroczystego tła z ggplot2?
2 answers
Zaktualizowany o funkcję theme()
, ggsave()
i Kod tła legendy:
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
)
Najszybszym sposobem jest użycie rect
, ponieważ wszystkie elementy prostokąta dziedziczą z rect:
p <- p +
theme(
rect = element_rect(fill = "transparent") # all rectangles
)
p
Bardziej kontrolowanym sposobem jest użycie opcji theme
:
p <- p +
theme(
panel.background = element_rect(fill = "transparent") # bg of the panel
, plot.background = element_rect(fill = "transparent", col = NA) # 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
Aby zapisać:
ggsave(p, filename = "tr_tst2.png", bg = "transparent")
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-16 13:50:31
Oprócz panel.background
Istnieje również opcja plot.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()
Z jakiegoś powodu przesłany obraz wyświetla się inaczej niż na moim komputerze, więc go pominąłem. Ale dla mnie, dostaję działkę z całkowicie szarym tłem, z wyjątkiem części pudełkowej boxplot, która jest nadal Biała. To można zmienić za pomocą estetyki wypełnienia w boxplot geom, jak sądzę.
Edit
Ggplot2 został zaktualizowany, a funkcja opts()
została deprecated. Obecnie używasz theme()
zamiast opts()
i element_rect()
zamiast theme_rect()
itp.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-03 01:04:34