Multiple plots on one page using ggplot

倾然丶 夕夏残阳落幕 提交于 2019-12-01 20:54:17

You might want to take a look at the cowplot package that allows more flexibility than just using a naked grid.arrange.

This works - albeit a bit inelegantly:

library(ggplot2)
library(grid)
library(gridExtra)
lg <- list()
for (i in 1:12) {
  x <- 1:10
  y <- 2*x + rnorm(x)
  lg[[i]] <- qplot(x, y)
}
grid.arrange(lg[[1]],lg[[2]],lg[[3]],lg[[4]],lg[[5]],lg[[6]],nrow=3,ncol=2)
grid.arrange(lg[[7]],lg[[8]],lg[[9]],lg[[10]],lg[[11]],lg[[12]],nrow=3,ncol=2)

Another more elegant but somewhat obtuse way to do the grid.arrange is the following (thanks to Axeman and beetroot - note the comments).

do.call(grid.arrange, c(lg[1:6], nrow = 3))
do.call(grid.arrange, c(lg[7:12], nrow = 3))

or this:

grid.arrange(grobs = lg[1:6], ncol=2)
grid.arrange(grobs = lg[7:12], ncol=2)

They all result in this - (think two of these - they look the same anyway):

marrangeGrob is a convenient wrapper for multiple pages,

marrangeGrob(lg, ncol=2, nrow=3)

or you can call grid.arrange() explicitly twice,

grid.arrange(grobs = lg[1:6], ncol=2)
grid.arrange(grobs = lg[7:12], ncol=2)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!