Skip to content

scale_fill_steps produces error when both breaks and labels are specified #4548

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
ClaudioHeinrich opened this issue Jul 7, 2021 · 5 comments
Labels
bug an unexpected problem or unintended behavior scales 🐍

Comments

@ClaudioHeinrich
Copy link

When using scale_fill_steps or scale_fill_steps2, specifying two equal-length vectors as breaks and labels leads to an error.

# some data:
random_data = data.frame(expand.grid(x = 1:10,y = 1:10),fill = runif(100,min = 0,max = 1))

# this works:
ggplot(random_data) + geom_tile(aes(x=x,y=y,fill = fill)) + scale_fill_steps()

# this doesn't:
breaks = seq(0,1,length.out = 5)
labels = breaks
ggplot(random_data) + geom_tile(aes(x=x,y=y,fill = fill)) + scale_fill_steps(breaks = breaks,labels = labels)

I am getting the error message 'Error: Breaks and labels are different lengths'.
I tried to find out which number of labels it expects, and ran this:

for(i in 2:10)
{
  tryCatch({
    labels = seq(0,1,length.out = i)
    ggplot(random_data) + geom_tile(aes(x=x,y=y,fill = fill)) + scale_fill_steps(breaks = breaks,labels = labels)
  }, error=function(e){print(i)})
}

This printed out the numbers from 2 to 10 excluding 5, which I don't understand because setting i to 5 and running the function passed to tryCatch manually does result in an error.

@ClaudioHeinrich
Copy link
Author

This could be related to this recent bugfix:
#4019

After this fix, the labels for the boundaries of the colorscale disappeared, so the number of default labels changed...

@hadley
Copy link
Member

hadley commented Mar 15, 2022

Can you please provide a minimal reprex (reproducible example)? The goal of a reprex is to make it as easy as possible for me to recreate your problem so that I can fix it: please help me help you! If you've never heard of a reprex before, start by reading about the reprex package, including the advice further down the page. Please make sure your reprex is created with the reprex package as it gives nicely formatted output and avoids a number of common pitfalls.

@hadley hadley added the reprex needs a minimal reproducible example label Mar 15, 2022
@ClaudioHeinrich
Copy link
Author

Here you go:

library(ggplot2)
data = data.frame(expand.grid(x = 1:10,y = 1:10),fill = 1:100)

# this works:
ggplot(data) + geom_tile(aes(x=x,y=y,fill = fill)) + scale_fill_steps()

# this doesn't work, although it should (I think):
breaks = seq(0,100,length.out = 5)
labels = breaks
ggplot(data) + geom_tile(aes(x=x,y=y,fill = fill)) + scale_fill_steps(breaks = breaks,labels = labels)
#> Error: Breaks and labels are different lengths

Created on 2022-03-17 by the reprex package (v2.0.1)

@hadley
Copy link
Member

hadley commented Mar 17, 2022

Thanks. I've made it a little more minimal to make it easier to track down the problem:

library(ggplot2)
data <- data.frame(x = 1:3)

ggplot(data) +
  geom_col(aes(x, x, fill = x)) +
  scale_fill_steps(breaks = 1:2, labels = 1:2)
#> Error in `f()`:
#> ! Breaks and labels are different lengths

Created on 2022-03-17 by the reprex package (v2.0.1)

@hadley hadley added bug an unexpected problem or unintended behavior scales 🐍 and removed reprex needs a minimal reproducible example labels Mar 17, 2022
@yutannihilation
Copy link
Member

It seems this is already fixed in the dev version (probably by #4849?).

library(ggplot2)
data <- data.frame(x = 1:3)

ggplot(data) +
  geom_col(aes(x, x, fill = x)) +
  scale_fill_steps(breaks = 1:2, labels = 1:2)

Created on 2022-07-23 by the reprex package (v2.0.1)

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

3 participants