Skip to content

labeled breaks for binned color scale malfunction when show.limits = T #4831

Closed
@waynerroper

Description

@waynerroper

When formatting labels for breaks in color scales, ggplot returns an error for the length of label vector if show.limits = T. This error appears even if the breaks and labels vectors are the same length.

This may be because limits and breaks are passed to different vectors before compiling the final plot and therefore the user cannot easily write labels for limits and breaks together. An example is provided here: https://stackoverflow.com/questions/71976832/custom-labels-for-limit-and-break-values-when-using-scale-color-binned-in-r-ggpl

Is it possible to integrate limits and breaks in a more streamlined way to avoid the error message and not have to define labels with a custom function that circumvents the issue?

library(ggplot2)

ggplot(mpg, aes(cty, hwy, color = year)) +
  geom_point() +
  scale_color_binned(limits = c(1999, 2008),
                    breaks = c(2000, 2002, 2004, 2006),
                    labels = c('2', '3', '4', '5'),
                    show.limits = F) # labels work

ggplot(mpg, aes(cty, hwy, color = year)) +
  geom_point() +
  scale_color_binned(limits = c(1999, 2008),
                    breaks = c(2000, 2002, 2004, 2006),
                    labels = c('2', '3', '4', '5'),
                    show.limits = T) # method fails when limits are activated

    # Error in f(): ! Breaks and labels are different lengths

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