-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Order of colors in case variable is 'character' #2429
Comments
|
I am using ggplot2 for years now, and I never had this problem, apparently because the variables have always been factors. So, to be honest, I am not very happy with this behaviour, because ggplot is doing something for me, which I am not aware about. I think a warning would have helped a lot to save my time. Wouldn´t it be more sensible to set the levels to the breaks I submitted by Thanks. |
Minimal reprex: library(ggplot2)
df <- data.frame(x = c("red", "black"))
ggplot(df, aes(x, 1, fill = x)) +
geom_col() +
scale_fill_manual(breaks = c("red", "black"), values = c("red", "black")) |
Not sure this is a bug, since it behaves exactly as documented:
Importantly, the documentation also explains how to make this work, by using a named vector: library(ggplot2)
test <- data.frame(a=as.character(gl(n = 4, length = 8, k = 2, labels = letters[1:4])),
b=as.character(gl(n = 2, length = 8, k = 1, labels = c("small", "big"))),
values = sample(10:300, 8))
ggplot(test, aes(x = a, y = values, fill = b)) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(
"test",
breaks = c("small", "big"),
values = c("small" = "black", "big" = "tomato2")
) However, there is a small modification one could make to make the scale behavior more intuitive: Check if |
Following @clauswilke's suggestion, I think this could be implemented by modifying However, with this change
Currently you can use breaks to modify the displayed legend without altering the colour mapping:
After this change, the mapping would be altered, too (plot below with a branch with the change):
For manual scales, this could still be overcome by specifying named PS. I think a good analogue would be that using |
I was proposing to implement this only for manual scales. |
Ah, right --- that makes sense! I hadn't realized that scales stayed aware of being manual; but looking it over again, I see they could be picked out with Alternatively (and probably more cleanly) if the Still, wouldn't it be a bit confusing if these calls gave different results?
|
This is object-oriented programming, so what you'd want to do is make a ggproto object that derives from Line 79 in 1f9ef73
I'll let @hadley chime in whether the feature would be welcome or not. In my own use of |
Okay, I see! A new class for manual scales would also mean that the Regarding the feature itself, I was just recently reading the ggplot2 book, and this quote from the scales chapter came to mind:
Using Perhaps this would be better addressed with changes in the documentation? The help text for Something along the lines of:
|
…ching in scale_manual
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/ |
1 similar comment
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/ |
Hi!
I am using ggplot2 quite often, and I ran into a problem concerning the ordering of colors. So I am setting the breaks and the color values in
scale_fill_manual()
, and I would expect that the first color "black" would be assigned to the first break ('small') and so on. This seems to happen when the grouping variable is data type = character. I suppose you transform the variable into a factor internally, or something like that.Is this the intended behaviour, because for me this is really confusing?!
Thank you!!
Manuel
The text was updated successfully, but these errors were encountered: