【问题标题】:ggplot2 grid_arrange_shared_legend share axis labelsggplot2 grid_arrange_shared_legend 共享轴标签
【发布时间】:2017-09-13 15:12:24
【问题描述】:

我正在使用 grid_arrange_shared_legend 将具有相同图例的图组合成具有一个图例的图。有没有办法稍微编辑函数,使其也共享 x 和 y 轴标签?例如,如果我制作了 3 个图表并想要使用 grid_arrange_shared_legend 制作的这些图表的 3x1 网格,并且想要最底部的共享 x 轴标签和左侧的共享 y 轴标签,我该怎么做? 这是我用于该功能的代码:

grid_arrange_shared_legend <- function(..., ncol=1, mytitle="mytitle") {
  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(ncol=ncol, lapply(plots, function(x)
      x + theme(legend.position="none")))),
    legend,
    ncol = 1,
    heights = unit.c(unit(.95, "npc") - lheight, lheight),
    top=textGrob(mytitle,  gp = gpar(fontsize=16)))

}

【问题讨论】:

  • 为什么不使用?facet_grid()
  • 您可以取消 x/y 轴标题,然后在 grid.arrange o 中使用 bottomleft 参数,或者按照建议使用 faceting ^^

标签: r ggplot2 grob


【解决方案1】:

我遇到了同样的问题。

我的解决方案是为每个绘图添加一个空白的 x 轴标题(以节省空间来添加轴标题):

p1 <- p1 + xlab(" ")

然后创建了具有共享图例的图:

grid_arrange_shared_legend(p1, p2, p3, position = "right")

然后我添加了新文本作为新的轴标题:

grid.text("My title", y= 0.02, x=0.43)

【讨论】:

    猜你喜欢
    • 2021-02-21
    • 1970-01-01
    • 2022-01-25
    • 2018-01-31
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多