Skip to content

Allow scale_{x/y}_sqrt() to have breaks at 0. #4775

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

Merged
merged 10 commits into from
May 17, 2022
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Imports:
MASS,
mgcv,
rlang (>= 1.0.0),
scales (>= 0.5.0),
scales (>= 1.2.0),
stats,
tibble,
withr (>= 2.0.0)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ggplot2 (development version)

* Automatic break calculation now squishes the scale limits to the domain
of the transformation. This allows `scale_{x/y}_sqrt()` to find breaks at 0
when appropriate (@teunbrand, #980).

* Using multiple modified aesthetics correctly will no longer trigger warnings.
If used incorrectly, the warning will now report the duplicated aesthetic
instead of `NA` (@teunbrand, #4707).
Expand Down
4 changes: 4 additions & 0 deletions R/scale-.r
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,10 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
return(numeric())
}

# Ensure limits don't exceed domain (#980)
domain <- self$trans$transform(self$trans$domain)
limits <- oob_squish(limits, domain)

# Limits in transformed space need to be converted back to data space
limits <- self$trans$inverse(limits)

Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/_snaps/sec-axis/sec-axis-monotonicity-test.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions tests/testthat/test-scales-breaks-labels.r
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,15 @@ test_that("functional limits work for continuous scales", {
ggplot(mpg, aes(class)) + geom_bar(aes(fill = drv)) + scale_y_continuous(limits = limiter(50))
)
})

test_that("limits are squished to transformation domain", {
# Breaks should not be calculated on ranges outside domain #980
sc1 <- scale_x_sqrt()
sc2 <- scale_x_sqrt()

sc1$train(c(0, 10))
sc2$train(c(-10, 10))

expect_equal(sc1$get_breaks(), sc2$get_breaks())
expect_equal(sc2$get_breaks()[1], 0)
})