Skip to content

Change in behaviour of manual scales in release candidate #3727

Closed

Description

@paleolimbot

In previous ggplot2 versions, the following was valid:

library(ggplot2)
df <- data.frame(x = 1:3, y = 1:3)
ggplot(df, aes(x, y, col = factor(x))) +  
  geom_point() +
  scale_colour_manual(values = c("red", "green", "blue", "teal"), breaks = factor(1))

In the release candidate, it errors (revdep failures on at least auditor, ezplot, GDCRNATools, ggmuller, obAnalytics, RAM)

library(ggplot2)
df <- data.frame(x = 1:3, y = 1:3)
ggplot(df, aes(x, y, col = factor(x))) +  
  geom_point() +
  scale_colour_manual(values = c("red", "green", "blue", "teal"), breaks = factor(1))
#> Error:   Differing number of values and breaks in manual scale.
#>   4 values provided compared to 1 breaks.

Shouldn't the number of values line up with the number limits instead of the breaks? Otherwise, how would you map colours and not include them on the guide (niche behaviour, but it's possible with other colour scales)?

Activity

  1. paleolimbot commented on Jan 13, 2020

    @paleolimbot
    MemberAuthor

    (it looks like this changed in #3579)

  2. thomasp85 commented on Jan 13, 2020

    @thomasp85
    Member

    I think the old behaviour should be kept (i.e. uneven length of values and breaks). I'm not really in to #3579 so I won't propose how we solve it while keeping the value matching

  3. paleolimbot commented on Jan 13, 2020

    @paleolimbot
    MemberAuthor

    I have a few minutes this morning...I'll see if it's possible to replace breaks with limits from the changes in that PR.

  4. paleolimbot commented on Jan 13, 2020

    @paleolimbot
    MemberAuthor

    I'm not sure that the issue it solved (#2429) was ever a problem...people should have been using limits instead of breaks.

    From the previous version of ggplot2:

    library(ggplot2)
    
    df <- data.frame(x = c("data_red", "data_black"))
    
    p <- ggplot(df, aes(x, 1, fill = x)) + 
      geom_col()
    
    p + scale_fill_manual(values = c("red", "black"),
                          limits = c("data_red", "data_black"))

    Maybe a documentation note to this effect is better than the changes in #3579?

  5. yutannihilation commented on Jan 13, 2020

    @yutannihilation
    Member

    I guess limits didn't work for unnamed values before #3685.

  6. clauswilke commented on Jan 13, 2020

    @clauswilke
    Member

    I think this is a simple oversight. We need at least as many values as breaks, but having more values is ok. This should be easy to fix. I'll give it a shot this morning.

  7. added a commit that references this issue on Jan 13, 2020
    ed44572
  8. clauswilke commented on Jan 13, 2020

    @clauswilke
    Member

    I've made the PR. It simply replaces != with > in the check before the error. I don't see a meaningful way of having more breaks than values, but please let me know if you think this case should also be allowed.

  9. thomasp85 commented on Jan 13, 2020

    @thomasp85
    Member

    Is it allowed in master? (More breaks than values, that is)

  10. clauswilke commented on Jan 13, 2020

    @clauswilke
    Member

    Master and RC behave exactly the same currently. I think the better comparison is named vs. unnamed values, as the error is only triggered by unnamed values.

    For named values, you can have either more or fewer breaks than values, it doesn't matter. The only thing that matters is that you don't have fewer values than limits. So, I think the behavior for unnamed values should match this. I'll amend my PR.

    library(ggplot2)
    df <- data.frame(x = 1:3, y = 1:3, z = letters[1:3])
    ggplot(df, aes(x, y, col = z)) +  
      geom_point() +
      scale_colour_manual(
        values = c(a = "red", b = "green", c = "blue"),
        breaks = letters[1:4],
        limits = letters[1:3]
      )

    df <- data.frame(x = 1:3, y = 1:3, z = letters[1:3])
    ggplot(df, aes(x, y, col = z)) +  
      geom_point() +
      scale_colour_manual(
        values = c("red", "green", "blue"),
        breaks = letters[1:4],
        limits = letters[1:3]
      )
    #> Error:   Too many breaks for the number of values in manual scale.
    #>   3 values provided compared to 4 breaks.

    Created on 2020-01-13 by the reprex package (v0.3.0)

  11. clauswilke commented on Jan 13, 2020

    @clauswilke
    Member

    I've updated the PR. Reprex for the latest version. I think this completely matches the behavior of the current release.

    library(ggplot2)
    
    df <- data.frame(x = 1:3, y = 1:3, z = letters[1:3])
    
    # fewer breaks than limits works
    ggplot(df, aes(x, y, col = z)) +  
      geom_point() +
      scale_colour_manual(
        values = c("red", "green", "blue"),
        breaks = letters[1:2],
        limits = letters[1:3]
      )

    # more breaks than limits works
    ggplot(df, aes(x, y, col = z)) +  
      geom_point() +
      scale_colour_manual(
        values = c("red", "green", "blue"),
        breaks = letters[1:4],
        limits = letters[1:3]
      )

    # only get an error if we request more limits than values
    # this was always the case
    ggplot(df, aes(x, y, col = z)) +  
      geom_point() +
      scale_colour_manual(
        values = c("red", "green", "blue"),
        breaks = letters[1:2],
        limits = letters[1:4]
      )
    #> Error: Insufficient values in manual scale. 4 needed but only 3 provided.

    Created on 2020-01-13 by the reprex package (v0.3.0)

  12. added a commit that references this issue on Jan 15, 2020
    debaf09
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions