将整个网格布局保存为图像或PDF,而不仅仅是最后一个图

时间:2015-06-25 22:56:20

标签: r ggplot2

无论如何都要保存整个布局,而不仅仅是推到网格上的最后一个图?

例如:

a <- (ggplot(mtcars, aes(wt, mpg)) + geom_point())
b <- (ggplot(mtcars, aes(wt, cyl)) + geom_point())
c <- (ggplot(mtcars, aes(wt, disp)) + geom_point())
d <- (ggplot(mtcars, aes(wt, qsec)) + geom_point())

vplayout <- function(x,y){
  viewport(layout.pos.row = x, layout.pos.col = y)
}

grid.newpage()
pushViewport(viewport(layout = grid.layout(2,2)))
print(a, vp = vplayout(1,1))
print(b, vp = vplayout(2,1))
print(c, vp = vplayout(1,2))
print(d, vp = vplayout(2,2))

有没有办法将所有四个图的图像或pdf保存在一起,而不是右下图。

谢谢。

1 个答案:

答案 0 :(得分:4)

只需使用您选择的图形设备:

png()
library(grid) # not loaded by default
grid.newpage()
pushViewport(viewport(layout = grid.layout(2,2)))
print(a, vp = vplayout(1,1))
print(b, vp = vplayout(2,1))
print(c, vp = vplayout(1,2))
print(d, vp = vplayout(2,2))
dev.off()

enter image description here

我使用png()设备完成了这项工作,因此我可以将其粘贴到此处,但pdf()设备也能正常工作。