Skip to content

Commit

Permalink
Preserve aesthetics in colourbar merge (#5328)
Browse files Browse the repository at this point in the history
* Preserve aesthetics in colourbar merge
  • Loading branch information
teunbrand authored Aug 9, 2023
1 parent bde88f8 commit 2cd0e96
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
2 changes: 2 additions & 0 deletions R/guide-colorbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
},

Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 ------------------------------------------------------------
Expand Down

0 comments on commit 2cd0e96

Please sign in to comment.