【发布时间】:2013-08-26 18:01:02
【问题描述】:
我正在通过更改 ggplot_build 生成的数据来修改使用 ggplot 构建的图形(原因类似于 Include space for missing factor level used in fill aesthetics in geom_boxplot)。据我了解我在这个主题上找到的帮助,我应该能够通过应用 ggplot_gtable 和arrangeGrob 来保存结果,然后再调用 ggsave 结果 (Saving grid.arrange() plot to file)。
但是我得到一个错误“情节应该是一个 ggplot2 情节”,也有这个简单的可复制的例子:
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)))
谁能解释为什么这不起作用?使用 ggplot_build() 修改数据后有没有办法使用 ggsave ?
(我的包版本是gridExtra_0.9.1和ggplot2_0.9.3.1)
【问题讨论】: