tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.52k stars 2.02k forks source link

Issue with scales when drop = F #6056

Closed dominicroye closed 2 months ago

dominicroye commented 2 months ago

When I use scale_color_brewer() with drop = F, the missing category is only displayed with a label but without the color point.

library(tidyverse)

iris <- mutate(iris, Species = factor(Species, c("setosa", "versicolor", "virginica")))

ggplot(iris, aes(Sepal.Width, Petal.Length, color = Species)) +
  geom_point() +
  scale_color_brewer(palette = "Set1", guide = guide_legend(nrow = 1)) +
  theme(legend.position = "bottom")


filter(iris, Species != "setosa") %>%
  ggplot(aes(Sepal.Width, Petal.Length, color = Species)) +
  geom_point() +
  scale_color_brewer(palette = "Set1", guide = guide_legend(nrow = 1), drop = F) +
  theme(legend.position = "bottom")

> sessionInfo()
R version 4.4.1 (2024-06-14 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22631)

Matrix products: default

locale:
[1] LC_COLLATE=Spanish_Spain.utf8  LC_CTYPE=Spanish_Spain.utf8    LC_MONETARY=Spanish_Spain.utf8 LC_NUMERIC=C                  
[5] LC_TIME=Spanish_Spain.utf8    

time zone: Europe/Madrid
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] ggtext_0.1.2       fs_1.6.4           CatastRo_0.4.0     osmdata_0.2.5      data.table_1.15.4  osmextract_0.5.1   here_1.0.1        
 [8] lubridate_1.9.3    forcats_1.0.0      stringr_1.5.1      dplyr_1.1.4        purrr_1.0.2        readr_2.1.5        tidyr_1.3.1       
[15] tibble_3.2.1       ggplot2_3.5.1.9000 tidyverse_2.0.0    sf_1.0-16          mapSpain_0.9.1     r5r_2.0            rJava_1.0-11      

loaded via a namespace (and not attached):
 [1] tidyselect_1.2.1   fastmap_1.2.0      blogdown_1.19      promises_1.3.0     reprex_2.1.1       digest_0.6.36      timechange_0.3.0  
 [8] mime_0.12          lifecycle_1.0.4    ellipsis_0.3.2     processx_3.8.4     magrittr_2.0.3     compiler_4.4.1     rlang_1.1.4       
[15] tools_4.4.1        utf8_1.2.4         yaml_2.3.9         knitr_1.48         htmlwidgets_1.6.4  pkgbuild_1.4.4     classInt_0.4-10   
[22] curl_5.2.1         xml2_1.3.6         pkgload_1.4.0      KernSmooth_2.23-24 miniUI_0.1.1.1     withr_3.0.0        grid_4.4.1        
[29] fansi_1.0.6        urlchecker_1.0.1   profvis_0.3.8      xtable_1.8-4       e1071_1.7-14       colorspace_2.1-0   scales_1.3.0      
[36] cli_3.6.3          rmarkdown_2.27     generics_0.1.3     remotes_2.5.0      rstudioapi_0.16.0  tzdb_0.4.0         sessioninfo_1.2.2 
[43] DBI_1.2.3          cachem_1.1.0       proxy_0.4-27       s2_1.1.7           vctrs_0.6.5        devtools_2.4.5     callr_3.7.6       
[50] hms_1.1.3          clipr_0.8.0        units_0.8-5        glue_1.7.0         ps_1.7.7           stringi_1.8.4      gtable_0.3.5      
[57] later_1.3.2        munsell_0.5.1      pillar_1.9.0       rappdirs_0.3.3     htmltools_0.5.8.1  R6_2.5.1           httr2_1.0.2       
[64] wk_0.9.2           rprojroot_2.0.4    evaluate_0.24.0    shiny_1.8.1.1      gridtext_0.1.5     memoise_2.0.1      httpuv_1.6.15     
[71] class_7.3-22       Rcpp_1.0.12        xfun_0.46          usethis_2.2.3      pkgconfig_2.0.3  

Created on 2024-08-23 with reprex v2.1.1

teunbrand commented 2 months ago

Thanks for the report, we appreciate the effort into preparing the code in a reproducible format! This is a duplicate of https://github.com/tidyverse/ggplot2/issues/5728 and we don't have the intent to change this behaviour.