将grid.arrange()绘图保存到文件

问题描述 投票:120回答:5

我试图使用ggplot2绘制多个图,使用grid.arrange()安排它们。由于我设法找到某人描述我遇到的确切问题,我引用了link的问题描述:

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

grid.arrange(sgcir1,sgcir2,sgcir3,ncol=2,nrow=2)
ggsave("sgcirNIR.jpg")

我不保存网格图,而是保存最后一个ggplot。有没有办法实际保存grid.arrange()使用ggsave()或类似的东西显示的情节?除了使用旧的方式

jpeg("sgcirNIR.jpg")
grid.arrange(sgcir1,sgcir2,sgcir3,ncol=2,nrow=2)
dev.off()

相同的链接提供以下解决方案:

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

但是,我无法弄清楚如何使用ggsave()来保存grid.arrange()调用的输出,代码来自link

library(ggplot2)
library(gridExtra)
dsamp <- diamonds[sample(nrow(diamonds), 1000), ] 

p1 <- qplot(carat, price, data=dsamp, colour=clarity)
p2 <- qplot(carat, price, data=dsamp, colour=clarity, geom="path")

g_legend<-function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)}

legend <- g_legend(p1)
lwidth <- sum(legend$width)

## using grid.arrange for convenience
## could also manually push viewports
grid.arrange(arrangeGrob(p1 + theme(legend.position="none"),
                                        p2 + theme(legend.position="none"),
                                        main ="this is a title",
                                        left = "This is my global Y-axis title"), legend, 
                     widths=unit.c(unit(1, "npc") - lwidth, lwidth), nrow=1)

# What code to put here to save output of grid.arrange()?
r ggplot2 gridextra
5个回答
129
投票

grid.arrange直接在设备上绘制。另一方面,arrangeGrob不会画任何东西,只会返回一个grob g,你可以传递给ggsave(file="whatever.pdf", g)

它与ggplot对象的工作原理不同,默认情况下,如果没有指定,最后一个图是保存的,那就是ggplot2无形地跟踪最新的情节,我不认为grid.arrange应该把这个计数器私有化了。


39
投票

我在巴蒂斯特的建议中遇到了一些问题,但终于得到了它。这是你应该使用的:

 # 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

这应该很好。


20
投票

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

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

它允许在排列中合并除ggplots之外的其他内容,例如表...


6
投票

我认为值得补充一点。我有上述问题,ggsave产生错误:“情节应该是ggplot2情节”

感谢这个答案:Saving a graph with ggsave after using ggplot_build and ggplot_gtable我对上述代码进行了修改。

  # 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

现在它对我很好。


1
投票

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

grid.arrange(plot1, plot2, plot3, nrow=3)

你做一个dev.copy()

dev.copy(pdf,"whatever.pdf")
dev.off()
© www.soinside.com 2019 - 2024. All rights reserved.