Skip to content

Unused levels persist in legends from version 3.3.4 #4545

Closed
@wjakethompson

Description

@wjakethompson

It appears a bug was introduced in version 3.3.4. When using scale_fill_manual(), unused levels persist in the legend. Here is some example data.

library(tidyverse)

set.seed(123)
dat <- tibble(var = paste("Group", 1:9),
              val = runif(9),
              grp = sample(LETTERS[1:4], 9, replace = TRUE))

When all levels are present, things work as expected:

dat %>%
  ggplot(aes(x = val, y = var)) +
  geom_col(aes(fill = grp)) +
  scale_fill_manual(values = c("A" = "red", "B" = "blue", "C" = "orange",
                               "D" = "black"))

But when grp = "C" is no longer in the data, that value still persists in the legend.

dat %>%
  filter(grp != "C") %>%
  ggplot(aes(x = val, y = var)) +
  geom_col(aes(fill = grp)) +
  scale_fill_manual(values = c("A" = "red", "B" = "blue", "C" = "orange",
                               "D" = "black"))

From this thread on StackOverflow, it appears that this worked as expected in version 3.3.3. I have tested version 3.3.4 and the development version from GitHub, and the unused level persists in the legend for both.

Created on 2021-07-06 by the reprex package (v2.0.0)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.1.0 (2021-05-18)
#>  os       macOS Big Sur 10.16         
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/Chicago             
#>  date     2021-07-06                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date       lib source                            
#>  assertthat    0.2.1      2019-03-21 [1] CRAN (R 4.1.0)                    
#>  backports     1.2.1      2020-12-09 [1] standard (@1.2.1)                 
#>  broom         0.7.7      2021-06-13 [1] standard (@0.7.7)                 
#>  cellranger    1.1.0      2016-07-27 [1] standard (@1.1.0)                 
#>  cli           3.0.0      2021-06-30 [1] CRAN (R 4.1.0)                    
#>  colorspace    2.0-2      2021-06-24 [1] CRAN (R 4.1.0)                    
#>  crayon        1.4.1      2021-02-08 [1] CRAN (R 4.1.0)                    
#>  curl          4.3.1      2021-04-30 [1] CRAN (R 4.1.0)                    
#>  DBI           1.1.1      2021-01-15 [1] standard (@1.1.1)                 
#>  dbplyr        2.1.1      2021-04-06 [1] standard (@2.1.1)                 
#>  digest        0.6.27     2020-10-24 [1] CRAN (R 4.1.0)                    
#>  dplyr       * 1.0.6      2021-05-05 [1] standard (@1.0.6)                 
#>  ellipsis      0.3.2      2021-04-29 [1] CRAN (R 4.1.0)                    
#>  evaluate      0.14       2019-05-28 [1] standard (@0.14)                  
#>  fansi         0.5.0      2021-05-25 [1] CRAN (R 4.1.0)                    
#>  farver        2.1.0      2021-02-28 [1] standard (@2.1.0)                 
#>  forcats     * 0.5.1      2021-01-27 [1] standard (@0.5.1)                 
#>  fs            1.5.0      2020-07-31 [1] standard (@1.5.0)                 
#>  generics      0.1.0      2020-10-31 [1] standard (@0.1.0)                 
#>  ggplot2     * 3.3.5.9000 2021-07-07 [1] Github (tidyverse/ggplot2@acaa98e)
#>  glue          1.4.2      2020-08-27 [1] CRAN (R 4.1.0)                    
#>  gtable        0.3.0      2019-03-25 [1] standard (@0.3.0)                 
#>  haven         2.4.1      2021-04-23 [1] standard (@2.4.1)                 
#>  highr         0.9        2021-04-16 [1] standard (@0.9)                   
#>  hms           1.1.0      2021-05-17 [1] CRAN (R 4.1.0)                    
#>  htmltools     0.5.1.1    2021-01-22 [1] standard (@0.5.1.1)               
#>  httr          1.4.2      2020-07-20 [1] standard (@1.4.2)                 
#>  jsonlite      1.7.2      2020-12-09 [1] CRAN (R 4.1.0)                    
#>  knitr         1.33       2021-04-24 [1] standard (@1.33)                  
#>  labeling      0.4.2      2020-10-20 [1] standard (@0.4.2)                 
#>  lifecycle     1.0.0      2021-02-15 [1] CRAN (R 4.1.0)                    
#>  lubridate     1.7.10     2021-02-26 [1] standard (@1.7.10)                
#>  magrittr      2.0.1      2020-11-17 [1] CRAN (R 4.1.0)                    
#>  mime          0.10       2021-02-13 [1] standard (@0.10)                  
#>  modelr        0.1.8      2020-05-19 [1] standard (@0.1.8)                 
#>  munsell       0.5.0      2018-06-12 [1] standard (@0.5.0)                 
#>  pillar        1.6.1      2021-05-16 [1] CRAN (R 4.1.0)                    
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.1.0)                    
#>  purrr       * 0.3.4      2020-04-17 [1] standard (@0.3.4)                 
#>  R6            2.5.0      2020-10-28 [1] CRAN (R 4.1.0)                    
#>  Rcpp          1.0.6      2021-01-15 [1] standard (@1.0.6)                 
#>  readr       * 1.4.0      2020-10-05 [1] standard (@1.4.0)                 
#>  readxl        1.3.1      2019-03-13 [1] standard (@1.3.1)                 
#>  reprex        2.0.0      2021-04-02 [1] standard (@2.0.0)                 
#>  rlang         0.4.11     2021-04-30 [1] CRAN (R 4.1.0)                    
#>  rmarkdown     2.9        2021-06-15 [1] standard (@2.9)                   
#>  rstudioapi    0.13       2020-11-12 [1] standard (@0.13)                  
#>  rvest         1.0.0      2021-03-09 [1] standard (@1.0.0)                 
#>  scales        1.1.1      2020-05-11 [1] standard (@1.1.1)                 
#>  sessioninfo   1.1.1      2018-11-05 [1] standard (@1.1.1)                 
#>  stringi       1.6.2      2021-05-17 [1] CRAN (R 4.1.0)                    
#>  stringr     * 1.4.0      2019-02-10 [1] CRAN (R 4.1.0)                    
#>  styler        1.4.1      2021-03-30 [1] standard (@1.4.1)                 
#>  tibble      * 3.1.2      2021-05-16 [1] CRAN (R 4.1.0)                    
#>  tidyr       * 1.1.3      2021-03-03 [1] standard (@1.1.3)                 
#>  tidyselect    1.1.1      2021-04-30 [1] standard (@1.1.1)                 
#>  tidyverse   * 1.3.1      2021-04-15 [1] standard (@1.3.1)                 
#>  utf8          1.2.1      2021-03-12 [1] CRAN (R 4.1.0)                    
#>  vctrs         0.3.8      2021-04-29 [1] CRAN (R 4.1.0)                    
#>  withr         2.4.2      2021-04-18 [1] CRAN (R 4.1.0)                    
#>  xfun          0.24       2021-06-15 [1] standard (@0.24)                  
#>  xml2          1.3.2      2020-04-23 [1] CRAN (R 4.1.0)                    
#>  yaml          2.2.1      2020-02-01 [1] standard (@2.2.1)                 
#> 
#> [1] /Library/Frameworks/R.framework/Versions/4.1/Resources/library

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions