Skip to content

When providing a function for a breaks argument, specifying a vector of labels of the right ultimate length trips an error. #6556

@BajczA475

Description

@BajczA475

I may have found an issue, or I may just be missing something. When I supply a function as the input to scale_x_continuous's breaks argument like so:

#THIS RUNS
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
    geom_point(aes(color = Sepal.Width)) +
    scale_x_continuous(breaks = scales::breaks_extended(n = 8))

I get a determinate output. In this case, there are clearly 8 breaks in the final graph, and this outcome never varies in length or the specific breaks arrived at.

However, if I then try to specify a vector of custom labels for those ultimate breaks:

#THIS THROWS AN ERROR
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
    geom_point(aes(color = Sepal.Width)) +
    scale_x_continuous(breaks = scales::breaks_extended(n = 8),
                       labels = as.character(1:8))

This throws an error that "breaks" and "labels" have different lengths.

I had suspected that this might be because of the probing steps that ggplot2 takes to, e.g., assess whether the inputs to labels and breaks are functions or vectors, or else the tuning steps that occur to arrive at a final set of breaks when the breaks input was a function--at some point, I assumed the number of breaks != the number of labels and the error throws, even if the "final" set of breaks would actually be the right length.

But then I kept experimenting. I was able to avoid the error if I made the labels input a function also:

#THIS WORKS
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
    geom_point(aes(color = Sepal.Width)) +
    scale_x_continuous(breaks = scales::breaks_extended(n = 8), labels = function(x) { paste0(x, "Can I?")})

But not when that function would result in a determinate vector of labels, even if the length of that vector would match the length of the breaks:

#THIS THROWS AN ERROR
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
    geom_point(aes(color = Sepal.Width)) +
    scale_x_continuous(breaks = scales::breaks_extended(n = 8), labels = function(x) { LETTERS[1:8]} )

In this particular case, it seems if I supply a slightly larger vector of labels than I think I should need, I'm fine:

#THIS RUNS
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
    geom_point(aes(color = Sepal.Width)) +
    scale_x_continuous(breaks = scales::breaks_extended(n = 8), labels = function(x) { LETTERS[1:9]} )

And that works even if I supply them as a vector and not as a function. So, I presume that means a single NA break is being assigned behind the scenes (at the beginning of the vector, since the first break gets assigned a B). Maybe because it's outside the limits of the scale? However, as the "end user," I'm not sure how I'm supposed to know what's happening to be able to provide the right length of vector for labels.

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