Skip to content

Commit e30642b

Browse files
authored
Limits of binned scales when data has zero width (#6225)
* fallback for zero-range limits * add test * add news bullet
1 parent 7eeb636 commit e30642b

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

NEWS.md

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

3+
* Binned scales with zero-width data expand the default limits by 0.1
4+
(@teunbrand, #5066)
35
* New default `geom_qq_line(geom = "abline")` for better clipping in the
46
vertical direction. In addition, `slope` and `intercept` are new computed
57
variables in `stat_qq_line()` (@teunbrand, #6087).

R/scale-.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1292,9 +1292,17 @@ ScaleBinned <- ggproto("ScaleBinned", Scale,
12921292
new_limits[1] <- breaks[1]
12931293
breaks <- breaks[-1]
12941294
}
1295-
} else {
1295+
} else if (nbreaks == 1) {
12961296
bin_size <- max(breaks[1] - limits[1], limits[2] - breaks[1])
12971297
new_limits <- c(breaks[1] - bin_size, breaks[1] + bin_size)
1298+
} else {
1299+
new_limits <- limits
1300+
if (zero_range(new_limits)) {
1301+
# 0.1 is the same width as the expansion `default_expansion()`
1302+
# gives for 0-width data
1303+
new_limits <- new_limits + c(-0.05, 0.05)
1304+
}
1305+
breaks <- new_limits
12981306
}
12991307
new_limits_trans <- suppressWarnings(transformation$transform(new_limits))
13001308
limits[is.finite(new_limits_trans)] <- new_limits[is.finite(new_limits_trans)]

tests/testthat/test-scale-binned.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,9 @@ test_that('binned scales can calculate breaks on date-times', {
104104
)))
105105
)
106106
})
107+
108+
test_that("binned scales can calculate breaks for zero-width data", {
109+
scale <- scale_x_binned()
110+
scale$train(c(1, 1))
111+
expect_equal(scale$get_breaks(), c(0.95, 1.05))
112+
})

0 commit comments

Comments
 (0)