Skip to content

Manual scales: not dropping and need exact order to merge with other aesthetics #4864

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
llrs opened this issue Jun 3, 2022 · 2 comments
Closed

Comments

@llrs
Copy link
Contributor

llrs commented Jun 3, 2022

Setting a manual color scale via scale_color_manual doesn't drop unused factors (this could be a feature and not a bug but it is not documented on the help page), and unless they are in the same exact order as other aesthetics the legends are not merged.

I wish there were some kind of check and reorder the scales to see if they match. I'm not sure how easy is to do this, but I think it would make ggplot2 slightly more user friendly.

I didn't find this mentioned previously on any other issue but I might have missed them.

library("ggplot2")
packageVersion("ggplot2")
#> [1] '3.3.6'
p <- ggplot(iris) +
  geom_point(aes(Petal.Length, Petal.Width, col = Species, shape = Species))

Manual scale

p + scale_color_manual(values = c("setosa" = "red", "versicolor" = "green", "virginica" = "blue"))

Manual scale with extra values

p + scale_color_manual(values = c("setosa" = "red", "versicolor" = "green", "virginica" = "blue", "Other" = "black"))

drop doesn’t seem to work with manual values

p + scale_color_manual(values = c("setosa" = "red", "versicolor" = "green", "virginica" = "blue", "Other" = "black"),
                     drop = TRUE)

Manual scale with different order

p + scale_color_manual(values = c("versicolor" = "green", "virginica" = "blue", "setosa" = "red"))

The legends are not merged into a single one.

Created on 2022-06-03 by the reprex package (v2.0.1)

@yutannihilation
Copy link
Member

I forgot which change solved this, but the current dev version's behavior looks fine to me.

library("ggplot2")
packageVersion("ggplot2")
#> [1] '3.3.6.9000'

p <- ggplot(iris) +
  geom_point(aes(Petal.Length, Petal.Width, col = Species, shape = Species))

p + scale_color_manual(values = c("setosa" = "red", "versicolor" = "green", "virginica" = "blue"))

p + scale_color_manual(values = c("setosa" = "red", "versicolor" = "green", "virginica" = "blue", "Other" = "black"))

p + scale_color_manual(values = c("setosa" = "red", "versicolor" = "green", "virginica" = "blue", "Other" = "black"), drop = TRUE)

p + scale_color_manual(values = c("versicolor" = "green", "virginica" = "blue", "setosa" = "red"))

Created on 2022-06-04 by the reprex package (v2.0.1)

@yutannihilation
Copy link
Member

#4619

@llrs llrs closed this as completed Jun 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants