Skip to content
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

scale_{colour,fill}_manual(limits = ...) doesn't limit the scale #3262

Closed
yutannihilation opened this issue Apr 24, 2019 · 2 comments · Fixed by #3685
Closed

scale_{colour,fill}_manual(limits = ...) doesn't limit the scale #3262

yutannihilation opened this issue Apr 24, 2019 · 2 comments · Fixed by #3685
Labels
bug an unexpected problem or unintended behavior scales 🐍
Milestone

Comments

@yutannihilation
Copy link
Member

(I found this during looking at the examples on scale_manual.)

It seems scale_{colour,fill}_manual(limits = ...) doesn't limit the scale actually (but limit the legend).

library(ggplot2)
library(patchwork)

p <- ggplot(mtcars, aes(factor(cyl))) +
  geom_bar(aes(fill = factor(cyl)))

# this drops 6
p1 <- p + scale_fill_discrete(limits = c("4", "8"))

# this doesn't
cols <- c("8" = "red", "4" = "blue", "6" = "darkgreen", "10" = "orange")
p2 <- p + scale_fill_manual(values = cols, limits = c("4", "8"))

p1 / p2

Created on 2019-04-24 by the reprex package (v0.2.1)

@thomasp85
Copy link
Member

Do you think we can fix this for 3.2.0?

@yutannihilation
Copy link
Member Author

I forgot to reply here. After a quick look, I think there are two possible fixes:

  1. Add limits argument to manual_scale() and subset values before creating a palette from the values.
  2. Raise an error when limits is provided.

First one seems straightforward, but I enumerate the second because manual_scale() can accept pal while it's difficult to limit the pal and it might be confusing.

If we go with the first, probably it will be something like this:

manual_scale <- function(aesthetic, values = NULL, ..., limits = NULL) {
  ...
  if (!is.null(limits)) {
    values <- values[values %in% limits]
  }

  pal <- function(n) {
  ...

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