From 9f1f73f81bd99110e363ac4f166d7e86f7f1c13d Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Sat, 17 Jun 2023 10:50:53 +0200 Subject: [PATCH] Preserve aesthetics in colourbar merge --- NEWS.md | 2 ++ R/guide-colorbar.R | 2 ++ tests/testthat/test-guides.R | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/NEWS.md b/NEWS.md index 7a9eb66130..7a79bff0c6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -54,6 +54,8 @@ * `guide_coloursteps()` and `guide_bins()` sort breaks (#5152). * `guide_axis()` gains a `cap` argument that can be used to trim the axis line to extreme breaks (#4907). + * `guide_colourbar()` and `guide_coloursteps()` merge properly when one + of aesthetics is dropped (#5324). * `geom_label()` now uses the `angle` aesthetic (@teunbrand, #2785) * 'lines' units in `geom_label()`, often used in the `label.padding` argument, diff --git a/R/guide-colorbar.R b/R/guide-colorbar.R index 65291f37cc..d4707f0380 100644 --- a/R/guide-colorbar.R +++ b/R/guide-colorbar.R @@ -370,6 +370,8 @@ GuideColourbar <- ggproto( }, merge = function(self, params, new_guide, new_params) { + new_params$key$.label <- new_params$key$.value <- NULL + params$key <- vec_cbind(params$key, new_params$key) return(list(guide = self, params = params)) }, diff --git a/tests/testthat/test-guides.R b/tests/testthat/test-guides.R index 4ef7174f99..a162dcb7b2 100644 --- a/tests/testthat/test-guides.R +++ b/tests/testthat/test-guides.R @@ -311,6 +311,26 @@ test_that("guide_coloursteps and guide_bins return ordered breaks", { expect_true(all(diff(key$.value) < 0)) }) +test_that("guide_colourbar merging preserves both aesthetics", { + # See issue 5324 + + scale1 <- scale_colour_viridis_c() + scale1$train(c(0, 2)) + + scale2 <- scale_fill_viridis_c() + scale2$train(c(0, 2)) + + g <- guide_colourbar() + p <- g$params + + p1 <- g$train(p, scale1, "colour") + p2 <- g$train(p, scale2, "fill") + + merged <- g$merge(p1, g, p2) + + expect_true(all(c("colour", "fill") %in% names(merged$params$key))) +}) + # Visual tests ------------------------------------------------------------ test_that("axis guides are drawn correctly", {