Skip to content

Rework legend spacing #5456

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

Merged
merged 16 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# ggplot2 (development version)

* The spacing between legend keys and their labels, in addition to legends
and their titles, is now controlled by the text's `margin` setting. Not
specifying margins will automatically add appropriate text margins. This
leaves the `legend.spacing` dedicated to controlling the spacing between
different keys (#5455).

* New function `check_device()` for testing the availability of advanced
graphics features introduced in R 4.1.0 onwards (@teunbrand, #5332).

Expand Down
7 changes: 0 additions & 7 deletions R/guide-colorbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -429,17 +429,10 @@ GuideColourbar <- ggproto(
return(list(labels = zeroGrob()))
}

just <- if (params$direction == "horizontal") {
elements$text$vjust
} else {
elements$text$hjust
}

list(labels = flip_element_grob(
elements$text,
label = validate_labels(key$.label),
x = unit(key$.value, "npc"),
y = rep(just, nrow(key)),
margin_x = FALSE,
margin_y = TRUE,
flip = params$direction == "vertical"
Expand Down
51 changes: 31 additions & 20 deletions R/guide-legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,17 @@ GuideLegend <- ggproto(
"cm", valueOnly = TRUE
)

if (is.null(params$label.theme$margin %||% theme$legend.text$margin) &&
!inherits(elements$text, "element_blank")) {
i <- match(params$label.position, .trbl[c(3, 4, 1, 2)])
elements$text$margin[i] <- elements$text$margin[i] + gap
}
if (is.null(params$title.theme$margin %||% theme$legend.title$margin) &&
!inherits(elements$title, "element_blank")) {
i <- match(params$title.position, .trbl[c(3, 4, 1, 2)])
elements$title$margin[i] <- elements$title$margin[i] + gap
}

# Evaluate backgrounds early
if (!is.null(elements$background)) {
elements$background <- ggname(
Expand Down Expand Up @@ -521,17 +532,17 @@ GuideLegend <- ggproto(
hgap <- elements$hgap %||% 0
widths <- switch(
params$label.position,
"left" = list(label_widths, hgap, widths, hgap),
"right" = list(widths, hgap, label_widths, hgap),
"left" = list(label_widths, widths, hgap),
"right" = list(widths, label_widths, hgap),
list(pmax(label_widths, widths), hgap * (!byrow))
)
widths <- head(vec_interleave(!!!widths), -1)

vgap <- elements$vgap %||% 0
heights <- switch(
params$label.position,
"top" = list(label_heights, vgap, heights, vgap),
"bottom" = list(heights, vgap, label_heights, vgap),
"top" = list(label_heights, heights, vgap),
"bottom" = list(heights, label_heights, vgap),
list(pmax(label_heights, heights), vgap * (byrow))
)
heights <- head(vec_interleave(!!!heights), -1)
Expand All @@ -543,14 +554,14 @@ GuideLegend <- ggproto(
# Combine title with rest of the sizes based on its position
widths <- switch(
params$title.position,
"left" = c(title_width, hgap, widths),
"right" = c(widths, hgap, title_width),
"left" = c(title_width, widths),
"right" = c(widths, title_width),
c(widths, max(0, title_width - sum(widths)))
)
heights <- switch(
params$title.position,
"top" = c(title_height, vgap, heights),
"bottom" = c(heights, vgap, title_height),
"top" = c(title_height, heights),
"bottom" = c(heights, title_height),
c(heights, max(0, title_height - sum(heights)))
)

Expand Down Expand Up @@ -585,29 +596,29 @@ GuideLegend <- ggproto(
switch(
params$label.position,
"top" = {
key_row <- key_row * 2
label_row <- label_row * 2 - 2
key_row <- key_row + df$R
label_row <- key_row - 1
},
"bottom" = {
key_row <- key_row * 2 - 2
label_row <- label_row * 2
key_row <- key_row + df$R - 1
label_row <- key_row + 1
},
"left" = {
key_col <- key_col * 2
label_col <- label_col * 2 - 2
key_col <- key_col + df$C
label_col <- key_col - 1
},
"right" = {
key_col <- key_col * 2 - 2
label_col <- label_col * 2
key_col <- key_col + df$C - 1
label_col <- key_col + 1
}
)

# Offset layout based on title position
switch(
params$title.position,
"top" = {
key_row <- key_row + 2
label_row <- label_row + 2
key_row <- key_row + 1
label_row <- label_row + 1
title_row <- 2
title_col <- seq_along(sizes$widths) + 1
},
Expand All @@ -616,8 +627,8 @@ GuideLegend <- ggproto(
title_col <- seq_along(sizes$widths) + 1
},
"left" = {
key_col <- key_col + 2
label_col <- label_col + 2
key_col <- key_col + 1
label_col <- label_col + 1
title_row <- seq_along(sizes$heights) + 1
title_col <- 2
},
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/_snaps/guides/rotated-guide-titles-and-labels.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions tests/testthat/test-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -601,10 +601,10 @@ test_that("guides title and text are positioned correctly", {
scale_fill_continuous(name = "the\ncontinuous\ncolorscale")
)
expect_doppelganger("vertical gap of 1cm between guide title and guide",
p + theme(legend.spacing.y = grid::unit(1, "cm"))
p + theme(legend.title = element_text(margin = margin(b = 1, unit = "cm")))
)
expect_doppelganger("horizontal gap of 1cm between guide and guide text",
p + theme(legend.spacing.x = grid::unit(1, "cm"))
p + theme(legend.text = element_text(margin = margin(l = 1, unit = "cm")))
)

# now test label positioning, alignment, etc
Expand All @@ -619,8 +619,8 @@ test_that("guides title and text are positioned correctly", {

expect_doppelganger("guide title and text positioning and alignment via themes",
p + theme(
legend.title = element_text(hjust = 0.5, margin = margin(t = 30)),
legend.text = element_text(hjust = 1, margin = margin(l = 5, t = 10, b = 10))
legend.title = element_text(hjust = 0.5, margin = margin(t = 30, b = 5.5)),
legend.text = element_text(hjust = 1, margin = margin(l = 10.5, t = 10, b = 10))
)
)

Expand Down