Dav*_*aze 4 printing r ggplot2
作为这个问题的后续,假设我做了这样的事情:
p <- ggplot(mtcars, aes(mpg)) + geom_histogram(aes(y = ..count..)) + facet_wrap(~am)
r <- print(p)
Run Code Online (Sandbox Code Playgroud)
在第二行中,我调用 print 方法,以便在向绘图对象添加其他图层之前可以以编程方式检查其返回值。
我的问题:有没有办法抑制此时绘制情节?
如果您查看内部,ggplot2:::print.ggplot您会发现您可能想要使用的是 或ggplot_build(),ggplot_gtable()具体取决于您想要检查的信息。
ggplot_build返回由 ggplot2 的 print 方法无形返回的数据对象,所以这可能就是您想要的。ggplot_gtable返回 grobs 本身,这允许直接修改网格图形对象本身。