grid_arrange_shared_legend 列表中的多面板 ggplot

multipanel ggplot from a list with grid_arrange_shared_legend

我试图通过允许用户选择要绘制的面板数量,使我的多面板 ggplotShinyApp 中具有共享图例更加灵活。

目前我的代码像这样一次写出面板对象 1。

grid_arrange_shared_legend(p1,p2,p3,p4, ncol = 4, nrow = 1)

我不完全理解为什么我找不到一种方法来告诉 grid_arrange_shared_legend 接受绘图列表(列表对象)而不是一个接一个地写出来。 它抛出这个错误:

Error in UseMethod("ggplot_build") : no applicable method for 'ggplot_build' applied to an object of class "NULL"

library(ggplot2)
library(lemon)
plotlist <- list()
dsamp <- diamonds[sample(nrow(diamonds), 300), ]
plotlist$p1 <- qplot(carat, price, data = dsamp, colour = clarity)
plotlist$p2 <- qplot(cut, price, data = dsamp, colour = clarity)
plotlist$p3 <- qplot(color, price, data = dsamp, colour = clarity)
plotlist$p4 <- qplot(depth, price, data = dsamp, colour = clarity)
grid_arrange_shared_legend(plotlist, ncol = 4, nrow = 1)

使用列表,列表中有多少地块并不重要,我会根据列表的长度计算 ncol 或 nrow...

我的自制版本的函数通过添加 plotlist 参数并添加 plots <- c(list(...), plotlist) 行作为代码的第一行来实现。这样它既可以获取绘图列表也可以获取单独的绘图对象。

grid_arrange_shared_legend_plotlist <- function(..., 
                                                plotlist=NULL,
                                                ncol = length(list(...)),
                                                nrow = NULL,
                                                position = c("bottom", "right")) {

  plots <- c(list(...), plotlist)

  if (is.null(nrow)) nrow = ceiling(length(plots)/ncol)

  position <- match.arg(position)
  g <- ggplotGrob(plots[[1]] + theme(legend.position = position))$grobs
  legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
  lheight <- sum(legend$height)
  lwidth <- sum(legend$width)
  gl <- lapply(plots, function(x) x + theme(legend.position="none"))
  gl <- c(gl, ncol = ncol, nrow = nrow)

  combined <- switch(position,
                     "bottom" = arrangeGrob(do.call(arrangeGrob, gl),
                                            legend,
                                            ncol = 1,
                                            heights = unit.c(unit(1, "npc") - lheight, lheight)),
                     "right" = arrangeGrob(do.call(arrangeGrob, gl),
                                           legend,
                                           ncol = 2,
                                           widths = unit.c(unit(1, "npc") - lwidth, lwidth)))

  grid.newpage()
  grid.draw(combined)

  # return gtable invisibly
  invisible(combined)
}

使用你的例子:

library(gridExtra)
library(grid)
library(ggplot2)
plots <- list()
dsamp <- diamonds[sample(nrow(diamonds), 300), ]
plots$p1 <- qplot(carat, price, data = dsamp, colour = clarity)
plots$p2 <- qplot(cut, price, data = dsamp, colour = clarity)
plots$p3 <- qplot(color, price, data = dsamp, colour = clarity)
plots$p4 <- qplot(depth, price, data = dsamp, colour = clarity)

grid_arrange_shared_legend_plotlist(plotlist = plots, ncol = 4)

难看的文字串粘贴解决方法:

由于提供的答案似乎不起作用或不合适(重建一组与我已经从大量代码中获得的绘图对象列表完全不同的绘图,我尝试了一下 eval(parse(text = ....)paste0 动态生成一个文本字符串,最终成为完全写出的代码(有效)而无需实际写出

nplots = 4
nrow = 2
ncol = ceiling(nplots/nrow)
eval(parse( text = paste0("grid_arrange_shared_legend(", paste0("plotlist", "[[", c(1:nplots), "]]", sep = '', collapse = ','), ",ncol =", ncol, ",nrow =", nrow, ", position = 'right',  top=grid::textGrob('My title', gp=grid::gpar(fontsize=18)))", sep = '')))

产生:

[1] "grid_arrange_shared_legend(plotlist[[1]],plotlist[[2]],plotlist[[3]],plotlist[[4]],ncol =2,nrow =2, position = 'right', top=grid::textGrob('My title', gp=grid::gpar(fontsize=18)))"