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

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

问题描述

我正在尝试使用 ggplot2 绘制多个图,并使用 grid.arrange() 排列它们.由于我设法找到了描述我遇到的确切问题的人,因此我引用了 link 中的问题描述:

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

<块引用>

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()

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

要求(网格)需要(网格额外)p <-arrangeGrob(qplot(1,1), textGrob("test"))grid.draw(p) # 交互设备ggsave("saving.pdf", p) # 需要明确指定要保存的内容

但是,我不知道如何使用ggsave()来保存下面代码中grid.arrange()调用的输出,即取自链接:

库(ggplot2)图书馆(gridExtra)dsamp <- 钻石[样本(nrow(钻石),1000),]p1 <- qplot(克拉,价格,数据=dsamp,颜色=清晰度)p2 <- qplot(克拉,价格,数据=dsamp,颜色=清晰度,geom=路径")g_legend<-function(a.gplot){tmp <- ggplot_gtable(ggplot_build(a.gplot))腿 <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")图例 <- tmp$grobs[[leg]]返回(传说)}图例 <- 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天全站免登陆