在几个ggplot2图表之间共享图例时控制绘图布局

时间:2015-10-11 14:49:50

标签: r ggplot2

这是一个从ggplot2对象中提取图例并在grid.arrange()中将其用作自己的对象的函数:

https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs

然而,我真的在努力:我只想要一行中的情节,但显然情节布局是不可改变的。我相信

grid_arrange_shared_legend <- function(...) {
    plots <- list(...)
    g <- ggplotGrob(plots[[1]] + theme(legend.position="bottom"))$grobs
    legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
    lheight <- sum(legend$height)
    grid.arrange(
        do.call(arrangeGrob, lapply(plots, function(x)
            x + theme(legend.position="none"))),
        legend,
        ncol = 1,
        heights = unit.c(unit(1, "npc") - lheight, lheight))
}

需要嵌套的grid.arrange命令,因为ncol=1参数与图例有关。有没有办法做到这一点?

2 个答案:

答案 0 :(得分:3)

您可以将nrow = 1作为列表提供给do.call

do.call(arrangeGrob, c(lapply(plots, function(x)
  x + theme(legend.position="none")), list(nrow = 1)))

我根据您提供的链接调整了示例:

library(ggplot2)
library(gridExtra)

grid_arrange_shared_legend <- function(...) {
  plots <- list(...)
  g <- ggplotGrob(plots[[1]] + theme(legend.position="bottom"))$grobs
  legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
  lheight <- sum(legend$height)
  grid.arrange(
    do.call(arrangeGrob, c(lapply(plots, function(x)
      x + theme(legend.position="none")), list(nrow = 1))),
    legend,
    ncol = 1,
    heights = grid::unit.c(unit(1, "npc") - lheight, lheight))
}

dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
p1 <- qplot(carat, price, data=dsamp, colour=clarity)
p2 <- qplot(cut, price, data=dsamp, colour=clarity)
p3 <- qplot(color, price, data=dsamp, colour=clarity)
p4 <- qplot(depth, price, data=dsamp, colour=clarity)
grid_arrange_shared_legend(p1, p2, p3, p4)

答案 1 :(得分:2)

这是一个较短的选择,

grid_arrange_shared_legend <- function(..., layout = rbind(c(1,2,3,4), 
                                                           c(5,5,5,5))) {
  plots <- list(...)
  g <- ggplotGrob(plots[[1]] + theme(legend.position="bottom"))$grobs
  legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
  lheight <- sum(legend$height)
  gl <- lapply(plots, function(x) x + theme(legend.position="none"))
  grid.arrange(grobs = c(gl, list(legend)), layout_matrix = layout,
    heights = grid::unit.c(unit(1, "npc") - lheight, lheight))
}