diff --git a/NEWS.md b/NEWS.md index 4b10da1543..0b7f698638 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/guide-bins.R b/R/guide-bins.R index 571a461d4b..e2bd0db428 100644 --- a/R/guide-bins.R +++ b/R/guide-bins.R @@ -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 { diff --git a/tests/testthat/test-guides.R b/tests/testthat/test-guides.R index 4930188025..8d677dc1bf 100644 --- a/tests/testthat/test-guides.R +++ b/tests/testthat/test-guides.R @@ -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", {