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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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)
* Passing empty unmapped aesthetics to layers raises a warning instead of
throwing an error (@teunbrand, #6009).
* Moved {mgcv} from Imports to Suggests (@teunbrand, #5986)
Expand Down
13 changes: 9 additions & 4 deletions R/guide-bins.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ 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)) {
limit_lab <- c(NA, NA)
} else {
Expand Down Expand Up @@ -328,19 +330,22 @@ GuideBins <- ggproto(

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

breaks <- breaks[!is.na(breaks)]
if (is.waive(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 @@ -110,11 +110,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 @@ -1305,9 +1305,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 @@ -552,6 +552,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_binned(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"))
})

test_that("legends can be forced to display unrelated geoms", {

df <- data.frame(x = 1:2)
Expand Down
Loading