Skip to content

limits are not used for the domain of manual scales with named values #2666

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
mikmart opened this issue May 27, 2018 · 4 comments
Closed

limits are not used for the domain of manual scales with named values #2666

mikmart opened this issue May 27, 2018 · 4 comments
Labels
bug an unexpected problem or unintended behavior scales 🐍
Milestone

Comments

@mikmart
Copy link
Contributor

mikmart commented May 27, 2018

Currently, the limits argument to manual scales is not used to define the domain of the scale when a named values vector is provided. Take the example from ?scale_colour_manual:

library(ggplot2)

p <- ggplot(mtcars, aes(mpg, wt)) +
  geom_point(aes(colour = factor(cyl)))

cols <- c("8" = "red", "4" = "blue", "6" = "darkgreen", "10" = "orange")
p + scale_colour_manual(values = cols)

Specifying limits will not change the domain of the scale---below, cyl == 6 values are still mapped to a colour and plotted---but will alter the legend (as the breaks used to draw the guide are assumed to be a subset of limits):

p + scale_colour_manual(values = cols, limits = c("4", "8"))

With more exotic limits this can lead to very confusing legends:

p + scale_colour_manual(values = cols, limits = 2:5)

This could be fixed by ensuring that with named values, only elements with names within the limits are used in the scale mapping.

Created on 2018-05-27 by the reprex package (v0.2.0).

PS. I'd be happy to contribute a PR if this is something that should be fixed.

@paleolimbot
Copy link
Member

I think the issue is that ScaleDiscrete$map() does not censor out-of-bounds values in the way that one might expect. I don't imagine this causes problems in many places, but in the above example I would expect values outside the limits to have the na.value colour, rather than the colour defined by values.

@teunbrand
Copy link
Collaborator

I think this might have been fixed already in #4093. With current master branch:

library(ggplot2)

p <- ggplot(mtcars, aes(mpg, wt)) +
  geom_point(aes(colour = factor(cyl)))

cols <- c("8" = "red", "4" = "blue", "6" = "darkgreen", "10" = "orange")

p + scale_colour_manual(values = cols, limits = as.character(2:5),
                        na.value = "grey50")

Created on 2021-04-28 by the reprex package (v1.0.0)

@clauswilke
Copy link
Member

Can we close this issue then?

@paleolimbot
Copy link
Member

This looks good to me!

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

No branches or pull requests

5 participants