Skip to content

Commit 2cd0e96

Browse files
authored
Preserve aesthetics in colourbar merge (#5328)
* Preserve aesthetics in colourbar merge
1 parent bde88f8 commit 2cd0e96

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@
7777
* `guide_coloursteps()` and `guide_bins()` sort breaks (#5152).
7878
* `guide_axis()` gains a `cap` argument that can be used to trim the
7979
axis line to extreme breaks (#4907).
80+
* `guide_colourbar()` and `guide_coloursteps()` merge properly when one
81+
of aesthetics is dropped (#5324).
8082
* Fixed regression in `guide_legend()` where the `linewidth` key size
8183
wasn't adapted to the width of the lines (#5160).
8284

R/guide-colorbar.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,8 @@ GuideColourbar <- ggproto(
376376
},
377377

378378
merge = function(self, params, new_guide, new_params) {
379+
new_params$key$.label <- new_params$key$.value <- NULL
380+
params$key <- vec_cbind(params$key, new_params$key)
379381
return(list(guide = self, params = params))
380382
},
381383

tests/testthat/test-guides.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,27 @@ test_that("guide_coloursteps and guide_bins return ordered breaks", {
307307
expect_true(all(diff(key$.value) < 0))
308308
})
309309

310+
311+
test_that("guide_colourbar merging preserves both aesthetics", {
312+
# See issue 5324
313+
314+
scale1 <- scale_colour_viridis_c()
315+
scale1$train(c(0, 2))
316+
317+
scale2 <- scale_fill_viridis_c()
318+
scale2$train(c(0, 2))
319+
320+
g <- guide_colourbar()
321+
p <- g$params
322+
323+
p1 <- g$train(p, scale1, "colour")
324+
p2 <- g$train(p, scale2, "fill")
325+
326+
merged <- g$merge(p1, g, p2)
327+
328+
expect_true(all(c("colour", "fill") %in% names(merged$params$key)))
329+
})
330+
310331
test_that("guide_colourbar warns about discrete scales", {
311332

312333
g <- guide_colourbar()
@@ -315,6 +336,7 @@ test_that("guide_colourbar warns about discrete scales", {
315336

316337
expect_warning(g <- g$train(g$params, s, "colour"), "needs continuous scales")
317338
expect_null(g)
339+
318340
})
319341

320342
# Visual tests ------------------------------------------------------------

0 commit comments

Comments
 (0)