代码之家  ›  专栏  ›  技术社区  ›  tassones

如何使图例颜色栏与我的刻面包裹绘图面板的高度相同?

  •  0
  • tassones  · 技术社区  · 7 月前

    使用时,如何调整图例颜色栏的高度以匹配绘图的高度 facet_wrap() ? This related SO question 为单个绘图提供了一个很好的解决方案,但当存在多个绘图时,颜色栏会延伸到刻面环绕标题和轴文本的一半。我希望颜色条仅从刻面环绕标题的底部延伸到轴文本的顶部。

    制作底图

    (注意小色带高度)

    library(tidyverse)
    library(ggcorrplot)
    library(reshape2)
    
    iris %>%
      group_by(Species) %>%
      summarise(correlation = list(cor(across(where(is.numeric))))) %>%
      ungroup() %>%
      mutate(correlation_df = map(correlation, ~ melt(.x, varnames = c("Var1", "Var2")))) %>%
      select(Species, correlation_df) %>%
      unnest(correlation_df) %>%
      ggplot(aes(x = Var1, y = Var2, fill = value)) +
      geom_tile() +
      scale_fill_gradient2(limit = c(-1, 1)) +
      labs(x = NULL,
           y = NULL,
           fill = NULL) +
      theme_bw() +
      facet_wrap(~Species)
    

    使用类似SO问题的答案

    (注意,颜色条延伸到标题和轴文本区域)

    
    make_fullsize <- function() structure("", class = "fullsizebar")
    
    ggplot_add.fullsizebar <- function(obj, g, name = "fullsizebar") {
      h <- ggplotGrob(g)$heights
      panel <- which(grid::unitType(h) == "null")
      panel_height <- unit(1, "npc") - sum(h[-panel])
      
      g + 
        guides(fill = guide_colorbar(barheight = panel_height,
                                     title.position = "right")) +
        theme(legend.title = element_text(angle = -90, hjust = 0.5))
    }
    
    iris %>%
      group_by(Species) %>%
      summarise(correlation = list(cor(across(where(is.numeric))))) %>%
      ungroup() %>%
      mutate(correlation_df = map(correlation, ~ melt(.x, varnames = c("Var1", "Var2")))) %>%
      select(Species, correlation_df) %>%
      unnest(correlation_df) %>%
      ggplot(aes(x = Var1, y = Var2, fill = value)) +
      geom_tile() +
      scale_fill_gradient2(limit = c(-1, 1)) +
      labs(x = NULL,
           y = NULL,
           fill = NULL) +
      theme_bw() +
      coord_cartesian(expand = FALSE) +
      make_fullsize() +
      facet_wrap(~Species)
    

    创建于2024年12月30日 reprex v2.1.1

    我一直在尝试改变 panel_height 例如, panel_height <- unit(0.95... 这提供了一个接近但不精确的答案。在放大/缩小绘图时,这个答案也不能很好地缩放。

    1 回复  |  直到 7 月前
        1
  •  2
  •   Eliot Dixon tmfmnk    7 月前

    根据艾伦·卡梅伦的评论编辑:

    library(tidyverse)
    library(ggcorrplot)
    library(reshape2)
    
    iris %>%
      group_by(Species) %>%
      summarise(correlation = list(cor(across(where(is.numeric))))) %>%
      ungroup() %>%
      mutate(correlation_df = map(correlation, ~ melt(.x, varnames = c("Var1", "Var2")))) %>%
      select(Species, correlation_df) %>%
      unnest(correlation_df) %>%
      ggplot(aes(x = Var1, y = Var2, fill = value)) +
      geom_tile() +
      scale_fill_gradient2(limit = c(-1, 1)) +
      labs(x = NULL,
           y = NULL,
           fill = NULL) +
      theme_bw() +
      facet_wrap(~Species) +
      theme(legend.key.height = unit(1, "null"),
            legend.margin = margin(0, 0, 0, 0))
    

    通过在 legend.key.height 参数中,参考线不仅垂直适合绘图,而且如果更改绘图的纵横比,还会拉伸。根据 this tidyverse.org文章,此功能仍处于试验阶段。

    (根据艾伦·卡梅伦的评论编辑)然后,您可以添加 legend.margin = margin(0, 0, 0, 0) 在主题内,传说与情节的顶部和底部垂直对齐。

    输出: enter image description here