diff --git a/NEWS.md b/NEWS.md index 3d9a9f7f99..4d67350673 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # ggplot2 (development version) +* Fixed regression in `guide_bins(reverse = TRUE)` (@teunbrand, #6183). * New function family for setting parts of a theme. For example, you can now use `theme_sub_axis(line, text, ticks, ticks.length, line)` as a substitute for `theme(axis.line, axis.text, axis.ticks, axis.ticks.length, axis.line)`. This diff --git a/R/geom-violin.R b/R/geom-violin.R index 8c3d23ad71..1ad8c2172a 100644 --- a/R/geom-violin.R +++ b/R/geom-violin.R @@ -118,7 +118,7 @@ geom_violin <- function(mapping = NULL, data = NULL, check_numeric(draw_quantiles) # Pass on to stat when stat accepts 'quantiles' - stat <- check_subclass(stat, "Stat", current_call(), caller_env()) + stat <- validate_subclass(stat, "Stat", current_call(), caller_env()) if ("quantiles" %in% stat$parameters()) { extra$quantiles <- draw_quantiles } diff --git a/R/guide-bins.R b/R/guide-bins.R index c0344bbbe0..c03d5179d6 100644 --- a/R/guide-bins.R +++ b/R/guide-bins.R @@ -211,7 +211,10 @@ GuideBins <- ggproto( params$show.limits <- show.limits if (params$reverse) { - key <- key[rev(seq_len(nrow(key))), , drop = FALSE] + ord <- seq_len(nrow(key)) + key <- vec_slice(key, rev(ord)) + # Put NA back in the trailing position + key[params$aesthetic] <- vec_slice(key[params$aesthetic], c(ord[-1], ord[1])) key$.value <- 1 - key$.value } diff --git a/tests/testthat/_snaps/guides/reversed-guide-bins.svg b/tests/testthat/_snaps/guides/reversed-guide-bins.svg new file mode 100644 index 0000000000..16e8f7efca --- /dev/null +++ b/tests/testthat/_snaps/guides/reversed-guide-bins.svg @@ -0,0 +1,97 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + +0 +25 +50 +75 +100 + + + + + + + + + + +0 +25 +50 +75 +100 +x +x + +x + + + + + + + + + + + + + + +100 +75 +50 +25 +0 + +x + + + + + + + + + + + + +75 +50 +25 +reversed guide_bins + + diff --git a/tests/testthat/test-guides.R b/tests/testthat/test-guides.R index 5718e4f2ad..1a3a31143a 100644 --- a/tests/testthat/test-guides.R +++ b/tests/testthat/test-guides.R @@ -411,6 +411,22 @@ test_that("guides title and text are positioned correctly", { expect_doppelganger("legends with all title justifications", p) }) +test_that("bin guide can be reversed", { + + p <- ggplot(data.frame(x = c(0, 100)), aes(x, x, colour = x, fill = x)) + + geom_point() + + guides( + colour = guide_bins(reverse = TRUE, show.limits = TRUE, order = 1), + fill = guide_bins( + reverse = TRUE, show.limits = FALSE, order = 2, + override.aes = list(shape = 21) + ) + ) + + expect_doppelganger("reversed guide_bins", p) + +}) + test_that("bin guide can be styled correctly", { df <- data_frame(x = c(1, 2, 3), y = c(6, 5, 7))