r - 当绘图背景透明时,plot(p)和plot(ggplot_gtable(ggplot_build(p)))似乎没有给出相同的输出

标签 r ggplot2

我有以下 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)))

但我确实得到了两个不同的数字:

enter image description here

enter image description here

我喜欢第一个图(即面板区域之外没有背景网格)。但是,我还需要使用 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/

相关文章:

r - 如何增加ggplot2中的条形大小?

r - 使用 R 中的 tmap 将饼图绘制成 map

r - AWS dynamodb 对 "R"编程语言的支持

r - 我是唯一一个遇到 R 避风港包问题的人吗?

r - 如何将统计测试的结果作为绘图数学表达式包含在 ggplot2 facet 中

r - 绘制正确的颜色和缺失值的有序因子

r - 使用 tidyverse 清理排名选择调查

基于具有相同标签和多个值的两个向量(级别和标签)重新编码值以替换

r - 在子集化时允许零索引有什么意义?

r - ggplot2 : full number without comma? 中的日志轴标签