From 59c751e12f87cafd797b15301d1653acc35be9d9 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 10 Jul 2023 22:33:03 +0200 Subject: [PATCH 1/3] Key size accounts for linewidth --- R/guide-legend.R | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/R/guide-legend.R b/R/guide-legend.R index 0e6193aa24..6f6930142f 100644 --- a/R/guide-legend.R +++ b/R/guide-legend.R @@ -332,10 +332,6 @@ GuideLegend <- ggproto( data <- modify_list(data, params$override.aes) - if (!is.null(data$size)) { - data$size[is.na(data$size)] <- 0 - } - list( draw_key = layer$geom$draw_key, data = data, @@ -728,15 +724,17 @@ measure_legend_keys <- function(decor, n, dim, byrow = FALSE, zeroes <- rep(0, prod(dim) - n) # For every layer, extract the size in cm - size <- lapply(decor, function(g) g$data$size / 10) # mm to cm + size <- lapply(decor, function(g) { + lwd <- g$data$linewidth %||% 0 + lwd[is.na(lwd)] <- 0 + size <- g$data$size %||% 0 + size[is.na(size)] <- 0 + vec_recycle((size + lwd) / 10, size = nrow(g$data)) + }) size <- inject(cbind(!!!size)) - # Guard against layers with no size aesthetic - if (any(dim(size) == 0)) { - size <- matrix(0, ncol = 1, nrow = n) - } else { - size <- size[seq_len(n), , drop = FALSE] - } + # Binned legends may have `n + 1` breaks, but we need to display `n` keys. + size <- vec_slice(size, seq_len(n)) # For every key, find maximum across all layers size <- apply(size, 1, max) From fda4dcf305c0bd4ab093ceb73df6e8471c991144 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 10 Jul 2023 22:33:10 +0200 Subject: [PATCH 2/3] Add test --- .../_snaps/guides/enlarged-guides.svg | 96 +++++++++++++++++++ tests/testthat/test-guides.R | 11 +++ 2 files changed, 107 insertions(+) create mode 100644 tests/testthat/_snaps/guides/enlarged-guides.svg diff --git a/tests/testthat/_snaps/guides/enlarged-guides.svg b/tests/testthat/_snaps/guides/enlarged-guides.svg new file mode 100644 index 0000000000..69c7aaba61 --- /dev/null +++ b/tests/testthat/_snaps/guides/enlarged-guides.svg @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.0 +0.5 +1.0 +1.5 +2.0 + + + + + + + + + + +0.0 +0.5 +1.0 +1.5 +2.0 +x +x + +x + + + + + + + + + + +0.0 +0.5 +1.0 +1.5 +2.0 + +2 - x + + + + + + + + + + +0.0 +0.5 +1.0 +1.5 +2.0 +enlarged guides + + diff --git a/tests/testthat/test-guides.R b/tests/testthat/test-guides.R index 4ef7174f99..13262447ec 100644 --- a/tests/testthat/test-guides.R +++ b/tests/testthat/test-guides.R @@ -621,6 +621,17 @@ test_that("guides title and text are positioned correctly", { expect_doppelganger("rotated guide titles and labels", p ) }) +test_that("size and linewidth affect key size", { + df <- data_frame(x = c(0, 1, 2)) + p <- ggplot(df, aes(x, x)) + + geom_point(aes(size = x)) + + geom_line(aes(linewidth = 2 - x)) + + scale_size_continuous(range = c(1, 12)) + + scale_linewidth_continuous(range = c(1, 20)) + + expect_doppelganger("enlarged guides", p) +}) + test_that("colorbar can be styled", { df <- data_frame(x = c(0, 1, 2)) p <- ggplot(df, aes(x, x, color = x)) + geom_point() From 11c081650db91c695fb8ab89e6650a29b97df4c9 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 10 Jul 2023 22:33:20 +0200 Subject: [PATCH 3/3] Add bullet --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 6fb136dcfc..67e1cafc3c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -57,6 +57,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). + * Fixed regression in `guide_legend()` where the `linewidth` key size + wasn't adapted to the width of the lines (#5160). * `geom_label()` now uses the `angle` aesthetic (@teunbrand, #2785) * 'lines' units in `geom_label()`, often used in the `label.padding` argument,