【问题标题】:Merging two y-axes titles in patchwork在拼凑中合并两个 y 轴标题
【发布时间】:2020-12-14 15:30:33
【问题描述】:

关于如何将两个相同的 y 轴标题“合并”为一个,然后将此 y 轴标题放在情节之间的任何想法?我通过使用plot_layout(guides = "collect") 成功合并了图例,但我似乎找不到任何类似的轴。在这种情况下,我会将名为 disp_disp_disp 的两个轴标题合并为一个。

mtcars

library(ggplot2)
library(patchwork)

p1 <- ggplot(mtcars) + 
  geom_point(aes(mpg, disp)) + 
  labs(x = "mpg", y = "disp_disp_disp_disp_disp")

p2 <- ggplot(mtcars) + 
  geom_boxplot(aes(gear, disp, group = gear)) + 
  labs(x = "gear", y = "disp_disp_disp_disp_disp")

p3 <- ggplot(mtcars) + 
  geom_point(aes(hp, wt, colour = mpg)) + 
  ggtitle('Plot 3')

p1 / (p2 | p3)

【问题讨论】:

    标签: r ggplot2 patchwork


    【解决方案1】:

    我想在绘图之前去掉 y 轴标题会稍微容易一些,然后在绘图后重新绘制它:

    library(ggplot2)
    library(patchwork)
    
    p1 <- ggplot(mtcars) + 
      geom_point(aes(mpg, disp)) + 
      labs(x = "mpg", y = "disp_disp_disp_disp_disp")
    
    p2 <- ggplot(mtcars) + 
      geom_boxplot(aes(gear, disp, group = gear)) + 
      labs(x = "gear", y = "disp_disp_disp_disp_disp")
    
    p3 <- ggplot(mtcars) + 
      geom_point(aes(hp, wt, colour = mpg)) + 
      ggtitle('Plot 3')
    
    ylab <- p1$labels$y
    p1$labels$y <- p2$labels$y <- " "
    
    p1 / (p2 | p3)
    grid::grid.draw(grid::textGrob(ylab, x = 0.02, rot = 90))
    

    如果您想避免完全被 grobs 弄脏,另一种选择是指定一个纯文本 ggplot 并将其添加为您的轴文本:

    p4 <- ggplot(data.frame(l = p1$labels$y, x = 1, y = 1)) +
          geom_text(aes(x, y, label = l), angle = 90) + 
          theme_void() +
          coord_cartesian(clip = "off")
    
    p1$labels$y <- p2$labels$y <- " "
    
    p4 + (p1 / (p2 | p3)) + plot_layout(widths = c(1, 25))
    

    这在调整大小时也表现得更好。

    【讨论】:

    • 最后一个,p4,很棒
    【解决方案2】:

    我能想到的唯一方法是在 gtable 级别破解它,但我也很高兴学习更方便的方法。这是gtable方法:

    library(ggplot2)
    library(patchwork)
    library(grid)
    
    p1 <- ggplot(mtcars) + 
      geom_point(aes(mpg, disp)) + 
      labs(x = "mpg", y = "disp_disp_disp_disp_disp")
    
    p2 <- ggplot(mtcars) + 
      geom_boxplot(aes(gear, disp, group = gear)) + 
      labs(x = "gear", y = "disp_disp_disp_disp_disp")
    
    p3 <- ggplot(mtcars) + 
      geom_point(aes(hp, wt, colour = mpg)) + 
      ggtitle('Plot 3')
    
    p123 <- p1 / (p2 | p3)
    
    # Convert to gtable
    gt <- patchworkGrob(p123)
    
    # Stretching one y-axis title
    is_yaxis_title <- which(gt$layout$name == "ylab-l")
    # Find new bottom position based on gtable::gtable_show_layout(gt)
    gt$layout$b[is_yaxis_title] <- gt$layout$b[is_yaxis_title] + 18
    
    # Deleting other y-axis title in sub-patchwork
    is_patchwork <- which(gt$layout$name == "patchwork-table")
    pw <- gt$grobs[[is_patchwork]]
    pw <- gtable::gtable_filter(pw, "ylab-l", invert = TRUE)
    
    # Set background to transparent
    pw$grobs[[which(pw$layout$name == "background")[1]]]$gp$fill <- NA
    
    # Putting sub-patchwork back into main patchwork
    gt$grobs[[is_patchwork]] <- pw
    
    # Render
    grid.newpage(); grid.draw(gt)
    

    reprex package (v0.3.0) 于 2020 年 12 月 14 日创建

    【讨论】:

      猜你喜欢
      • 2017-03-28
      • 2020-05-17
      • 1970-01-01
      • 2021-08-19
      • 2020-11-27
      • 2022-01-19
      • 2019-07-05
      • 2021-11-17
      • 1970-01-01
      相关资源
      最近更新 更多