Skip to content

Change in behaviour of manual scales in release candidate #3727

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
paleolimbot opened this issue Jan 13, 2020 · 10 comments
Closed

Change in behaviour of manual scales in release candidate #3727

paleolimbot opened this issue Jan 13, 2020 · 10 comments

Comments

@paleolimbot
Copy link
Member

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

@paleolimbot
Copy link
Member Author

(it looks like this changed in #3579)

@thomasp85
Copy link
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

@paleolimbot
Copy link
Member Author

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.

@paleolimbot
Copy link
Member Author

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?

@yutannihilation
Copy link
Member

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

@clauswilke
Copy link
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.

clauswilke added a commit to wilkelab/ggplot2_archive that referenced this issue Jan 13, 2020
@clauswilke
Copy link
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.

@thomasp85
Copy link
Member

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

@clauswilke
Copy link
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)

@clauswilke
Copy link
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)

clauswilke added a commit that referenced this issue Jan 15, 2020
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

4 participants