Skip to content

Commit

Permalink
fix scale_units when both unit and trans are specified, closes #357
Browse files Browse the repository at this point in the history
  • Loading branch information
Enchufa2 committed Sep 12, 2023
1 parent b6c9ab7 commit dca9e63
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions R/scale_units.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down
61 changes: 61 additions & 0 deletions tests/testthat/_snaps/plot/ggplot2-trans-unit.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions tests/testthat/test_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit dca9e63

Please sign in to comment.