From dca9e63e82fad03b6d51de357044e31de727c780 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20=C3=9Acar?= Date: Tue, 12 Sep 2023 15:53:45 +0200 Subject: [PATCH] fix scale_units when both unit and trans are specified, closes #357 --- NEWS.md | 2 + R/scale_units.R | 10 +-- .../_snaps/plot/ggplot2-trans-unit.svg | 61 +++++++++++++++++++ tests/testthat/test_plot.R | 2 + 4 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 tests/testthat/_snaps/plot/ggplot2-trans-unit.svg diff --git a/NEWS.md b/NEWS.md index 22be71f..76c92ff 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,8 @@ `units_options(simplify = FALSE)` reverting a change from #310; #355 @billdenney +* Fix `scale_units` when both `unit` and `trans` are specified; #357 + # version 0.8-3 * Remove tolerance from comparisons with logical operators, restoring behavior diff --git a/R/scale_units.R b/R/scale_units.R index 53d320c..aad2dd2 100644 --- a/R/scale_units.R +++ b/R/scale_units.R @@ -88,13 +88,6 @@ MakeScaleContinuousPositionUnits <- function() { ggplot2::ScaleContinuousPosition, units = NULL, - train = function(self, x) { - if (length(x) == 0) return() - if (!is.null(self$units)) - units(x) <- as_units(1, self$units) - self$range$train(x) - }, - map = function(self, x, limits = self$get_limits()) { if (inherits(x, "units")) { if (is.null(self$units)) @@ -107,6 +100,9 @@ MakeScaleContinuousPositionUnits <- function() { }, transform = function(self, x) { + if (!is.null(self$units)) + units(x) <- as_units(1, self$units) + new_x <- ggplot2::ggproto_parent( ggplot2::ScaleContinuousPosition, self)$transform(drop_units(x)) as_units(new_x, units(x)) diff --git a/tests/testthat/_snaps/plot/ggplot2-trans-unit.svg b/tests/testthat/_snaps/plot/ggplot2-trans-unit.svg new file mode 100644 index 0000000..44ca7d3 --- /dev/null +++ b/tests/testthat/_snaps/plot/ggplot2-trans-unit.svg @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +2.5 +5.0 +7.5 +10.0 + + + + + + + +1000 +3000 +10000 +a [mm] +a [m] +ggplot2 trans + unit + + diff --git a/tests/testthat/test_plot.R b/tests/testthat/test_plot.R index 1518884..c88d35d 100644 --- a/tests/testthat/test_plot.R +++ b/tests/testthat/test_plot.R @@ -71,9 +71,11 @@ test_that("axis transformations do not affect displayed units", { p0 <- ggplot(df, aes(y=a, x=a)) + geom_point() p1 <- p0 + scale_x_units(trans='log10') + scale_y_units(trans='sqrt') + p2 <- p0 + scale_x_units(trans="log10", unit="mm") vdiffr::expect_doppelganger("ggplot2 default", p0) vdiffr::expect_doppelganger("ggplot2 transformed", p1) + vdiffr::expect_doppelganger("ggplot2 trans + unit", p2) }) do.call(units_options, units:::.default_options)