【问题标题】:ggplot legend order with multiple geoms具有多个几何图形的ggplot图例顺序
【发布时间】:2020-06-24 15:35:53
【问题描述】:
library(tidyverse)
df <- tibble(col1 = as.Date(c("2020-01-01", "2020-01-01", "2020-01-01", 
                              "2020-02-01", "2020-02-01", "2020-02-01")),
             col2 = factor(rep(c("A", "B", "C"), 2), levels = c("C", "A", "B")),
             col3 = c(8, 3, 2, 9, 1, 5))

df
#> # A tibble: 6 x 3
#>   col1       col2   col3
#>   <date>     <fct> <dbl>
#> 1 2020-01-01 A         8
#> 2 2020-01-01 B         3
#> 3 2020-01-01 C         2
#> 4 2020-02-01 A         9
#> 5 2020-02-01 B         1
#> 6 2020-02-01 C         5

levels(df$col2)
#> [1] "C" "A" "B"

我已经创建了数据框dfcol2,如上所示。我认为这种因素的顺序决定了 ggplot 图例条目的默认顺序,但事实并非如此,如下所示。

ggplot(df, aes(col1, col3, fill = col2)) + 
  geom_col(data = df %>% filter(col2 != "C"), 
           position = "dodge", 
           color = "black") + 
  geom_col(data = df %>% filter(col2 == "C"), 
           width = 15,
           color = "black") +
  guides(fill = guide_legend(title = "Legend"))

我将推测 col2 级别排序 (#> [1] "C" "A" "B") 在 ggplot 的图例排序中不受尊重,因为有多个我的代码中的几何图形。它是否正确?如何让图例条目按以下顺序出现?

  1. C
  2. 一个
  3. B

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以在色标上设置限制:

    library(tidyverse)
    df <- tibble(col1 = as.Date(c("2020-01-01", "2020-01-01", "2020-01-01", 
                                  "2020-02-01", "2020-02-01", "2020-02-01")),
                 col2 = factor(rep(c("A", "B", "C"), 2), levels = c("C", "A", "B")),
                 col3 = c(8, 3, 2, 9, 1, 5))
    
    ggplot(df, aes(col1, col3, fill = col2)) + 
      geom_col(data = df %>% filter(col2 != "C"), 
               position = "dodge", 
               color = "black") + 
      geom_col(data = df %>% filter(col2 == "C"), 
               width = 15,
               color = "black") +
      scale_fill_discrete(name = "Legend",
                          limits = c("C", "A", "B"))
    

    或者,您可以在比例中设置drop = FALSE,这样它就不会在层之间丢弃未使用的级别:

    ggplot(df, aes(col1, col3, fill = col2)) + 
      geom_col(data = df %>% filter(col2 != "C"), 
               position = "dodge", 
               color = "black") + 
      geom_col(data = df %>% filter(col2 == "C"), 
               width = 15,
               color = "black") +
      scale_fill_discrete(name = "Legend", drop = FALSE)
    

    【讨论】:

      【解决方案2】:

      只需设置 scale_fill_discrete 中断参数...

      library(tidyverse)
      
      df <- tibble(col1 = as.Date(c("2020-01-01", "2020-01-01", "2020-01-01", 
                                    "2020-02-01", "2020-02-01", "2020-02-01")),
                   col2 = factor(rep(c("A", "B", "C"), 2), levels = c("C", "A", "B")),
                   col3 = c(8, 3, 2, 9, 1, 5))
      
      
      ggplot(df, aes(col1, col3, fill = col2)) + 
        geom_col(data = df %>% filter(col2 != "C"), 
                 position = "dodge", 
                 color = "black") + 
        geom_col(data = df %>% filter(col2 == "C"), 
                 width = 15,
                 color = "black") +
        scale_fill_discrete(breaks = c("C", "A", "B"))+
        guides(fill = guide_legend(title = "Legend"))
      

      reprex package (v0.3.0) 于 2020 年 6 月 24 日创建

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-08
        相关资源
        最近更新 更多