我试图使用ggplot2
绘制多个地块,使用grid.arrange()
排列它们。由于我设法找到了描述我所遇到的确切问题的人,我从链接中引用了问题描述:
当我在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()
同样的链接给出了下面的解决方案:
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()
调用在以下代码中的输出,这是从链接:
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
直接在设备上绘图。另一方面,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
另一种保存网格的简单方法。整理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
它允许在排列中合并其他东西而不是ggplot,例如表…
您不需要使用arrangeGrob,您可以分配grid的结果。直接安排到一个情节,并使用ggsave:
保存p3 <- grid.arrange(p1,p2, nrow = 1)
ggsave("filename.jpg", p3)
我认为这是值得添加的。我在上面遇到了问题,ggsave产生了一个错误:"plot应该是一个ggplot2 plot"
感谢这个答案:在使用ggplot_build和ggplot_gtable之后使用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
try this
ggsave("whatever.png", plot=grid.arrange(plot1, plot2, plot3, nrow=3), device=..., scale = ..., width =..., height = ..., units = "...", dpi = ...)
另一个简单的解决方案:就在你的grid.arrange()
grid.arrange(plot1, plot2, plot3, nrow=3)
你做一个dev.copy()
dev.copy(pdf,"whatever.pdf")
dev.off()