Skip to content
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

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

Closed
waynerroper opened this issue May 5, 2022 · 1 comment · Fixed by #4849
Closed

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

waynerroper opened this issue May 5, 2022 · 1 comment · Fixed by #4849

Comments

@waynerroper
Copy link

waynerroper commented May 5, 2022


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
@yutannihilation
Copy link
Member

Thanks, confirmed. It seems the problem is that, when show.limits = TRUE, the labels for the upper and lower limits are set separately with scale$get_labels(), whereas get_labels() always validates the result against the supplied labels.

if (guide$show.limits %||% scale$show.limits %||% FALSE) {
edges <- rescale(c(0, 1), to = guide$bar$value[c(1, nrow(guide$bar))], from = c(0.5, guide$nbin - 0.5) / guide$nbin)
if (guide$reverse) edges <- rev(edges)
guide$key <- guide$key[c(NA, seq_len(nrow(guide$key)), NA), , drop = FALSE]
guide$key$.value[c(1, nrow(guide$key))] <- edges
guide$key$.label[c(1, nrow(guide$key))] <- scale$get_labels(limits)
}

ggplot2/R/scale-.r

Lines 1122 to 1135 in b9e1694

if (is.null(self$labels)) {
return(NULL)
} else if (identical(self$labels, NA)) {
abort("Invalid labels specification. Use NULL, not NA")
} else if (is.waive(self$labels)) {
labels <- self$trans$format(breaks)
} else if (is.function(self$labels)) {
labels <- self$labels(breaks)
} else {
labels <- self$labels
}
if (length(labels) != length(breaks)) {
abort("Breaks and labels are different lengths")
}

@yutannihilation yutannihilation changed the title labeled breaks for color scale malfunction when show.limits = T labeled breaks for binned color scale malfunction when show.limits = T May 14, 2022
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

Successfully merging a pull request may close this issue.

2 participants