From b174986fbf9d6c2c7b0f0356a4dc98a21bf7de84 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 28 Oct 2024 15:50:04 +0100 Subject: [PATCH] Fix binned scaled not accepting function-limits if there are transformations (#6145) * fix bug * add test * add news bullet --- NEWS.md | 2 ++ R/scale-.R | 2 +- tests/testthat/test-scale-binned.R | 10 ++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index f369879868..540e662f0c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # ggplot2 (development version) +* Fixed bug where binned scales wouldn't simultaneously accept transformations + and function-limits (@teunbrand, #6144). * Fixed bug where the `ggplot2::`-prefix did not work with `stage()` (@teunbrand, #6104). * New `get_labs()` function for retrieving completed plot labels diff --git a/R/scale-.R b/R/scale-.R index 878cc602b9..bb56743bdb 100644 --- a/R/scale-.R +++ b/R/scale-.R @@ -319,7 +319,7 @@ binned_scale <- function(aesthetics, scale_name = deprecated(), palette, name = } transform <- as.transform(transform) - if (!is.null(limits)) { + if (!is.null(limits) && !is.function(limits)) { limits <- transform$transform(limits) } diff --git a/tests/testthat/test-scale-binned.R b/tests/testthat/test-scale-binned.R index 31e32e9eba..1f558b6c77 100644 --- a/tests/testthat/test-scale-binned.R +++ b/tests/testthat/test-scale-binned.R @@ -44,6 +44,16 @@ test_that("binned limits should not compute out-of-bounds breaks", { )) }) +test_that("binned scales can use limits and transformations simultaneously (#6144)", { + s <- scale_x_binned( + limits = function(x) x + 1, + trans = transform_log10() + ) + s$train(c(0, 1)) # c(1, 10) in untransformed space + out <- s$get_limits() + expect_equal(s$get_limits(), log10(c(2, 11))) +}) + test_that("binned scales can use NAs in limits", { scale <- scale_x_binned(limits = c(NA, 10)) scale$train(c(-20, 20))