Skip to content

scale_fill_discrete makes fills with grey when using a named vector #4197

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

Closed
vinay-swamy opened this issue Sep 10, 2020 · 4 comments
Closed

Comments

@vinay-swamy
Copy link
Contributor

Hi,
I'm having a problem with scale_fill_discrete, where if I provide a named vector of colors as input, the colors are filled as grey. However, If I use an unnamed vector for the same colors, the plot works fine

This does not work

library(ggplot2)
data(iris)
pal <- c('amethyst' ="#F0A0FF" , 'blue' ="#0075DC" , 'caramel'= "#993F00" )

ggplot(iris)  + 
  geom_boxplot(aes(x=Species, y=Petal.Length, fill = Species))+
  scale_fill_discrete(type = pal)+
  theme_classic()

This does work

library(ggplot2)
data(iris)
pal <- c('amethyst' ="#F0A0FF" , 'blue' ="#0075DC" , 'caramel'= "#993F00" )
names(pal) <-NULL
ggplot(iris)  + 
  geom_boxplot(aes(x=Species, y=Petal.Length, fill = Species))+
  scale_fill_discrete(type = pal)+
  theme_classic()

A bit of an edge case, but the pals package which I use a lot for my color palettes adds names to their discrete palettes.
Any idea for a solution

Session info

─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 3.6.1 (2019-07-05)
 os       macOS Mojave 10.14.6        
 system   x86_64, darwin15.6.0        
 ui       RStudio                     
 language (EN)                        
 collate  en_US.UTF-8                 
 ctype    en_US.UTF-8                 
 tz       America/New_York            
 date     2020-09-09                  

─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 package          * version    date       lib source                          
 assertthat         0.2.1      2019-03-21 [1] CRAN (R 3.6.0)                  
 backports          1.1.4      2019-04-10 [1] CRAN (R 3.6.0)                  
 broom              0.5.2      2019-04-07 [1] CRAN (R 3.6.0)                  
 cellranger         1.1.0      2016-07-27 [1] CRAN (R 3.6.0)                  
 cli                2.0.2      2020-02-28 [1] CRAN (R 3.6.0)                  
 colorspace         1.4-1      2019-03-18 [1] CRAN (R 3.6.0)                  
 crayon             1.3.4      2017-09-16 [1] CRAN (R 3.6.0)                  
 crosstalk          1.0.0      2016-12-21 [1] CRAN (R 3.6.0)                  
 DBI                1.1.0      2019-12-15 [1] CRAN (R 3.6.0)                  
 dbplyr             1.4.2      2019-06-17 [1] CRAN (R 3.6.0)                  
 dichromat          2.0-0      2013-01-24 [1] CRAN (R 3.6.0)                  
 digest             0.6.25     2020-02-23 [1] CRAN (R 3.6.0)                  
 dplyr            * 1.0.0.9000 2020-06-10 [1] Github (tidyverse/dplyr@6380011)
 ellipsis           0.3.1      2020-05-15 [1] CRAN (R 3.6.2)                  
 fansi              0.4.1      2020-01-08 [1] CRAN (R 3.6.0)                  
 fastmap            1.0.1      2019-10-08 [1] CRAN (R 3.6.0)                  
 forcats          * 0.4.0      2019-02-17 [1] CRAN (R 3.6.0)                  
 fs                 1.3.1      2019-05-06 [1] CRAN (R 3.6.0)                  
 generics           0.0.2      2018-11-29 [1] CRAN (R 3.6.0)                  
 ggplot2          * 3.3.2      2020-06-19 [1] CRAN (R 3.6.2)                  
 glue               1.4.1      2020-05-13 [1] CRAN (R 3.6.2)                  
 gtable             0.3.0      2019-03-25 [1] CRAN (R 3.6.0)                  
 haven              2.2.0      2019-11-08 [1] CRAN (R 3.6.0)                  
 hms                0.5.3      2020-01-08 [1] CRAN (R 3.6.0)                  
 htmltools          0.5.0      2020-06-16 [1] CRAN (R 3.6.2)                  
 htmlwidgets        1.3        2018-09-30 [1] CRAN (R 3.6.0)                  
 httpuv             1.5.4      2020-06-06 [1] CRAN (R 3.6.2)                  
 httr               1.4.1      2019-08-05 [1] CRAN (R 3.6.0)                  
 jsonlite           1.6        2018-12-07 [1] CRAN (R 3.6.0)                  
 knitr              1.23       2019-05-18 [1] CRAN (R 3.6.0)                  
 labeling           0.3        2014-08-23 [1] CRAN (R 3.6.0)                  
 later              1.0.0      2019-10-04 [1] CRAN (R 3.6.0)                  
 lattice            0.20-38    2018-11-04 [2] CRAN (R 3.6.1)                  
 lifecycle          0.2.0      2020-03-06 [1] CRAN (R 3.6.0)                  
 lubridate          1.7.4      2018-04-11 [1] CRAN (R 3.6.0)                  
 magrittr           1.5        2014-11-22 [1] CRAN (R 3.6.0)                  
 manipulateWidget   0.10.0     2018-06-11 [1] CRAN (R 3.6.0)                  
 mapproj            1.2.6      2018-03-29 [1] CRAN (R 3.6.0)                  
 maps             * 3.3.0      2018-04-03 [1] CRAN (R 3.6.0)                  
 mime               0.7        2019-06-11 [1] CRAN (R 3.6.0)                  
 miniUI             0.1.1.1    2018-05-18 [1] CRAN (R 3.6.0)                  
 modelr             0.1.5      2019-08-08 [1] CRAN (R 3.6.0)                  
 munsell            0.5.0      2018-06-12 [1] CRAN (R 3.6.0)                  
 nlme               3.1-140    2019-05-12 [2] CRAN (R 3.6.1)                  
 pals             * 1.5        2018-01-22 [1] CRAN (R 3.6.0)                  
 patchwork        * 1.0.0      2019-12-01 [1] CRAN (R 3.6.0)                  
 pillar             1.4.4      2020-05-05 [1] CRAN (R 3.6.2)                  
 pkgconfig          2.0.3      2019-09-22 [1] CRAN (R 3.6.0)                  
 promises           1.1.1      2020-06-09 [1] CRAN (R 3.6.2)                  
 purrr            * 0.3.4      2020-04-17 [1] CRAN (R 3.6.2)                  
 R6                 2.4.1      2019-11-12 [1] CRAN (R 3.6.0)                  
 RColorBrewer     * 1.1-2      2014-12-07 [1] CRAN (R 3.6.0)                  
 Rcpp               1.0.2      2019-07-25 [1] CRAN (R 3.6.0)                  
 readr            * 1.3.1      2018-12-21 [1] CRAN (R 3.6.0)                  
 readxl             1.3.1      2019-03-13 [1] CRAN (R 3.6.0)                  
 reprex             0.3.0      2019-05-16 [1] CRAN (R 3.6.0)                  
 rgl                0.100.26   2019-07-08 [1] CRAN (R 3.6.0)                  
 rlang              0.4.6      2020-05-02 [1] CRAN (R 3.6.2)                  
 rstudioapi         0.10       2019-03-19 [1] CRAN (R 3.6.0)                  
 rvest              0.3.5      2019-11-08 [1] CRAN (R 3.6.0)                  
 scales             1.0.0      2018-08-09 [1] CRAN (R 3.6.0)                  
 sessioninfo        1.1.1      2018-11-05 [1] CRAN (R 3.6.0)                  
 shiny              1.5.0      2020-06-23 [1] CRAN (R 3.6.2)                  
 stringi            1.4.3      2019-03-12 [1] CRAN (R 3.6.0)                  
 stringr          * 1.4.0      2019-02-10 [1] CRAN (R 3.6.0)                  
 tibble           * 3.0.1      2020-04-20 [1] CRAN (R 3.6.2)                  
 tidyr            * 1.0.2      2020-01-24 [1] CRAN (R 3.6.0)                  
 tidyselect         1.1.0      2020-05-11 [1] CRAN (R 3.6.2)                  
 tidyverse        * 1.3.0      2019-11-21 [1] CRAN (R 3.6.0)                  
 vctrs              0.3.1      2020-06-05 [1] CRAN (R 3.6.2)                  
 webshot            0.5.1      2018-09-28 [1] CRAN (R 3.6.0)                  
 withr              2.1.2      2018-03-15 [1] CRAN (R 3.6.0)                  
 xfun               0.8        2019-06-25 [1] CRAN (R 3.6.0)                  
 xml2               1.2.2      2019-08-09 [1] CRAN (R 3.6.0)                  
 xtable             1.8-4      2019-04-21 [1] CRAN (R 3.6.0)    
@karawoo
Copy link
Member

karawoo commented Sep 10, 2020

If given a named vector, ggplot2 will match the named colors to the limits of the scale. So in your example, the following palette would work:

pal <- c("setosa" = "#F0A0FF", "versicolor" = "#0075DC", "virginica" = "#993F00")

I think this is the intended behavior and you'll have to unname or rename the palettes you want to use, or else create a custom scale as in #4087 (and using the dev version of the scales package).

@vinay-swamy
Copy link
Contributor Author

isn't that what scale_fill_manual would before used for? personally I feel like the use case for scale_fill_discrete is to just drop in a color palette and have it auto-mapped to factor levels, and the use case for scale_fill_manual is to provide a specific color mapping.

In any case, I think it would be useful to include something like " If this is a named vector, then the values will be matched based on the names instead. Data values that don't match will be given na.value."(which is from the scale_*_manual doc) to the documentation for scale_fill_discrete

@yutannihilation
Copy link
Member

In any case, I think it would be useful to include something like " If this is a named vector, then the values will be matched based on the names instead. Data values that don't match will be given na.value."(which is from the scale_*_manual doc) to the documentation for scale_fill_discrete

Sounds good to me. Would you mind creating a pull request for this?

@vinay-swamy
Copy link
Contributor Author

I made a pull request (#4243)

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

3 participants