Как я могу организовать произвольное количество ggplots, используя grid.arrange? - программирование
Подтвердить что ты не робот

Как я могу организовать произвольное количество ggplots, используя grid.arrange?

Это перекрестная ссылка в группе ggplot2 google

Моя ситуация в том, что я работающий над функцией, который выводит произвольное количество графиков (в зависимости от входных данных, предоставленных пользователем). Функция возвращает список n графиков, и я хотел бы выложить эти сюжеты в 2 x 2 образования. Я борюсь с одновременными проблемами:

  • Как я могу позволить гибкости передать произвольное (n) количество графиков?
  • Как я могу указать, я хочу, чтобы они выложили 2 x 2

В моей текущей стратегии используется grid.arrange из пакета gridExtra. Это, вероятно, не оптимально, тем более, что, и это ключ, он полностью не работает. Здесь мой прокомментированный пример кода, экспериментируя с тремя графиками:

library(ggplot2)
library(gridExtra)

x <- qplot(mpg, disp, data = mtcars)
y <- qplot(hp, wt, data = mtcars)
z <- qplot(qsec, wt, data = mtcars)

# A normal, plain-jane call to grid.arrange is fine for displaying all my plots
grid.arrange(x, y, z)

# But, for my purposes, I need a 2 x 2 layout. So the command below works acceptably.
grid.arrange(x, y, z, nrow = 2, ncol = 2)

# The problem is that the function I'm developing outputs a LIST of an arbitrary
# number plots, and I'd like to be able to plot every plot in the list on a 2 x 2
# laid-out page. I can at least plot a list of plots by constructing a do.call()
# expression, below. (Note: it totally even surprises me that this do.call expression
# DOES work. I'm astounded.)
plot.list <- list(x, y, z)
do.call(grid.arrange, plot.list)

# But now I need 2 x 2 pages. No problem, right? Since do.call() is taking a list of
# arguments, I'll just add my grid.layout arguments to the list. Since grid.arrange is
# supposed to pass layout arguments along to grid.layout anyway, this should work.
args.list <- c(plot.list, "nrow = 2", "ncol = 2")

# Except that the line below is going to fail, producing an "input must be grobs!"
# error
do.call(grid.arrange, args.list)

Как я обычно это делаю, я смиренно сжимаюсь в углу, с нетерпением ожидая прозорливого отзыва сообщества, намного мудрее, чем я. Особенно, если я делаю это сложнее, чем это должно быть.

4b9b3361

Ответ 1

Ты ПОЧТИ! Проблема в том, что do.call ожидает, что ваши аргументы будут находиться в именованном объекте list. Вы поместили их в список, но как строки символов, но не назвали элементы списка.

Я думаю, что это должно сработать:

args.list <- c(plot.list, 2,2)
names(args.list) <- c("x", "y", "z", "nrow", "ncol")

как указал Бен и Джошуа в комментариях, я мог назначить имена, когда создал список:

args.list <- c(plot.list,list(nrow=2,ncol=2))

или

args.list <- list(x=x, y=y, z=x, nrow=2, ncol=2)

Ответ 2

Попробуйте это,

require(ggplot2)
require(gridExtra)
plots <- lapply(1:11, function(.x) qplot(1:10,rnorm(10), main=paste("plot",.x)))

params <- list(nrow=2, ncol=2)

n <- with(params, nrow*ncol)
## add one page if division is not complete
pages <- length(plots) %/% n + as.logical(length(plots) %% n)

groups <- split(seq_along(plots), 
  gl(pages, n, length(plots)))

pl <-
  lapply(names(groups), function(g)
         {
           do.call(arrangeGrob, c(plots[groups[[g]]], params, 
                                  list(main=paste("page", g, "of", pages))))
         })

class(pl) <- c("arrangelist", "ggplot", class(pl))
print.arrangelist = function(x, ...) lapply(x, function(.x) {
  if(dev.interactive()) dev.new() else grid.newpage()
   grid.draw(.x)
   }, ...)

## interactive use; open new devices
pl

## non-interactive use, multipage pdf
ggsave("multipage.pdf", pl)

Ответ 3

Я немного опоздал, но наткнулся на решение в R Graphics Cookbook, которое делает что-то очень похожее, используя пользовательскую функцию под названием multiplot. Возможно, это поможет другим, кто найдет этот вопрос. Я также добавляю ответ, поскольку решение может быть более новым, чем другие ответы на этот вопрос.

Несколько графиков на одной странице (ggplot2)

Здесь текущая функция, хотя, пожалуйста, используйте приведенную выше ссылку, поскольку автор отметил, что она была обновлена ​​для ggplot2 0.9.3, которая указывает, что она может снова измениться.

# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols:   Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  require(grid)

  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)

  numPlots = length(plots)

  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                    ncol = cols, nrow = ceiling(numPlots/cols))
  }

 if (numPlots==1) {
    print(plots[[1]])

  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))

      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}

Создаются сюжетные объекты:

p1 <- ggplot(...)
p2 <- ggplot(...)
# etc.

И затем передает их в multiplot:

multiplot(p1, p2, ..., cols = n)