Skip to content

Commit 7a393da

Browse files
authored
Only sort numeric breaks in binned guides (#5758)
* only sort numeric breaks * add test * add news bullet
1 parent d05f8a4 commit 7a393da

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
* Patterns and gradients are now also enabled in `geom_sf()`
1616
(@teunbrand, #5716).
1717
* `stat_bin()` deals with non-finite breaks better (@teunbrand, #5665).
18+
* Fixed bug in `guide_bins()` and `guide_coloursteps()` where discrete breaks,
19+
such as the levels produced by `cut()`, were ordered incorrectly
20+
(@teunbrand, #5757).
1821
* Theme elements that do not exist now throw warnings instead of errors (#5719).
1922
* Fixed bug in `coord_radial()` where full circles were not treated as such
2023
(@teunbrand, #5750).

R/guide-bins.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,9 @@ parse_binned_breaks = function(scale, breaks = scale$get_breaks(),
333333
if (length(breaks) == 0) {
334334
return(NULL)
335335
}
336-
breaks <- sort(breaks)
336+
337337
if (is.numeric(breaks)) {
338+
breaks <- sort(breaks)
338339
limits <- scale$get_limits()
339340
if (!is.numeric(scale$breaks)) {
340341
breaks <- breaks[!breaks %in% limits]

tests/testthat/test-guides.R

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,34 @@ test_that("empty guides are dropped", {
501501
expect_equal(lengths(guides, use.names = FALSE), rep(0, 5))
502502
})
503503

504+
test_that("bins can be parsed by guides for all scale types", {
505+
506+
breaks <- c(90, 100, 200, 300)
507+
limits <- c(0, 1000)
508+
509+
sc <- scale_colour_continuous(breaks = breaks)
510+
sc$train(limits)
511+
512+
expect_equal(parse_binned_breaks(sc)$breaks, breaks)
513+
514+
sc <- scale_colour_binned(breaks = breaks)
515+
sc$train(limits)
516+
517+
expect_equal(parse_binned_breaks(sc)$breaks, breaks)
518+
519+
# Note: discrete binned breaks treats outer breaks as limits
520+
cut <- cut(c(0, 95, 150, 250, 1000), breaks = breaks)
521+
522+
sc <- scale_colour_discrete()
523+
sc$train(cut)
524+
525+
parsed <- parse_binned_breaks(sc)
526+
expect_equal(
527+
sort(c(parsed$limits, parsed$breaks)),
528+
breaks
529+
)
530+
})
531+
504532
# Visual tests ------------------------------------------------------------
505533

506534
test_that("axis guides are drawn correctly", {

0 commit comments

Comments
 (0)