Description
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