Skip to content

Commit

Permalink
Consider linewidth in legend key sizes (#5346)
Browse files Browse the repository at this point in the history
* Key size accounts for linewidth

* Add test

* Add bullet
  • Loading branch information
teunbrand committed Aug 7, 2023
1 parent 9395629 commit da2a8e8
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 11 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,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,
Expand Down
20 changes: 9 additions & 11 deletions R/guide-legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,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,
Expand Down Expand Up @@ -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)
Expand Down
96 changes: 96 additions & 0 deletions tests/testthat/_snaps/guides/enlarged-guides.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions tests/testthat/test-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,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()
Expand Down

0 comments on commit da2a8e8

Please sign in to comment.