使用ggplot_build和ggplot_gtable后使用ggsave保存图形 [英] Saving a graph with ggsave after using ggplot_build and ggplot_gtable

查看:1901
本文介绍了使用ggplot_build和ggplot_gtable后使用ggsave保存图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过修改ggplot_build生成的数据来修改使用ggplot构建的图形(原因类似于在geom_boxplot中填充美学时使用缺少的因子级别空间)。据我了解我在这个主题上找到的帮助,我应该能够通过应用ggplot_gtable和arrangeGrob在结果上调用ggsave之前保存结果(保存grid.arrange()图到文件)。

然而,我得到一个错误plot应该是一个ggplot2情节,也有这个简单的可重现的例子:

  require('ggplot2')
require 'gridExtra')
df < - data.frame(f1 = factor(rbinom(100,1,0.45),label = c(m,w)),
f2 = factor (rb(100,1,0.45),label = c(young,old)),
boxthis = rnorm(100))
g < - ggplot(aes(y = boxthis, x = f2,fill = f1),data = df)+ geom_boxplot()
dd < - ggplot_build(g)

#打印图表:
print(arrangeGrob (ggplot_gtable(dd)))

#保存图不包含:
ggsave('test.png',arrangeGrob(ggplot_gtable(dd)))

任何人都可以解释为什么这不起作用吗?有没有办法使用ggplot_build()修改数据后使用ggsave?



(我的版本包为gridExtra_0.9.1和ggplot2_0.9.3.1)

解决方案

它不起作用,因为 ggsave 需要类 ggplot ,而你正在传递一个grob。 arrangeGrob 在假装继承 ggplot 时有时会欺骗 ggsave ,但只有当至少有一个grob属于这个类别时;在这里,然而,你只传递一个 gtable



也许最简单的解决方法是克隆ggsave和绕过class check,

  ggsave < -  ggplot2 :: ggsave; body(ggsave)<  -  body(ggplot2 :: ggsave)[ -  2] 

编辑: ggplot2的开发版本不再需要这个破解*,如 ggsave 现在可以与任何grob一起工作



* PS:这个hack不再工作,因为arrangeGrob现在返回一个gtable ,并且其打印方法不会在设备上绘制。


I am modifying a graph built with ggplot by altering the data produced by ggplot_build (for a reason similar to Include space for missing factor level used in fill aesthetics in geom_boxplot). As far as I understand the help I found on this topic, I should be able to save the result by applying ggplot_gtable and arrangeGrob before calling ggsave on the results (Saving grid.arrange() plot to file).

However I obtain an error "plot should be a ggplot2 plot", also with this simple reproductible example:

require('ggplot2')
require('gridExtra')
df <- data.frame(f1=factor(rbinom(100, 1, 0.45), label=c("m","w")), 
                  f2=factor(rbinom(100, 1, 0.45), label=c("young","old")),
                  boxthis=rnorm(100))
g <- ggplot(aes(y = boxthis, x = f2, fill = f1), data = df) + geom_boxplot()
dd <- ggplot_build(g)

# Printing the graph works:
print(arrangeGrob(ggplot_gtable(dd)))

# Saving the graph doesn't:
ggsave('test.png',arrangeGrob(ggplot_gtable(dd)))

Can anybody explain why this does not work ? Is there a way to use ggsave after modifying the data by using ggplot_build() ?

(My version of the packages are gridExtra_0.9.1 and ggplot2_0.9.3.1)

解决方案

it does not work because ggsave wants an object of class ggplot, while you're passing a grob. arrangeGrob will sometimes trick ggsave in pretending inheritance from ggplot, but only when at least one of the grobs belongs to this class; here, however, you're only passing a gtable.

Perhaps the easiest workaround is to clone ggsave and bypass the class check,

ggsave <- ggplot2::ggsave; body(ggsave) <- body(ggplot2::ggsave)[-2]

Edit: The dev version of ggplot2 no longer requires this hack*, as ggsave now works with any grob.

*PS: this hack works no longer, as arrangeGrob now returns a gtable, and its print method does not draw on a device.

这篇关于使用ggplot_build和ggplot_gtable后使用ggsave保存图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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