You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 expectedpal_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 colorspal_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 issuepal_oiu<- unname(pal_oi)
iris_plot+ scale_fill_manual(values=pal_oiu)
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
When using a named vector in the
values
argument ofscale_fill_manual()
and similar functions, data values that do not appear in the vector's names are given thena.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 ofpalette
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!).
Created on 2023-05-06 with reprex v2.0.2
The text was updated successfully, but these errors were encountered: