Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scale_*_discrete(drop = FALSE) changed behaviour/stopped working in version < 3.5.0 #5966

Closed
markus-schaffer opened this issue Jul 1, 2024 · 1 comment

Comments

@markus-schaffer
Copy link

The behaviour of scale_*_discrete(drop = FALSE) stopped working/changed its behaviour for ggplot2 < 3.5.0 (without that I was able to find information about it in the changelog) . The legend does not longer show the colour/symbols for levels without data.

ggplot2 version 3.5.1

library(ggplot2)

df <- data.frame(
  x = 1:5, 
  y = rep(1,5),
  z = factor(c(1,1,3,1,1), levels = c("1","2","3","4","5"))
)
df$z
#> [1] 1 1 3 1 1
#> Levels: 1 2 3 4 5
ggplot(df, aes(x, y, fill = z)) +
  geom_tile()+
  scale_fill_discrete(drop = FALSE)

ggplot(df, aes(x, y, color = z)) +
  geom_point()+
  scale_color_discrete(drop = FALSE)

sessionInfo()
#> R version 4.4.1 (2024-06-14 ucrt)
#> Platform: x86_64-w64-mingw32/x64
#> Running under: Windows 10 x64 (build 19045)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=Danish_Denmark.utf8  LC_CTYPE=Danish_Denmark.utf8   
#> [3] LC_MONETARY=Danish_Denmark.utf8 LC_NUMERIC=C                   
#> [5] LC_TIME=Danish_Denmark.utf8    
#> 
#> time zone: Europe/Copenhagen
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] ggplot2_3.5.1
#> 
#> loaded via a namespace (and not attached):
#>  [1] vctrs_0.6.5       cli_3.6.3         knitr_1.47        rlang_1.1.4      
#>  [5] xfun_0.45         purrr_1.0.2       styler_1.10.3     generics_0.1.3   
#>  [9] labeling_0.4.3    glue_1.7.0        colorspace_2.1-0  htmltools_0.5.8.1
#> [13] fansi_1.0.6       scales_1.3.0      rmarkdown_2.27    R.cache_0.16.0   
#> [17] grid_4.4.1        tibble_3.2.1      munsell_0.5.1     evaluate_0.24.0  
#> [21] fastmap_1.2.0     yaml_2.3.8        lifecycle_1.0.4   compiler_4.4.1   
#> [25] dplyr_1.1.4       fs_1.6.4          pkgconfig_2.0.3   rstudioapi_0.16.0
#> [29] R.oo_1.26.0       farver_2.1.2      R.utils_2.12.3    digest_0.6.35    
#> [33] R6_2.5.1          tidyselect_1.2.1  utf8_1.2.4        reprex_2.1.0     
#> [37] pillar_1.9.0      magrittr_2.0.3    R.methodsS3_1.8.2 tools_4.4.1      
#> [41] withr_3.0.0       gtable_0.3.5

ggplot2 version 3.4.4

library(ggplot2)

df <- data.frame(
  x = 1:5, 
  y = rep(1,5),
  z = factor(c(1,1,3,1,1), levels = c("1","2","3","4","5"))
)
df$z
#> [1] 1 1 3 1 1
#> Levels: 1 2 3 4 5
ggplot(df, aes(x, y, fill = z)) +
  geom_tile()+
  scale_fill_discrete(drop = FALSE)

ggplot(df, aes(x, y, color = z)) +
  geom_point()+
  scale_color_discrete(drop = FALSE)

sessionInfo()
#> R version 4.4.1 (2024-06-14 ucrt)
#> Platform: x86_64-w64-mingw32/x64
#> Running under: Windows 10 x64 (build 19045)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=Danish_Denmark.utf8  LC_CTYPE=Danish_Denmark.utf8   
#> [3] LC_MONETARY=Danish_Denmark.utf8 LC_NUMERIC=C                   
#> [5] LC_TIME=Danish_Denmark.utf8    
#> 
#> time zone: Europe/Copenhagen
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] ggplot2_3.4.4
#> 
#> loaded via a namespace (and not attached):
#>  [1] vctrs_0.6.5       cli_3.6.3         knitr_1.47        rlang_1.1.4      
#>  [5] xfun_0.45         purrr_1.0.2       styler_1.10.3     generics_0.1.3   
#>  [9] labeling_0.4.3    glue_1.7.0        colorspace_2.1-0  htmltools_0.5.8.1
#> [13] fansi_1.0.6       scales_1.3.0      rmarkdown_2.27    R.cache_0.16.0   
#> [17] grid_4.4.1        tibble_3.2.1      munsell_0.5.1     evaluate_0.24.0  
#> [21] fastmap_1.2.0     yaml_2.3.8        lifecycle_1.0.4   compiler_4.4.1   
#> [25] dplyr_1.1.4       fs_1.6.4          pkgconfig_2.0.3   rstudioapi_0.16.0
#> [29] R.oo_1.26.0       farver_2.1.2      R.utils_2.12.3    digest_0.6.35    
#> [33] R6_2.5.1          tidyselect_1.2.1  utf8_1.2.4        reprex_2.1.0     
#> [37] pillar_1.9.0      magrittr_2.0.3    R.methodsS3_1.8.2 tools_4.4.1      
#> [41] withr_3.0.0       gtable_0.3.5

Created on 2024-07-01 with reprex v2.1.0

@teunbrand
Copy link
Collaborator

This issue is a duplicate of #5728.
In short, you'd need to use show.legend = TRUE if you want to display keys for data that isn't present in the legend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants