Skip to content

Commit 3b83eea

Browse files
authored
Binned guides discard out-of-bound breaks (#5873)
* oob breaks are discarded * add test * add news bullet
1 parent 49b45c1 commit 3b83eea

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
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+
* Fixed bug where binned guides would keep out-of-bounds breaks
4+
(@teunbrand, #5870).
35
* The size of the `draw_key_polygon()` glyph now reflects the `linewidth`
46
aesthetic (#4852).
57
* New function `complete_theme()` to replicate how themes are handled during

R/guide-bins.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ parse_binned_breaks = function(scale, breaks = scale$get_breaks()) {
339339
if (!is.numeric(scale$breaks)) {
340340
breaks <- breaks[!breaks %in% limits]
341341
}
342+
breaks <- oob_discard(breaks, limits)
342343
all_breaks <- unique0(c(limits[1], breaks, limits[2]))
343344
bin_at <- all_breaks[-1] - diff(all_breaks) / 2
344345
} else {

tests/testthat/test-guides.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,14 @@ test_that("guide_coloursteps and guide_bins return ordered breaks", {
300300
g <- guide_bins()
301301
key <- g$train(scale = scale, aesthetics = "colour")$key
302302
expect_true(all(diff(key$.value) > 0))
303+
304+
# Out of bound breaks are removed
305+
scale <- scale_colour_viridis_c(breaks = c(10, 20, 30, 40, 50), na.value = "grey50")
306+
scale$train(c(15, 45))
307+
308+
g <- guide_colorsteps()
309+
key <- g$train(scale = scale, aesthetic = "colour")$key
310+
expect_equal(sum(key$colour == "grey50"), 0)
303311
})
304312

305313
test_that("guide_coloursteps can parse (un)even steps from discrete scales", {

0 commit comments

Comments
 (0)