我有以下 R ggplot 代码:
require(ggplot2)
require(ggthemes)
df <- data.frame(x=1:10, y=5*(1:10))
p <- ggplot(df, aes(x,y)) +
geom_point() +
theme_few() +
theme(plot.background = element_rect(fill='transparent', colour=NA), legend.position='top')
pdf('test.pdf', width=5, height=2)
plot(p)
plot(ggplot_gtable(ggplot_build(p)))
但我确实得到了两个不同的数字:
我喜欢第一个图(即面板区域之外没有背景网格)。但是,我还需要使用 ggplot_build() 进行一些其他处理。你能帮忙吗?
最佳答案
您可以复制 ggplot2::print.ggplot
做得更直接。这似乎有效。
pdf('test.pdf', width=5, height=2)
plot(p)
grid::grid.newpage()
grid::grid.draw(ggplot_gtable(ggplot_build(p)))
dev.off()
关于r - 当绘图背景透明时,plot(p)和plot(ggplot_gtable(ggplot_build(p)))似乎没有给出相同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48066550/