Skip to content

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

Closed

Description

@yutannihilation

(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)

Activity

  1. thomasp85 commented on Apr 24, 2019

    @thomasp85
    Member

    Do you think we can fix this for 3.2.0?

  2. yutannihilation commented on May 1, 2019

    @yutannihilation
    MemberAuthor

    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) {
      ...
  3. added
    bug
    on May 21, 2019
  4. added this to the ggplot2 3.3.0 milestone on Dec 18, 2019
  5. added a commit that references this issue on Jan 6, 2020
    c514c26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugscales 🐍

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions