Skip to content

Limits of binned scales when data has zero width #6225

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 4 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggplot2 (development version)

* Binned scales with zero-width data expand the default limits by 0.1
(@teunbrand, #5066)
* New default `geom_qq_line(geom = "abline")` for better clipping in the
vertical direction. In addition, `slope` and `intercept` are new computed
variables in `stat_qq_line()` (@teunbrand, #6087).
Expand Down
10 changes: 9 additions & 1 deletion R/scale-.R
Original file line number Diff line number Diff line change
Expand Up @@ -1292,9 +1292,17 @@ ScaleBinned <- ggproto("ScaleBinned", Scale,
new_limits[1] <- breaks[1]
breaks <- breaks[-1]
}
} else {
} else if (nbreaks == 1) {
bin_size <- max(breaks[1] - limits[1], limits[2] - breaks[1])
new_limits <- c(breaks[1] - bin_size, breaks[1] + bin_size)
} else {
new_limits <- limits
if (zero_range(new_limits)) {
# 0.1 is the same width as the expansion `default_expansion()`
# gives for 0-width data
new_limits <- new_limits + c(-0.05, 0.05)
}
breaks <- new_limits
}
new_limits_trans <- suppressWarnings(transformation$transform(new_limits))
limits[is.finite(new_limits_trans)] <- new_limits[is.finite(new_limits_trans)]
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-scale-binned.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,9 @@ test_that('binned scales can calculate breaks on date-times', {
)))
)
})

test_that("binned scales can calculate breaks for zero-width data", {
scale <- scale_x_binned()
scale$train(c(1, 1))
expect_equal(scale$get_breaks(), c(0.95, 1.05))
})
Loading