From dfcba72864455efe7853656d06c35342ca7d0b32 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Wed, 13 Nov 2024 11:52:50 +0100 Subject: [PATCH 1/3] bump NA to back of key --- R/guide-bins.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/guide-bins.R b/R/guide-bins.R index 0124ea6052..e7b217888d 100644 --- a/R/guide-bins.R +++ b/R/guide-bins.R @@ -208,7 +208,9 @@ 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)) + key[params$aesthetic] <- vec_slice(key[params$aesthetic], c(ord[-1], ord[1])) key$.value <- 1 - key$.value } From d06fa35eb926305264de7db47ce9abe5f06b47b9 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Wed, 13 Nov 2024 12:00:08 +0100 Subject: [PATCH 2/3] add test --- R/guide-bins.R | 1 + .../_snaps/guides/reversed-guide-bins.svg | 97 +++++++++++++++++++ tests/testthat/test-guides.R | 16 +++ 3 files changed, 114 insertions(+) create mode 100644 tests/testthat/_snaps/guides/reversed-guide-bins.svg diff --git a/R/guide-bins.R b/R/guide-bins.R index e7b217888d..f4638f461d 100644 --- a/R/guide-bins.R +++ b/R/guide-bins.R @@ -210,6 +210,7 @@ GuideBins <- ggproto( if (params$reverse) { 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 5904676541..19b9a080b4 100644 --- a/tests/testthat/test-guides.R +++ b/tests/testthat/test-guides.R @@ -378,6 +378,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)) From 7929c1429838a353199d406a18da736db5e4074d Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Fri, 15 Nov 2024 09:10:44 +0100 Subject: [PATCH 3/3] add news bullet --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 081504c782..8fc04e7762 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # ggplot2 (development version) +* Fixed regression in `guide_bins(reverse = TRUE)` (@teunbrand, #6183). * When discrete breaks have names, they'll be used as labels by default (@teunbrand, #6147). * The helper function `is.waiver()` is now exported to help extensions to work