首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >标签ggplot patchwork

标签ggplot patchwork
EN

Stack Overflow用户
提问于 2021-06-01 16:23:19
回答 1查看 173关注 0票数 2

我试着把9个地块放在一个有一些独特标签和一些常见标签的地方。在另一篇文章中,我被告知使用patchwork而不是grid.arrange (因为我对图例有问题),但使用patch work我无法放置所有标签。

下面是这些图的代码:

代码语言:javascript
复制
library(ggplot2)

df <- data.frame(
  index = 1:21,
  corr = c(0.10470688, 0.10827255, 0.12322448, 0.11887717, 0.12719741, 0.12635607, 0.13427974,
           0.13539245, 0.13636687, 0.13834174, 0.13864013, 0.13816236, 0.13640052, 0.13775515,
           0.13563827, 0.13968726, 0.12499506, 0.11836173, 0.11097081, 0.09829338, 0.10470688),
  group = c(rep("Group A", 14), rep("Group B", 7))
)

p1 <- ggplot(df) +
  aes(x = index, y = corr, shape = group) +
  geom_point(size = 2) +
  theme_light()
library(patchwork)
p1 + p1 + p1 + p1 + p1 + p1 + p1 + p1 + p1 +
  plot_layout(ncol = 3, nrow = 3, guides = "collect")

但我不知道该如何贴上具体的标签。在这里您可以看到我是如何使用grid.arrange做到这一点的:

代码语言:javascript
复制
library(ggplot2)
library(gridExtra)
library(grid)
library(lattice)

t <- textGrob("Proportion of S1>S2 on Variable1")

lay <- rbind(c(1,2,3),
             c(1,2,3),
             c(1,2,3),
             c(1,2,3),
             c(1,2,3),
             c(1,2,3),
             c(1,2,3),
             c(4,4,4),
             c(5,6,7),
             c(5,6,7),
             c(5,6,7),
             c(5,6,7),
             c(5,6,7),
             c(5,6,7),
             c(5,6,7),
             c(8,8,8),
             c(9,10,11),
             c(9,10,11),
             c(9,10,11),
             c(9,10,11),
             c(9,10,11),
             c(9,10,11),
             c(9,10,11),
             c(12,12,12))

grid.arrange (arrangeGrob (p1, top="High w", left="High p correlation")
,arrangeGrob(p1,top="Medium w correlation")
,arrangeGrob(p1,top="Low w correlation")
,arrangeGrob(t)
,arrangeGrob(p1, left="Medium p correlation")
,arrangeGrob(p1)
,arrangeGrob(p1)
,t
,arrangeGrob(p1, left="Low p correlation")
,arrangeGrob(p1)
,arrangeGrob(p1)
,t
, ncol=3,nrow=24,top="Title", layout_matrix=lay)

但我需要做它与pachwork,以便有一个共享的传奇,而不是9个传奇,每个情节一个。

有没有办法像我在grid.arrange中那样在patchwork中添加标签?

我见过grind.arrange的这个函数来提取共享的图例,但我无法在我的示例中使用它:

代码语言:javascript
复制
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))
}

它在这里起作用:

代码语言:javascript
复制
grid_arrange_shared_legend(p1, p1, p1, p1)

但我不能让它在这里工作:

代码语言:javascript
复制
grid.arrange (arrangeGrob (p1, top="High w", left="High p correlation")
,arrangeGrob(p1,top="Medium w correlation")
,arrangeGrob(p1,top="Low w correlation")
,arrangeGrob(t)
,arrangeGrob(p1, left="Medium p correlation")
,arrangeGrob(p1)
,arrangeGrob(p1)
,t
,arrangeGrob(p1, left="Low p correlation")
,arrangeGrob(p1)
,arrangeGrob(p1)
,t
, ncol=3,nrow=24,top="Title", layout_matrix=lay)

有什么想法吗?提前感谢你,非常感谢你在这个论坛上的帮助和支持。

带着所有美好的愿望,

EN

回答 1

Stack Overflow用户

发布于 2021-06-01 16:48:20

在这里你可以找到不同的策略(不同的函数)来解决你的任务:https://github.com/thomasp85/patchwork/issues/43

  • 为例,使用以下提供的函数向地块添加全局标签:

代码语言:javascript
复制
add_global_label <- function(pwobj, Xlab = NULL, Ylab = NULL, Xgap = 0.03, Ygap = 0.03, ...) {
  ylabgrob <- patchwork::plot_spacer()
  if (!is.null(Ylab)) {
    ylabgrob <- ggplot() +
      geom_text(aes(x = .5, y = .5), label = Ylab, angle = 90, ...) +
      theme_void()
  }
  if (!is.null(Xlab)) {
    xlabgrob <- ggplot() +
      geom_text(aes(x = .5, y = .5), label = Xlab, ...) +
      theme_void()
  }
  if (!is.null(Ylab) & is.null(Xlab)) {
    return((ylabgrob + patchworkGrob(pwobj)) + 
             patchwork::plot_layout(widths = 100 * c(Ygap, 1 - Ygap)))
  }
  if (is.null(Ylab) & !is.null(Xlab)) {
    return((ylabgrob + pwobj) + 
             (xlabgrob) +
             patchwork::plot_layout(heights = 100 * c(1 - Xgap, Xgap),
                                    widths = c(0, 100),
                                    design = "
                                   AB
                                   CC
                                   "
             ))
  }
  if (!is.null(Ylab) & !is.null(Xlab)) {
    return((ylabgrob + pwobj) + 
             (xlabgrob) +
             patchwork::plot_layout(heights = 100 * c(1 - Xgap, Xgap),
                                    widths = 100 * c(Ygap, 1 - Ygap),
                                    design = "
                                   AB
                                   CC
                                   "
             ))
  }
  return(pwobj)
}

,然后使用:

代码语言:javascript
复制
require(magrittr)
(p1 + p1 + p1 + p1 + p1 + p1 + p1 + p1 + p1) %>%
  add_global_label(Ylab = "Global Y title",
                   #       size = 8,
                   #      Ygap = 0.04
  ) + plot_layout(guides = "collect")

结果为:的

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67785239

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档