正在将grid.arrange()打印保存到文件

正在将grid.arrange()打印保存到文件,r,ggplot2,gridextra,R,Ggplot2,Gridextra,我正在尝试使用ggplot2绘制多个绘图,并使用grid.arrange()对它们进行排列。 由于我设法找到了描述我遇到的问题的人,我引用了以下问题描述: 当我在grid.arrange()之后使用ggsave()时,即 我不保存网格图,而是保存最后一个单独的网格图。有吗 通过grid.arrange()使用 ggsave()还是类似的东西? 除了使用旧方法之外 同一链接给出了以下解决方案: require(grid) require(gridExtra) p <- arrangeGrob

我正在尝试使用
ggplot2
绘制多个绘图,并使用
grid.arrange()
对它们进行排列。 由于我设法找到了描述我遇到的问题的人,我引用了以下问题描述:

当我在
grid.arrange()
之后使用
ggsave()
时,即

我不保存网格图,而是保存最后一个单独的网格图。有吗 通过
grid.arrange()
使用
ggsave()
还是类似的东西? 除了使用旧方法之外

同一链接给出了以下解决方案:

require(grid)
require(gridExtra)
p <- arrangeGrob(qplot(1,1), textGrob("test"))
grid.draw(p) # interactive device
ggsave("saving.pdf", p) # need to specify what to save explicitly

grid.arrange
直接在设备上绘制<另一方面,code>arrangeGrob不绘制任何内容,而是返回一个grob
g
,您可以将其传递到
ggsave(file=“whatever.pdf”,g)


它与ggplot对象(默认情况下,如果未指定最后一个绘图,则保存最后一个绘图)的工作方式不同的原因是ggplot2不可见地跟踪最新的绘图,我不认为grid.arrange会弄乱这个包专用的计数器。

我对babptiste的建议有一些问题,但最终还是得到了。以下是您应该使用的:

 # draw your plots
 plot1 <- ggplot(...) # this specifies your first plot
 plot2 <- ggplot(...) # this specifies your second plot
 plot3 <- ggplot(...) # this specifies your third plot

 #merge all three plots within one grid (and visualize this)
 grid.arrange(plot1, plot2, plot3, nrow=3) #arranges plots within grid

 #save
 g <- arrangeGrob(plot1, plot2, plot3, nrow=3) #generates g
 ggsave(file="whatever.pdf", g) #saves g
#绘制绘图

plot1我认为值得再加上它。 我在上面遇到了问题,ggsave产生了一个错误: “绘图应为ggplot2绘图”

多亏了这个答案: 我对上述守则有一项修订

  # draw your plots
 plot1 <- ggplot(...) # this specifies your first plot
 plot2 <- ggplot(...) # this specifies your second plot
 plot3 <- ggplot(...) # this specifies your third plot

 #merge all three plots within one grid (and visualize this)
 grid.arrange(plot1, plot2, plot3, nrow=3) #arranges plots within grid

 #save
 ggsave <- ggplot2::ggsave; body(ggsave) <- body(ggplot2::ggsave)[-2]
#绘制绘图

plot1另一种将grid.arrange保存为pdf文件的简单方法是使用pdf()

它允许在排列中合并GGPLOT以外的其他内容,如表格…

另一个简单的解决方案: 就在您的
grid.arrange()之后

执行
dev.copy()


您不需要使用arrangeGrob,可以将grid.arrange的结果直接指定给绘图,并使用ggsave保存:

p3 <- grid.arrange(p1,p2, nrow = 1)
ggsave("filename.jpg", p3)
p3试试这个

ggsave("whatever.png", plot=grid.arrange(plot1, plot2, plot3, nrow=3), device=..., scale = ..., width =..., height = ..., units = "...", dpi = ...)

使用
png();grid.arrange();ggplot();ggplot();dev.off()
Not
print(ggplot())
?@DWin可能是的!:-)@安德烈:你的建议行得通,但图像的分辨率很低。当我使用
ggsave()
保存单个
ggplot
时,图像的分辨率要高得多。是否有办法以高分辨率保存
grid.arrange()
的输出,就像使用
ggsave()
保存单个绘图一样?例如,如果我提供选项
png(…,height=1600,width=2500)
图像看起来非常模糊。当我尝试这样做时,我得到一个错误,告诉我g不是正确的类型?@JackAidley问一个新问题,至少有一个自包含的可复制示例,以及你的sessionInfo()(确保你有最新版本的R和包)@DaveX请不要传播这些误导性信息<代码>绘图(g)
不是显示GTTable的正确方法,请使用
网格。绘制(g)
。@baptiste感谢并返工:请注意,如果您试图查看
g的结果,ggsave将不再处理(部分或全部)grobs。我需要这个。显然,ggplot2的开发版本消除了这样的类测试错误,但目前的CRAN版本(2015-10-21)没有。这对我来说适用于marrangeGrob,但不适用于arrangeGrob或grid。arrange。@DaveX,除了如上所示修改ggsave之外,您还需要做些什么来让它工作吗?谢谢
  # draw your plots
 plot1 <- ggplot(...) # this specifies your first plot
 plot2 <- ggplot(...) # this specifies your second plot
 plot3 <- ggplot(...) # this specifies your third plot

 #merge all three plots within one grid (and visualize this)
 grid.arrange(plot1, plot2, plot3, nrow=3) #arranges plots within grid

 #save
 ggsave <- ggplot2::ggsave; body(ggsave) <- body(ggplot2::ggsave)[-2]
 g <- arrangeGrob(plot1, plot2, plot3, nrow=3) #generates g
 ggsave(file="whatever.pdf", g) #saves g
pdf("filename.pdf", width = 8, height = 12) # Open a new pdf file
grid.arrange(plot1, plot2, plot3, nrow=3) # Write the grid.arrange in the file
dev.off() # Close the file
grid.arrange(plot1, plot2, plot3, nrow=3)
dev.copy(pdf,"whatever.pdf")
dev.off()
p3 <- grid.arrange(p1,p2, nrow = 1)
ggsave("filename.jpg", p3)
ggsave("whatever.png", plot=grid.arrange(plot1, plot2, plot3, nrow=3), device=..., scale = ..., width =..., height = ..., units = "...", dpi = ...)