Skip to content
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

Discard out-of-bounds binned breaks after generating labels #6061

Merged
merged 7 commits into from
Dec 2, 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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# ggplot2 (development version)

* Fixed bug in out-of-bounds binned breaks (@teunbrand, #6054)
* Binned guides now accept expressions as labels (@teunbrand, #6005)
* (internal) `Scale$get_labels()` format expressions as lists.
* In non-orthogonal coordinate systems (`coord_sf()`, `coord_polar()` and
Expand Down
14 changes: 10 additions & 4 deletions R/guide-bins.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ GuideBins <- ggproto(
key$.show <- NA

labels <- scale$get_labels(breaks)
labels <- labels[!is.na(breaks)]
breaks <- breaks[!is.na(breaks)]

if (is.character(scale$labels) || is.numeric(scale$labels) || is.expression(scale$labels)) {
limit_lab <- c(NA, NA)
} else {
Expand Down Expand Up @@ -335,19 +338,22 @@ GuideBins <- ggproto(

parse_binned_breaks <- function(scale, breaks = scale$get_breaks()) {

breaks <- breaks[!is.na(breaks)]
if (is.waiver(scale$labels) || is.function(scale$labels)) {
breaks <- breaks[!is.na(breaks)]
}
if (length(breaks) == 0) {
return(NULL)
}

if (is.numeric(breaks)) {
breaks <- sort(breaks)
limits <- scale$get_limits()
if (!is.numeric(scale$breaks)) {
breaks <- breaks[!breaks %in% limits]
breaks[breaks %in% limits] <- NA
}
breaks <- oob_discard(breaks, limits)
breaks <- oob_censor(breaks, limits)
all_breaks <- unique0(c(limits[1], breaks, limits[2]))
# Sorting drops NAs on purpose here
all_breaks <- sort(all_breaks, na.last = NA)
bin_at <- all_breaks[-1] - diff(all_breaks) / 2
} else {
bin_at <- breaks
Expand Down
4 changes: 3 additions & 1 deletion R/guide-colorsteps.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,13 @@ GuideColoursteps <- ggproto(

key <- data_frame0(!!aesthetic := scale$map(breaks))
if (even.steps) {
key$.value <- seq_along(breaks)
key$.value <- NA_integer_
key$.value[!is.na(breaks)] <- seq_along(breaks[!is.na(breaks)])
} else {
key$.value <- breaks
}
key$.label <- scale$get_labels(breaks)
key <- vec_slice(key, !is.na(breaks))

if (breaks[1] %in% limits) {
key$.value <- key$.value - 1L
Expand Down
3 changes: 0 additions & 3 deletions R/scale-.R
Original file line number Diff line number Diff line change
Expand Up @@ -1307,9 +1307,6 @@ ScaleBinned <- ggproto("ScaleBinned", Scale,
breaks <- self$breaks
}

# Breaks must be within limits
breaks <- oob_discard(breaks, sort(limits))

self$breaks <- breaks

transformation$transform(breaks)
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,20 @@ test_that("bins can be parsed by guides for all scale types", {
)
})

test_that("binned breaks can have hardcoded labels when oob", {

sc <- scale_colour_steps(breaks = 1:3, labels = as.character(1:3))
sc$train(c(1, 2))

g <- guide_bins()
key <- g$train(scale = sc, aesthetic = "colour")$key
expect_equal(key$.label, c("1", "2"))

g <- guide_coloursteps()
key <- g$train(scale = sc, aesthetic = "colour")$key
expect_equal(key$.label, c("1", "2"))
})

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

test_that("guides are positioned correctly", {
Expand Down
Loading