diff --git a/NEWS.md b/NEWS.md index 10a4869519..34353096e7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,8 @@ * Fixed regression where `draw_key_rect()` stopped using `fill` colours (@mitchelloharawild, #6609). +* Fixed regression where `scale_{x,y}_*()` threw an error when an expression + object is set to `labels` argument (@yutannihilation, #6617). # ggplot2 4.0.0 diff --git a/R/utilities.R b/R/utilities.R index ca06985f26..37000121c0 100644 --- a/R/utilities.R +++ b/R/utilities.R @@ -803,7 +803,7 @@ as_unordered_factor <- function(x) { size0 <- function(x) { if (obj_is_vector(x)) { vec_size(x) - } else if (is.vector(x)) { + } else if (is.vector(x) || is.expression(x)) { length(x) } else { NULL diff --git a/tests/testthat/test-scales-breaks-labels.R b/tests/testthat/test-scales-breaks-labels.R index 1aaf798e52..70a7e0ddcb 100644 --- a/tests/testthat/test-scales-breaks-labels.R +++ b/tests/testthat/test-scales-breaks-labels.R @@ -16,6 +16,14 @@ test_that("labels don't have to match null breaks", { expect_silent(check_breaks_labels(breaks = NULL, labels = 1:2)) }) +test_that("labels accept expressions", { + labels <- parse(text = paste0(1:4, "^degree")) + sc <- scale_y_continuous(breaks = 1:4, labels = labels, limits = c(1, 3)) + + expect_equal(sc$get_breaks(), 1:4) + expect_equal(sc$get_labels(), as.list(labels)) +}) + test_that("labels don't have extra spaces", { labels <- c("a", "abc", "abcdef")