将grid.arrange()图保存到文件 [英] Saving grid.arrange() plot to file

查看:1018
本文介绍了将grid.arrange()图保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用 ggplot2 绘制多个图,并使用 grid.arrange()来排列它们。
由于我设法找到描述我确切问题的人,所以我引用了之后使用 ggsave()时 grid.arrange(),即


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




我不保存网格图,而是保存最后一个单独的ggplot。是否有任何
的方式实际上通过使用
ggsave() grid.arrange() code>或类似的东西?
除了使用旧的方式


  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)#交互设备
ggsave(saving.pdf,p)#需要明确指定要保存的内容

然而,我不知道如何使用 ggsave()来保存 grid.arrange()调用的输出以下代码取自链接

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

p1 < - qplot(克拉,价格,数据= dsamp,颜色=净度)
p2 < ; - qplot(carat,price,data = dsamp,color = clarity,geom =path)

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

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

##使用grid.arrange以方便
##也可以手动推送视口
grid.arrange(arrangeGrob(p1 + theme(legend.position =none),
p2 +主题(legend.position =none),
main =这是一个标题,
left =这是我的全球Y轴标题),图例
widths = unit.c(unit(1,npc) - lwidth,lwidth),nrow = 1)

#在这里放置什么代码来保存grid.arrange()的输出?


解决方案

grid.arrange 直接在设备上绘制。另一方面, arrangeGrob 不会绘制任何内容,而是返回一个grob g ,您可以传递给 ggsave(file =whatever.pdf,g)



它的工作原理与ggplot对象不同,在默认情况下,如果没有指定,最后一个绘图将被保存,是ggplot2无形地跟踪最新的绘图,我不认为> grid.arrange 应该混淆这个反私人包裹。


I am trying to plot multiple plots using ggplot2, arranging them using grid.arrange(). Since I managed to find someone describing the exact problem I have, I have quoted from the problem description from link:

When I use ggsave() after grid.arrange(), i.e.

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

I do not save the grid plot but the last individual ggplot. Is there any way of actually saving the plot as displayed by grid.arrange() using ggsave() or something similar? Other than using the older way

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

The same link gives the solution below:

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

However, I can't figure out how to use ggsave() to save the output of the grid.arrange() call in the following code, which is taken from 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()?

解决方案

grid.arrange draws directly on a device. arrangeGrob, on the other hand, doesn't draw anything but returns a grob g, that you can pass to ggsave(file="whatever.pdf", g).

The reason it works differently than with ggplot objects, where by default the last plot is being saved if not specified, is that ggplot2 invisibly keeps track of the latest plot, and I don't think grid.arrange should mess with this counter private to the package.

这篇关于将grid.arrange()图保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆