Skip to content

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

Closed
@ClaudioHeinrich

Description

@ClaudioHeinrich

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorscales 🐍

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions