我希望能夠將ggplot2中的許多圖繪製爲單個PDF。我在下面創建了可重現的代碼,創建了我收到的錯誤消息。將多個ggplot2圖組合到PDF上
m <- matrix(data=cbind(rnorm(30, 0), rnorm(30, 2), rnorm(30, 5)), nrow=30, ncol=3)
df <- as.data.frame(m)
dfs <- stack(df)
uniqueplot1=ggplot(dfs, aes(x=values)) + geom_density()
uniqueplot2=ggplot(dfs, aes(x=values)) + geom_density()
objects=ls()
plot_search=grep("uniqueplot",objects)
objects=objects[plot_search]
pdf("plots.pdf")
grid.arrange(objects,ncol=2)
dev.off()
的錯誤,我得到的是:
Error in gList(list("uniqueplot1", "uniqueplot2", wrapvp = list(x = 0.5, :
only 'grobs' allowed in "gList"
In addition: Warning message:
In grob$wrapvp <- vp : Coercing LHS to a list
有沒有辦法將項目objects
轉換成正確的對象類型,以便它可以與grid.arrange成功地使用?
編輯:這是一個簡單的例子 - 在現實生活中,我將生產數百個情節,並且不可能單獨列出所有這些情節。
會像'grid.arrange(uniqueplot1,uniqueplot2,ncol = 2)'工作嗎? –
不同的方法:你之前使用過RMarkdown嗎? – Wietze314