0

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

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

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

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

UseMethod(“ggplot_build”)中的错误:没有适用于“ggplot_build”的方法应用于“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 ......

4

2 回答 2

1

我的自制版本的函数通过添加一个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)

结果图

于 2019-04-01T20:05:21.980 回答
0

丑陋的文本字符串粘贴解决方案:

由于提供的答案似乎不起作用,或者不合适(重建一组完全不同的绘图,而不是我已经从大量代码中获得的绘图对象列表,我玩了一下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('我的标题', gp=grid::gpar(fontsize=18)))"

于 2019-04-01T21:16:44.407 回答