Skip to content

Dev version: Error in get_labels(): ! breaks and labels are different lengths #4858

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
tungttnguyen opened this issue May 26, 2022 · 2 comments · Fixed by #4859
Closed

Dev version: Error in get_labels(): ! breaks and labels are different lengths #4858

tungttnguyen opened this issue May 26, 2022 · 2 comments · Fixed by #4859

Comments

@tungttnguyen
Copy link

The following code worked with the current version 3.3.6 on CRAN but failed in the dev version (R 4.1.3 x64 on Windows 10). I am wondering what commit broke it? Thanks!

library(ggplot2)
packageVersion("ggplot2")
#> [1] '3.3.6.9000'

groups <- LETTERS[1:3]

df <- data.frame(
  x = rep(1:10, 3),
  y = rnorm(30),
  group = rep(groups, each = 10)
)
df
#>     x           y group
#> 1   1 -0.38073967     A
#> 2   2  0.67451767     A
#> 3   3  0.02328174     A
#> 4   4  1.29683640     A
#> 5   5 -1.70061129     A
#> 6   6 -1.66503519     A
#> 7   7  0.18308837     A
#> 8   8 -0.89437196     A
#> 9   9 -0.37365461     A
#> 10 10 -1.44795542     A
#> 11  1 -0.20990950     B
#> 12  2  0.24870379     B

ggplot(df, aes(x, y, colour = group, linetype = group)) +
  geom_line() +
  scale_x_reverse(breaks = rev(c(10, 5, 2, 1)),
                  labels = rev(c("10", "5", "", "1"))) +
  scale_colour_manual(
    "",
    values = setNames(c("blue", "green", "red"), groups)
  ) +
  scale_linetype_manual(
    "",
    values = setNames(c("solid", "dashed", "dotted"), groups)
  )
#> Error in `get_labels()`:
#> ! `breaks` and `labels` are different lengths

rlang::last_error()
---
Backtrace:
  1. base `<fn>`(x)
  2. ggplot2:::print.ggplot(x)
  4. ggplot2:::ggplot_gtable.ggplot_built(data)
  5. layout$setup_panel_guides(plot$guides, plot$layers, plot$mapping)
  6. ggplot2 setup_panel_guides(..., self = self)
  7. base::lapply(...)
  8. ggplot2 FUN(X[[i]], ...)
  9. ggplot2 train_panel_guides(..., self = self)
 10. base::lapply(...)
 11. ggplot2 FUN(X[[i]], ...)
 13. ggplot2:::guide_train.axis(guide, panel_params[[aesthetic]])
 14. scale$get_labels(breaks)
 15. ggplot2 get_labels(..., self = self)
 16. self$scale$get_labels(breaks)
 17. ggplot2 get_labels(..., self = self)

---
Backtrace:
     x
  1. +-base `<fn>`(x)
  2. \-ggplot2:::print.ggplot(x)
  3.   +-ggplot2::ggplot_gtable(data)
  4.   \-ggplot2:::ggplot_gtable.ggplot_built(data)
  5.     \-layout$setup_panel_guides(plot$guides, plot$layers, plot$mapping)
  6.       \-ggplot2 setup_panel_guides(..., self = self)
  7.         \-base::lapply(...)
  8.           \-ggplot2 FUN(X[[i]], ...)
  9.             \-ggplot2 train_panel_guides(..., self = self)
 10.               \-base::lapply(...)
 11.                 \-ggplot2 FUN(X[[i]], ...)
 12.                   +-ggplot2::guide_train(guide, panel_params[[aesthetic]])
 13.                   \-ggplot2:::guide_train.axis(guide, panel_params[[aesthetic]])
 14.                     \-scale$get_labels(breaks)
 15.                       \-ggplot2 get_labels(..., self = self)
 16.                         \-self$scale$get_labels(breaks)
 17.                           \-ggplot2 get_labels(..., self = self)
 18.                             \-cli::cli_abort("{.arg breaks} and {.arg labels} are different lengths")
 19.                               \-rlang::abort(message, ..., call = call, use_cli_format = TRUE)

Plot output from version 3.3.6 on CRAN

Created on 2022-05-26 by the reprex package (v2.0.1)

@teunbrand
Copy link
Collaborator

teunbrand commented May 26, 2022

A more minimal reprex (not current dev with pretty errors):

library(ggplot2)

df <- data.frame(
  x = c(1:10),
  y = rnorm(10)
)

ggplot(df, aes(x, y)) +
  geom_line() +
  scale_x_reverse(breaks = c(10, 5, 2, 1),
                  labels = c("10", "5", "", "1"))
#> Error in `f()`:
#> ! `breaks` and `labels` are different lengths

Created on 2022-05-26 by the reprex package (v2.0.1)

The cause of the error happens here (I'm afraid I'm guilty of introducing this bug in #4775):

ggplot2/R/scale-.r

Lines 639 to 640 in 50e917a

domain <- suppressWarnings(self$trans$transform(self$trans$domain))
limits <- oob_squish(limits, domain)

The domain (-Inf, Inf) gets the reverse transform and becomes (Inf, -Inf). oob_squish() doesn't internally sort the range argument, so the limits become c(-Inf,-Inf), which lead to one break: -Inf, which has a different length than labels.

Should in theory be fixed by sorting the domain before passing it to oob_squish() , e.g.:

limits <- oob_squish(limits, sort(domain))

Off topic: I think it would do no harm to suggest to the good people of the {scales} package that it might be a good idea to sort the range before squishing.

@tungttnguyen
Copy link
Author

Thanks @teunbrand !

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