Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider linewidth in legend key sizes #5346

Merged
merged 3 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 9 additions & 11 deletions R/guide-legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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 @@ -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()
Expand Down
Loading