-
Notifications
You must be signed in to change notification settings - Fork 2.1k
allow fewer elements in named values vector in manual scales #4471
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
Conversation
This is close but I'm not sure it's exactly what you intended...I imagine that specifying values as a named vector should either match the equivalent CRAN ggplot2: library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.5
df <- data.frame(x = 1, y = 1:3, z = factor(c("a","b", NA), exclude = NULL))
p <- qplot(x, y, data = df, colour = z)
p + scale_color_manual(limits = "a", values = "black")
#> Warning: Removed 2 rows containing missing values (geom_point). p + scale_colour_manual(breaks = "a", values = "black")
#> Error: Insufficient values in manual scale. 2 needed but only 1 provided.
p + scale_colour_manual(values = c("a" = "black"))
#> Error: Insufficient values in manual scale. 2 needed but only 1 provided. Created on 2021-05-07 by the reprex package (v2.0.0) This PR: library(ggplot2)
df <- data.frame(x = 1, y = 1:3, z = factor(c("a","b", NA), exclude = NULL))
p <- qplot(x, y, data = df, colour = z)
p + scale_color_manual(limits = "a", values = "black")
#> Warning: Removed 2 rows containing missing values (geom_point). p + scale_colour_manual(breaks = "a", values = "black")
#> Warning: Removed 2 rows containing missing values (geom_point). p + scale_colour_manual(values = c("a" = "black"))
#> Warning: Removed 2 rows containing missing values (geom_point). Created on 2021-05-07 by the reprex package (v2.0.0) |
What is the behaviour you'd want? This change means that unmatched values gets the na value (defaults to |
It looks like the behaviour is the same but the legend is drawn differently for the named values. It looks like the behaviour and legend for |
The output is now consistent, but I think this no longer fixes the issue (all three error now). library(ggplot2)
df <- data.frame(x = 1, y = 1:3, z = factor(c("a","b", NA), exclude = NULL))
p <- qplot(x, y, data = df, colour = z)
p + scale_color_manual(limits = "a", values = "black")
#> Error: Insufficient values in manual scale. 2 needed but only 1 provided.
p + scale_colour_manual(breaks = "a", values = "black")
#> Error: Insufficient values in manual scale. 2 needed but only 1 provided.
p + scale_colour_manual(values = c("a" = "black"))
#> Error: Insufficient values in manual scale. 2 needed but only 1 provided. Created on 2021-05-07 by the reprex package (v2.0.0) |
Why am I so bad at this today... |
Ah - forgot to push the fix, sorry |
I've looked at the code but I have no idea why the patch does what it does. If @paleolimbot thinks it's good I'm fine with that. |
All good! I'm not sure how deeply you want to delve into the issue...this PR is a huge improvement over the current funky manual scale, but the manual scale is still inconsistent (I would argue) with the behaviour of other discrete scales. Is it easy to have the library(ggplot2)
df <- data.frame(x = 1, y = 1:3, z = factor(c("a","b", NA), exclude = NULL))
p <- qplot(x, y, data = df, colour = z)
p + scale_colour_discrete(limits = "a") p + scale_colour_discrete(breaks = "a") p + scale_color_manual(limits = "a", values = "black")
#> Warning: Removed 2 rows containing missing values (geom_point). p + scale_colour_manual(breaks = "a", values = "black")
#> Error: Insufficient values in manual scale. 2 needed but only 1 provided.
p + scale_colour_manual(values = c("a" = "black"))
#> Warning: Removed 2 rows containing missing values (geom_point). Created on 2021-05-07 by the reprex package (v2.0.0) |
You just set na.value to something sensible, e.g. I'm open to setting a better default that matches the default discrete scales though |
I see, you need |
…com/tidyverse/ggplot2 into issue-3451-manual-scale-named-value
* issue introduced in tidyverse#4471 * potential solution for tidyverse#4534 and tidyverse#4511 * add extra tests
* issue introduced in tidyverse#4471 * potential solution for tidyverse#4534 and tidyverse#4511 * add extra tests
Fix #3451
This PR relaxes the requirement on the manual scale
values
vector and allow it to contain fewer elements than the data. Non-existing values are implicitly consideredNA
and mapped to thena.value
value