Skip to content

Commit 76f0812

Browse files
authored
Scales don't squish to zero-range domains (#5279)
* Don't squish to zero-range domains * Add test battery
1 parent e3b7822 commit 76f0812

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

R/scale-.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
648648
domain <- suppressWarnings(self$trans$transform(self$trans$domain))
649649
domain <- sort(domain)
650650
# To avoid NaN causing issues. NaN are dropped by the sort()
651-
if (length(domain) == 2) {
651+
if (length(domain) == 2 && !zero_range(domain)) {
652652
limits <- oob_squish(limits, domain)
653653
}
654654

tests/testthat/_snaps/scales.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,10 @@
4242

4343
`breaks` and `labels` are different lengths
4444

45+
# numeric scale transforms can produce breaks
46+
47+
Code
48+
test_breaks("log", limits = c(0.1, 1000))
49+
Output
50+
[1] NA 1.00000 20.08554 403.42879
51+

tests/testthat/test-scales.R

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,67 @@ test_that("staged aesthetics are backtransformed properly (#4155)", {
468468
# x / 2 should be 16 / 2 = 8, thus the result should be sqrt(8) on scale_x_sqrt()
469469
expect_equal(layer_data(p)$x, sqrt(8))
470470
})
471+
472+
test_that("numeric scale transforms can produce breaks", {
473+
474+
test_breaks <- function(trans, limits) {
475+
scale <- scale_x_continuous(trans = trans)
476+
scale$train(scale$transform(limits))
477+
view <- view_scale_primary(scale)
478+
scale$trans$inverse(view$get_breaks())
479+
}
480+
481+
expect_equal(test_breaks("asn", limits = c(0, 1)),
482+
seq(0, 1, by = 0.25))
483+
484+
expect_equal(test_breaks("sqrt", limits = c(0, 10)),
485+
seq(0, 10, by = 2.5))
486+
487+
expect_equal(test_breaks("atanh", limits = c(-0.9, 0.9)),
488+
c(NA, -0.5, 0, 0.5, NA))
489+
490+
# Broken, should fix on {scale}'s side
491+
# expect_equal(test_breaks(boxcox_trans(0), limits = c(0, 10)), ...)
492+
493+
expect_equal(test_breaks(modulus_trans(0), c(-10, 10)),
494+
seq(-10, 10, by = 5))
495+
496+
expect_equal(test_breaks(yj_trans(0), c(-10, 10)),
497+
seq(-10, 10, by = 5))
498+
499+
expect_equal(test_breaks("exp", c(-10, 10)),
500+
seq(-10, 10, by = 5))
501+
502+
expect_equal(test_breaks("identity", limits = c(-10, 10)),
503+
seq(-10, 10, by = 5))
504+
505+
# irrational numbers, so snapshot values
506+
expect_snapshot(test_breaks("log", limits = c(0.1, 1000)))
507+
508+
expect_equal(test_breaks("log10", limits = c(0.1, 1000)),
509+
10 ^ seq(-1, 3))
510+
511+
expect_equal(test_breaks("log2", limits = c(0.5, 32)),
512+
c(0.5, 2, 8, 32))
513+
514+
expect_equal(test_breaks("log1p", limits = c(0, 10)),
515+
seq(0, 10, by = 2.5))
516+
517+
expect_equal(test_breaks("pseudo_log", limits = c(-10, 10)),
518+
seq(-10, 10, by = 5))
519+
520+
expect_equal(test_breaks("logit", limits = c(0.001, 0.999)),
521+
c(NA, 0.25, 0.5, 0.75, NA))
522+
523+
expect_equal(test_breaks("probit", limits = c(0.001, 0.999)),
524+
c(NA, 0.25, 0.5, 0.75, NA))
525+
526+
expect_equal(test_breaks("reciprocal", limits = c(1, 10)),
527+
c(NA, 2.5, 5, 7.5, 10))
528+
529+
expect_equal(test_breaks("reverse", limits = c(-10, 10)),
530+
seq(-10, 10, by = 5))
531+
532+
expect_equal(test_breaks("sqrt", limits = c(0, 10)),
533+
seq(0, 10, by = 2.5))
534+
})

0 commit comments

Comments
 (0)