我正在修改ggplot生成的圖表,通過更改ggplot_build生成的數據(原因類似於)。據我瞭解我在這個主題上找到的幫助,我應該能夠在調用ggsave結果(Saving grid.arrange() plot to file)之前應用ggplot_gtable和arrangeGrob來保存結果。使用ggplot_build和ggplot_gtable保存ggsave圖表
不過,我得到一個錯誤「的情節應該是一個GGPLOT2陰謀」,也與這個簡單的reproductible例如:
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)
非常感謝你,它完美的作品。 –
謝謝。這與[link](http://stackoverflow.com/a/20433318/2173340)相結合,解決了使用'gtable'和'ggsave'保存由'ggplot2'創建的圖的問題。 –
@baptiste我用覆蓋'ggsave'函數試過了你的訣竅,但它不適用於我的情況。同時,'ggplot'仍然是'1.0.1',問題還沒有解決。如果我使用你的方法,我使用'ggsave('test.pdf',arrangeGrob(p1,p2))'創建一個pdf文件,但是它會以某種方式損壞。同時''gtable'打印在控制檯中。所以有些東西不能正常工作。 – drmariod