当绘图背景透明时,plot(p)和plot(ggplot_gtable(ggplot_build(p)))似乎没有提供相同的输出 [英] plot(p) and plot(ggplot_gtable(ggplot_build(p))) don't seem to give same output when plot background is transparent

查看:99
本文介绍了当绘图背景透明时,plot(p)和plot(ggplot_gtable(ggplot_build(p)))似乎没有提供相同的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下R ggplot代码:

I have the following R ggplot code:

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

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

But I do get two different figures:

我喜欢第一个图形(即面板区域外没有背景网格).但是,我还需要使用ggplot_build()进行其他处理.你能帮忙吗?

I like the first figure (i.e without the background grid outside panel area). However, I also need to use ggplot_build() for some other processing. Could you please help?

推荐答案

您可以直接复制ggplot2::print.ggplot的功能.这似乎可行.

You could copy what ggplot2::print.ggplot does more directly. This seems to work.

pdf('test.pdf', width=5, height=2)

plot(p)

grid::grid.newpage()
grid::grid.draw(ggplot_gtable(ggplot_build(p)))

dev.off()

这篇关于当绘图背景透明时,plot(p)和plot(ggplot_gtable(ggplot_build(p)))似乎没有提供相同的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆