Skip to content

Suggestion: scale_*_manual warning when named vector doesn't match #5298

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
jashapiro opened this issue May 6, 2023 · 1 comment
Closed
Labels
messages requests for improvements to error, warning, or feedback messages

Comments

@jashapiro
Copy link
Contributor

When using a named vector in the values argument of scale_fill_manual() and similar functions, data values that do not appear in the vector's names are given the na.value. This seems like the correct behavior, but it does introduce subtle bugs when using a vector for the scale that may not have been created by the user.

This has come up for me in the context of the palette.colors() function in R 4.0+, which sometimes returns a named vector (see examples below). While the fix is easy once the source of the issue is known, identifying that source can be surprisingly subtle for a new user, as there is little indication in the code below that changing the choice of palette will result in a change in the vector type, or the effect that will have on the plot.

I think my best suggestion would be to add a warning for the user when a named vector is used in a scale that does not match any of the values it is being used to modify. I'm not sure if I would want this warning to occur once per session or more frequently, but even the one time I think would save potential user frustration (speaking from past experience!).

library(ggplot2)
iris_plot <- ggplot(iris, aes(y = Sepal.Length, x = Species, fill = Species)) +
  geom_boxplot()

# vector without names works as expected
pal_s1 <- palette.colors(palette = "Set 1") 
pal_s1
#> [1] "#E41A1C" "#377EB8" "#4DAF4A" "#984EA3" "#FF7F00" "#FFFF33" "#A65628"
#> [8] "#F781BF" "#999999"
iris_plot + scale_fill_manual(values = pal_s1)

# named vector does not result in use of expected colors
pal_oi <- palette.colors(palette = "Okabe-Ito") 
pal_oi
#>         black        orange       skyblue   bluishgreen        yellow 
#>     "#000000"     "#E69F00"     "#56B4E9"     "#009E73"     "#F0E442" 
#>          blue    vermillion reddishpurple          gray 
#>     "#0072B2"     "#D55E00"     "#CC79A7"     "#999999"
iris_plot + scale_fill_manual(values = pal_oi)

# adding unname() solves the issue
pal_oiu <- unname(pal_oi)
iris_plot + scale_fill_manual(values = pal_oiu)

Created on 2023-05-06 with reprex v2.0.2

@jashapiro
Copy link
Contributor Author

I'll note that this particular example has been patched in R-devel, but the more general concern remains, and I still believe a warning would be useful.

teunbrand added a commit to teunbrand/ggplot2 that referenced this issue May 8, 2023
@teunbrand teunbrand added the messages requests for improvements to error, warning, or feedback messages label May 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
messages requests for improvements to error, warning, or feedback messages
Projects
None yet
Development

No branches or pull requests

2 participants