Skip to content

Only sort numeric breaks in binned guides #5758

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
Mar 18, 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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
* Patterns and gradients are now also enabled in `geom_sf()`
(@teunbrand, #5716).
* `stat_bin()` deals with non-finite breaks better (@teunbrand, #5665).
* Fixed bug in `guide_bins()` and `guide_coloursteps()` where discrete breaks,
such as the levels produced by `cut()`, were ordered incorrectly
(@teunbrand, #5757).
* Theme elements that do not exist now throw warnings instead of errors (#5719).
* Fixed bug in `coord_radial()` where full circles were not treated as such
(@teunbrand, #5750).
Expand Down
3 changes: 2 additions & 1 deletion R/guide-bins.R
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,9 @@ parse_binned_breaks = function(scale, breaks = scale$get_breaks(),
if (length(breaks) == 0) {
return(NULL)
}
breaks <- sort(breaks)

if (is.numeric(breaks)) {
breaks <- sort(breaks)
limits <- scale$get_limits()
if (!is.numeric(scale$breaks)) {
breaks <- breaks[!breaks %in% limits]
Expand Down
28 changes: 28 additions & 0 deletions tests/testthat/test-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,34 @@ test_that("empty guides are dropped", {
expect_equal(lengths(guides, use.names = FALSE), rep(0, 5))
})

test_that("bins can be parsed by guides for all scale types", {

breaks <- c(90, 100, 200, 300)
limits <- c(0, 1000)

sc <- scale_colour_continuous(breaks = breaks)
sc$train(limits)

expect_equal(parse_binned_breaks(sc)$breaks, breaks)

sc <- scale_colour_binned(breaks = breaks)
sc$train(limits)

expect_equal(parse_binned_breaks(sc)$breaks, breaks)

# Note: discrete binned breaks treats outer breaks as limits
cut <- cut(c(0, 95, 150, 250, 1000), breaks = breaks)

sc <- scale_colour_discrete()
sc$train(cut)

parsed <- parse_binned_breaks(sc)
expect_equal(
sort(c(parsed$limits, parsed$breaks)),
breaks
)
})

# Visual tests ------------------------------------------------------------

test_that("axis guides are drawn correctly", {
Expand Down