Skip to content

scale_manual throws unexpected error when na.values given and insufficient values provided #3451

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
baderstine opened this issue Jul 23, 2019 · 1 comment · Fixed by #4471
Labels
bug an unexpected problem or unintended behavior scales 🐍
Milestone

Comments

@baderstine
Copy link

baderstine commented Jul 23, 2019

In the help documentation for scale_manual(), the values portion states:

values "a set of aesthetic values to map data values to. If this is a named vector, then the values will be matched based on the names. If unnamed, values will be matched in order (usually alphabetical) with the limits of the scale. Any data values that don't match will be given na.value."

Given this definition, I was surprised that an error was still thrown in the following situation:

For discrete scales, regardless of whether the actual values match, if there are too few values provided, the "Insufficient Values" error is thrown, as in the test below:

test_that("insufficient values raise an error", {

You could provide a sufficient number of (non-matching) values and avoid the error.

df <- data.frame(x = 1, y = 1:3, z = factor(c("a","b", NA), exclude = NULL))
p <- qplot(x, y, data = df, colour = z)

# Case 1: Insufficient values error (as expected)
ggplot_build(p + scale_colour_manual(values = c("a" = "black")))

# Case 2: Insufficient values error (not as expected)
ggplot_build(p + scale_colour_manual(values = c("a" = "black"), na.value="grey"))

# Case 3: plot with "b" points given na.value (as expected)
ggplot_build(p + scale_colour_manual(values = c("a" = "black", "c" = "white"), na.value="grey"))

I was expecting an error if na.value was not specified (Case 1).
I was NOT expecting an error if na.value WAS specified (Case 2).

My thinking being that case 2 and case 3 are doing basically the same thing but Case 3 has some arbitrary extra value(s) added simply to match the number of values. I'm not sure what was intended, but seems to me that if na.value is provided, the number of values given shouldn't really matter because "Any data values that don't match will be given na.value."

Related to #2428

@paleolimbot
Copy link
Member

I think there are two things here:

  • scale_*_manual() scales do some very odd things...they pretty much only work as expected when the number of values is the same length as the limits. They use scales::manual_pal(), which might not return the right named values when limits is too small, and as you've noted, an error is thrown when the limits are longer than the manual palette provided.

  • Discrete scales do not censor (turn to NA) values that are not in limits. I don't know why this is, but it has come up a few times (I'm thinking of limits are not used for the domain of manual scales with named values #2666)

I'm not sure exactly how this should be fixed, but the workaround is to always specify the limits when using a manual scale.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior scales 🐍
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants