diff --git a/NEWS.md b/NEWS.md index 8fe979a6a8..b6bd30f1db 100644 --- a/NEWS.md +++ b/NEWS.md @@ -77,6 +77,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). * Fixed regression in `guide_legend()` where the `linewidth` key size wasn't adapted to the width of the lines (#5160). diff --git a/R/guide-colorbar.R b/R/guide-colorbar.R index 092b5a35ba..4917679ebf 100644 --- a/R/guide-colorbar.R +++ b/R/guide-colorbar.R @@ -376,6 +376,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 a19de230b6..ac704fdf79 100644 --- a/tests/testthat/test-guides.R +++ b/tests/testthat/test-guides.R @@ -307,6 +307,27 @@ 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))) +}) + test_that("guide_colourbar warns about discrete scales", { g <- guide_colourbar() @@ -315,6 +336,7 @@ test_that("guide_colourbar warns about discrete scales", { expect_warning(g <- g$train(g$params, s, "colour"), "needs continuous scales") expect_null(g) + }) # Visual tests ------------------------------------------------------------