Skip to content

Binned guides discard out-of-bound breaks #5873

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
May 20, 2024
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)

* Fixed bug where binned guides would keep out-of-bounds breaks
(@teunbrand, #5870).
* The size of the `draw_key_polygon()` glyph now reflects the `linewidth`
aesthetic (#4852).
* New function `complete_theme()` to replicate how themes are handled during
Expand Down
1 change: 1 addition & 0 deletions R/guide-bins.R
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ parse_binned_breaks = function(scale, breaks = scale$get_breaks()) {
if (!is.numeric(scale$breaks)) {
breaks <- breaks[!breaks %in% limits]
}
breaks <- oob_discard(breaks, limits)
all_breaks <- unique0(c(limits[1], breaks, limits[2]))
bin_at <- all_breaks[-1] - diff(all_breaks) / 2
} else {
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,14 @@ test_that("guide_coloursteps and guide_bins return ordered breaks", {
g <- guide_bins()
key <- g$train(scale = scale, aesthetics = "colour")$key
expect_true(all(diff(key$.value) > 0))

# Out of bound breaks are removed
scale <- scale_colour_viridis_c(breaks = c(10, 20, 30, 40, 50), na.value = "grey50")
scale$train(c(15, 45))

g <- guide_colorsteps()
key <- g$train(scale = scale, aesthetic = "colour")$key
expect_equal(sum(key$colour == "grey50"), 0)
})

test_that("guide_coloursteps can parse (un)even steps from discrete scales", {
Expand Down
Loading