Skip to content

Commit 3932165

Browse files
authored
Allow scale_{x/y}_sqrt() to have breaks at 0. (#4775)
1 parent ef22669 commit 3932165

File tree

5 files changed

+25
-3
lines changed

5 files changed

+25
-3
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Imports:
4141
MASS,
4242
mgcv,
4343
rlang (>= 1.0.0),
44-
scales (>= 0.5.0),
44+
scales (>= 1.2.0),
4545
stats,
4646
tibble,
4747
withr (>= 2.0.0)

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# ggplot2 (development version)
22

3+
* Automatic break calculation now squishes the scale limits to the domain
4+
of the transformation. This allows `scale_{x/y}_sqrt()` to find breaks at 0
5+
when appropriate (@teunbrand, #980).
6+
37
* Using multiple modified aesthetics correctly will no longer trigger warnings.
48
If used incorrectly, the warning will now report the duplicated aesthetic
59
instead of `NA` (@teunbrand, #4707).

R/scale-.r

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,10 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
638638
return(numeric())
639639
}
640640

641+
# Ensure limits don't exceed domain (#980)
642+
domain <- self$trans$transform(self$trans$domain)
643+
limits <- oob_squish(limits, domain)
644+
641645
# Limits in transformed space need to be converted back to data space
642646
limits <- self$trans$inverse(limits)
643647

tests/testthat/_snaps/sec-axis/sec-axis-monotonicity-test.svg

Lines changed: 4 additions & 2 deletions
Loading

tests/testthat/test-scales-breaks-labels.r

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,15 @@ test_that("functional limits work for continuous scales", {
367367
ggplot(mpg, aes(class)) + geom_bar(aes(fill = drv)) + scale_y_continuous(limits = limiter(50))
368368
)
369369
})
370+
371+
test_that("limits are squished to transformation domain", {
372+
# Breaks should not be calculated on ranges outside domain #980
373+
sc1 <- scale_x_sqrt()
374+
sc2 <- scale_x_sqrt()
375+
376+
sc1$train(c(0, 10))
377+
sc2$train(c(-10, 10))
378+
379+
expect_equal(sc1$get_breaks(), sc2$get_breaks())
380+
expect_equal(sc2$get_breaks()[1], 0)
381+
})

0 commit comments

Comments
 (0)