Skip to content

Relative legend spacing #6431

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 8 commits into from
May 20, 2025
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 @@ -350,6 +350,8 @@
(@teunbrand, #6269).
* The default colour and fill scales have a new `palette` argument
(@teunbrand, #6064).
* The `theme(legend.spacing.{x/y})` setting now accepts `null`-units
(@teunbrand, #6417).

# ggplot2 3.5.2

Expand Down
43 changes: 22 additions & 21 deletions R/guides-.R
Original file line number Diff line number Diff line change
Expand Up @@ -653,11 +653,14 @@ Guides <- ggproto(
height = heightDetails(grobs[[i]]))
)
}

spacing <- convertWidth(theme$legend.spacing.x, "cm")
spacing <- theme$legend.spacing.x
stretch_spacing <- any(unitType(spacing) == "null")
if (!stretch_spacing) {
spacing <- convertWidth(spacing, "cm")
}
heights <- unit(height_cm(lapply(heights, sum)), "cm")

if (stretch_x) {
if (stretch_x || stretch_spacing) {
widths <- redistribute_null_units(widths, spacing, margin, "width")
vp_width <- unit(1, "npc")
} else {
Expand Down Expand Up @@ -692,10 +695,14 @@ Guides <- ggproto(
)
}

spacing <- convertHeight(theme$legend.spacing.y, "cm")
spacing <- theme$legend.spacing.y
stretch_spacing <- any(unitType(spacing) == "null")
if (!stretch_spacing) {
spacing <- convertWidth(spacing, "cm")
}
widths <- unit(width_cm(lapply(widths, sum)), "cm")

if (stretch_y) {
if (stretch_y || stretch_spacing) {
heights <- redistribute_null_units(heights, spacing, margin, "height")
vp_height <- unit(1, "npc")
} else {
Expand Down Expand Up @@ -735,10 +742,10 @@ Guides <- ggproto(
)

# Set global margin
if (stretch_x) {
if (stretch_x || stretch_spacing) {
global_margin[c(2, 4)] <- unit(0, "cm")
}
if (stretch_y) {
if (stretch_y || stretch_spacing) {
global_margin[c(1, 3)] <- unit(0, "cm")
}
guides <- gtable_add_padding(guides, global_margin)
Expand Down Expand Up @@ -933,26 +940,20 @@ redistribute_null_units <- function(units, spacing, margin, type = "width") {
}

# Get spacing between guides and margins in absolute units
size <- switch(type, width = convertWidth, height = convertHeight)
spacing <- size(spacing, "cm", valueOnly = TRUE)
spacing <- sum(rep(spacing, length(units) - 1))
size <- switch(type, width = width_cm, height = height_cm)
spacing <- sum(rep(spacing, length.out = length(units) - 1))
margin <- switch(type, width = margin[c(2, 4)], height = margin[c(1, 3)])
margin <- sum(size(margin, "cm", valueOnly = TRUE))
margin <- sum(size(margin))

# Get the absolute parts of the unit
absolute <- vapply(units, function(u) {
u <- absolute.size(u)
u <- size(u, "cm", valueOnly = TRUE)
sum(u)
}, numeric(1))
absolute_sum <- sum(absolute) + spacing + margin
absolute <- vapply(units, function(u) sum(size(absolute.size(u))), numeric(1))
absolute_sum <- sum(absolute) + sum(size(spacing)) + margin

# Get the null parts of the unit
num_null <- function(x) sum(as.numeric(x)[unitType(x) == "null"])
relative <- rep(0, length(units))
relative[has_null] <- vapply(units[has_null], function(u) {
sum(as.numeric(u)[unitType(u) == "null"])
}, numeric(1))
relative_sum <- sum(relative)
relative[has_null] <- vapply(units[has_null], num_null, numeric(1))
relative_sum <- sum(relative) + num_null(spacing)

if (relative_sum == 0) {
return(unit(absolute, "cm"))
Expand Down
100 changes: 100 additions & 0 deletions tests/testthat/_snaps/theme/horizontal-legends-placed-apart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading