Skip to content

Commit da2a8e8

Browse files
authored
Consider linewidth in legend key sizes (#5346)
* Key size accounts for linewidth * Add test * Add bullet
1 parent 9395629 commit da2a8e8

File tree

4 files changed

+118
-11
lines changed

4 files changed

+118
-11
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@
7575
* `guide_coloursteps()` and `guide_bins()` sort breaks (#5152).
7676
* `guide_axis()` gains a `cap` argument that can be used to trim the
7777
axis line to extreme breaks (#4907).
78+
* Fixed regression in `guide_legend()` where the `linewidth` key size
79+
wasn't adapted to the width of the lines (#5160).
7880

7981
* `geom_label()` now uses the `angle` aesthetic (@teunbrand, #2785)
8082
* 'lines' units in `geom_label()`, often used in the `label.padding` argument,

R/guide-legend.R

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,6 @@ GuideLegend <- ggproto(
329329

330330
data <- modify_list(data, params$override.aes)
331331

332-
if (!is.null(data$size)) {
333-
data$size[is.na(data$size)] <- 0
334-
}
335-
336332
list(
337333
draw_key = layer$geom$draw_key,
338334
data = data,
@@ -728,15 +724,17 @@ measure_legend_keys <- function(decor, n, dim, byrow = FALSE,
728724
zeroes <- rep(0, prod(dim) - n)
729725

730726
# For every layer, extract the size in cm
731-
size <- lapply(decor, function(g) g$data$size / 10) # mm to cm
727+
size <- lapply(decor, function(g) {
728+
lwd <- g$data$linewidth %||% 0
729+
lwd[is.na(lwd)] <- 0
730+
size <- g$data$size %||% 0
731+
size[is.na(size)] <- 0
732+
vec_recycle((size + lwd) / 10, size = nrow(g$data))
733+
})
732734
size <- inject(cbind(!!!size))
733735

734-
# Guard against layers with no size aesthetic
735-
if (any(dim(size) == 0)) {
736-
size <- matrix(0, ncol = 1, nrow = n)
737-
} else {
738-
size <- size[seq_len(n), , drop = FALSE]
739-
}
736+
# Binned legends may have `n + 1` breaks, but we need to display `n` keys.
737+
size <- vec_slice(size, seq_len(n))
740738

741739
# For every key, find maximum across all layers
742740
size <- apply(size, 1, max)
Lines changed: 96 additions & 0 deletions
Loading

tests/testthat/test-guides.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,17 @@ test_that("guides title and text are positioned correctly", {
627627
expect_doppelganger("rotated guide titles and labels", p )
628628
})
629629

630+
test_that("size and linewidth affect key size", {
631+
df <- data_frame(x = c(0, 1, 2))
632+
p <- ggplot(df, aes(x, x)) +
633+
geom_point(aes(size = x)) +
634+
geom_line(aes(linewidth = 2 - x)) +
635+
scale_size_continuous(range = c(1, 12)) +
636+
scale_linewidth_continuous(range = c(1, 20))
637+
638+
expect_doppelganger("enlarged guides", p)
639+
})
640+
630641
test_that("colorbar can be styled", {
631642
df <- data_frame(x = c(0, 1, 2))
632643
p <- ggplot(df, aes(x, x, color = x)) + geom_point()

0 commit comments

Comments
 (0)