我想输出ggplot2图形有透明边框(面板)但是白色(不透明)图背景。
我尝试了这两个选项
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()
答案 0 :(得分:2)
嗯,诀窍很明显。我误解了面板和情节背景是什么。所以这个应该工作:
# third option
p <- p + theme(
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
plot.background = element_rect(fill = "transparent", colour = NA)
)