-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Nothing plotted with manual_scale when a named vector is used as the input. #4087
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
Comments
The relevant lines are here: Lines 809 to 819 in 7f88a56
With 3 colors, we enter the first branch of the However when we request 4 colors, library("scales")
library("rlang")
named_vec <- c(
"teracota" = "#d18975",
"green" = "#8fd175",
"purple" = "#3f2d54"
)
(pal_3 <- manual_pal(named_vec)(3))
#> teracota green purple
#> "#d18975" "#8fd175" "#3f2d54"
(pal_4 <- manual_pal(named_vec)(4))
#> Warning: This manual palette can handle a maximum of 3 values. You have supplied
#> 4.
#> teracota green purple <NA>
#> "#d18975" "#8fd175" "#3f2d54" NA
is_named(pal_3)
#> [1] TRUE
is_named(pal_4)
#> [1] FALSE Created on 2020-06-24 by the reprex package (v0.3.0.9001) It looks like some of this was added by @yutannihilation in #3685 to fix a different issue. I'm not totally sure what we want to do here, but I agree it is confusing, both that the palette has to be unnamed (though this can be worked around easily enough), and that when you have too many values for the palette it suddenly works. I ran into the first issue myself just the other day. |
The convention in ggplot2 is that for named palettes the values are assigned by matching the names to the scale limits. So example 2 and example 3 are correct, I'd say. The only question is whether example 1 is a problem. Since it issues a clear warning, maybe not? |
I think example 1 is behaving inconsistently regardless of the warning message. Example 1 has two issues: the limits don't match the named values and it requests more values than are in the palette. The warning message clearly explains the latter issue, but that still leaves the fact that when 3 colors are requested and they don't match the limits, none of them appear, but when too many colors are requested and they don't match the limits, 3 of them appear. |
I still don't get the correct semantics here, but this seems just my mistake that I didn't aware how
|
No worries, it's definitely an edge case. It sounds like we should stick with the existing behavior when the limits don't match the names in a named palette, but update our check for the names so that in example 1 it would return |
Thanks, basically I agree. By the way, one of the cause of this confusion is that the behavior of manual_pal <- function(values) {
force(values)
function(n) {
n_values <- length(values)
if (n > n_values) {
warning("This manual palette can handle a maximum of ", n_values,
" values. You have supplied ", n, ".",
call. = FALSE
)
}
values[seq_len(n)]
}
} Lines 143 to 148 in ccd94e1
|
Quickly revisited the histories. When Meanwhile, ggplot2 had supported named usage at some point (sorry, I couldn't find the exact commit), and then the implementation got diverged by this commit, which removed @ds-jim |
I mean, it seems |
@yutannihilation I'm using discreet scale as I'm building a company colour theme for our internal package. I'm a heavy ggplot2 user but this is my first foray into building colour pallets. I used this post as my starting point https://vf.svbtle.com/creating-corporate-colour-palettes-for-ggplot2. However, for groups where n < our branding pallet using a I completely agree that this is an edge case but not knowing how ggplot handled the vector took me quite a while to realise it was the named vector causing the issue, as you suggested it was very easily fixed in my code with I can see that named vectors have their place and my only suggestion is to either close the case and have this knowledge saved to help someone else who stumbles into this issue or return a more explicit warning like when you accidentally type a pipe instead of a + |
Ah, thanks for the detail. I got it. You actually needed (I just remembered I had the same experience of creating a company colour theme and shouting "why do I need |
This literally just happened to me last week as well 😆 |
When building a manual_scale ggplot shows no data when the number of colours <= the number in the scale when a named vector is used. Strangely the colours work when the number of colours > the number in the scale. This seems to be fixed by using an unnamed vector.
Created on 2020-06-24 by the reprex package (v0.3.0)
The text was updated successfully, but these errors were encountered: