【问题标题】:Placing legend above main title在主标题上方放置图例
【发布时间】:2018-10-12 17:37:13
【问题描述】:

在使用theme(legend.position = "top") 时,将图例放在ggplot2 的主标题上方似乎是以前版本的ggplot 中的默认(和不需要的)结果:ggplot legend at top but below title?

在当前版本的ggplot2 中,当设置theme(legend.position = "top") 时,图例将自己置于情节和主标题之间。一个小例子:

d <- data.frame(x = 1:2, y = 1:2, z = c("a", "b")) 
ggplot(d, aes(x = x, y = y, fill = z)) + 
  geom_col() +
  ggtitle("My title") +
  theme(legend.position = "top") 

如何将图例放在主标题上方?

【问题讨论】:

    标签: r ggplot2 legend


    【解决方案1】:
    library(ggplot2)
    
    ggplot(mtcars, aes(wt, mpg, color=cyl)) +
      geom_point() +
      labs(title = "Hey") +
      theme(plot.margin = margin(t=4,1,1,1, "lines")) +
      theme(legend.direction = "horizontal") +
      theme(legend.position = c(0.5, 1.2))
    

    还有其他方法,但这是我想到的最简单的方法。

    【讨论】:

      【解决方案2】:

      与调整边距相比,这需要做更多的工作,但应该可以更好地控制放置和大小。我正在使用来自cowplot:get_legend 的函数从图中提取图例,并使用plot_grid 来创建这两个ggplot 元素的网格。

      在创建带有图例的情节p 后,cowplot::get_legend(p) 然后创建一个只是图例的ggplot 对象。使用plot_grid 重新定位它们,同时添加一个从p 中删除图例的theme 调用。您可能需要调整高度,或许还需要调整边距。

      library(ggplot2)
      
      p <- ggplot(d, aes(x = x, y = y, fill = z)) + 
        geom_col() +
        ggtitle("My title") +
        theme(legend.position = "bottom") 
      
      legend <- cowplot::get_legend(p)
      
      cowplot::plot_grid(
        legend,
        p + theme(legend.position = "none"),
        ncol = 1, rel_heights = c(0.1, 1)
      )
      

      reprex package (v0.2.1) 于 2018 年 10 月 12 日创建

      【讨论】:

        【解决方案3】:

        或者我们可以创建一个假构面并将情节标题放入其中。之后进行一些调整以删除条形刻面并减少图例边距

        library(ggplot2)
        
        d <- data.frame(x = 1:2, y = 1:2, z = c("a", "b")) 
        d$Title <- "My title\n"
        
        # default legend key text
        p1 <- ggplot(d, aes(x = x, y = y, fill = z)) + 
          geom_col() +
          facet_grid(~ Title) +
          theme(strip.text.x = element_text(hjust = 0, vjust = 1,
                                            size = 14, face = 'bold'),
                strip.background = element_blank()) +
          theme(legend.margin = margin(5, 0, 0, 0),
                legend.box.margin = margin(0, 0, -10, 0)) +
          theme(legend.position = "top") +
          NULL
        
        # legend key text at the bottom
        p2 <- ggplot(d, aes(x = x, y = y, fill = z)) + 
          geom_col() +
          facet_grid(~ Title) +
          theme(strip.text.x = element_text(hjust = 0, vjust = 1,
                                            size = 14, face = 'bold'),
                strip.background = element_blank()) +
          theme(legend.margin = margin(5, 0, 0, 0),
                legend.box.margin = margin(0, 0, -10, 0)) +
          guides(fill = guide_legend(label.position = "bottom",
                                     title.position = "left", title.vjust = 1)) +
          theme(legend.position = "top") +
          NULL
        
        
        library(patchwork)
        p1 | p2
        

        reprex package (v0.2.1.9000) 于 2018 年 10 月 12 日创建

        【讨论】:

        • 您能解释一下+ NULL 部分吗?以前没见过
        • NULL 在那里,您可以在不破坏代码的情况下评论它上面的行(每个ggplot 调用末尾的+ 有时可能是一个问题)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-10
        • 2022-12-01
        • 2015-12-15
        • 1970-01-01
        • 1970-01-01
        • 2016-12-03
        相关资源
        最近更新 更多