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

Rework legend spacing #5456

Merged
merged 16 commits into from
Nov 20, 2023
Merged
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# 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).
* `ggsave()` no longer sometimes creates new directories, which is now
controlled by the new `create.dir` argument (#5489).

Expand Down
8 changes: 0 additions & 8 deletions R/guide-bins.R
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,6 @@ GuideBins <- ggproto(
}
key$.label[c(1, n_labels)[!params$show.limits]] <- ""

just <- switch(
params$direction,
horizontal = elements$text$vjust,
vertical = elements$text$hjust,
0.5
)

if (params$direction == "vertical") {
key$.value <- 1 - key$.value
}
Expand All @@ -356,7 +349,6 @@ GuideBins <- ggproto(
elements$text,
label = key$.label,
x = unit(key$.value, "npc"),
y = rep(just, nrow(key)),
margin_x = FALSE,
margin_y = TRUE,
flip = params$direction == "vertical"
Expand Down
10 changes: 0 additions & 10 deletions R/guide-colorbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,6 @@ GuideColourbar <- ggproto(
ticks_length = unit(0.2, "npc"),
background = "legend.background",
margin = "legend.margin",
spacing = "legend.spacing",
spacing.x = "legend.spacing.x",
spacing.y = "legend.spacing.y",
key = "legend.key",
key.height = "legend.key.height",
key.width = "legend.key.width",
Expand Down Expand Up @@ -413,17 +410,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
99 changes: 69 additions & 30 deletions R/guide-legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
#' @param keyheight A numeric or a [grid::unit()] object specifying
#' the height of the legend key. Default value is `legend.key.height` or
#' `legend.key.size` in [theme()].
#' @param key.spacing,key.spacing.x,key.spacing.y A numeric or [grid::unit()]
#' object specifying the distance between key-label pairs in the horizontal
#' direction (`key.spacing.x`), vertical direction (`key.spacing.y`) or both
#' (`key.spacing`).
#' @param direction A character string indicating the direction of the guide.
#' One of "horizontal" or "vertical."
#' @param default.unit A character string indicating [grid::unit()]
Expand Down Expand Up @@ -143,6 +147,9 @@ guide_legend <- function(
# Key size
keywidth = NULL,
keyheight = NULL,
key.spacing = NULL,
key.spacing.x = NULL,
key.spacing.y = NULL,

# General
direction = NULL,
Expand All @@ -156,12 +163,24 @@ guide_legend <- function(
...
) {
# Resolve key sizes
if (!inherits(keywidth, c("NULL", "unit"))) {
if (!(is.null(keywidth) || is.unit(keywidth))) {
keywidth <- unit(keywidth, default.unit)
}
if (!inherits(keyheight, c("NULL", "unit"))) {
if (!(is.null(keyheight) || is.unit(keyheight))) {
keyheight <- unit(keyheight, default.unit)
}

# Resolve spacing
key.spacing.x <- key.spacing.x %||% key.spacing
if (!is.null(key.spacing.x) || is.unit(key.spacing.x)) {
key.spacing.x <- unit(key.spacing.x, default.unit)
}
key.spacing.y <- key.spacing.y %||% key.spacing
if (!is.null(key.spacing.y) || is.unit(key.spacing.y)) {
key.spacing.y <- unit(key.spacing.y, default.unit)
}


if (!is.null(title.position)) {
title.position <- arg_match0(title.position, .trbl)
}
Expand All @@ -187,6 +206,8 @@ guide_legend <- function(
# Key size
keywidth = keywidth,
keyheight = keyheight,
key.spacing.x = key.spacing.x,
key.spacing.y = key.spacing.y,

# General
direction = direction,
Expand Down Expand Up @@ -226,9 +247,10 @@ GuideLegend <- ggproto(

keywidth = NULL,
keyheight = NULL,
key.spacing.x = NULL,
key.spacing.y = NULL,

# General
direction = NULL,
override.aes = list(),
nrow = NULL,
ncol = NULL,
Expand All @@ -249,9 +271,6 @@ GuideLegend <- ggproto(
elements = list(
background = "legend.background",
margin = "legend.margin",
spacing = "legend.spacing",
spacing.x = "legend.spacing.x",
spacing.y = "legend.spacing.y",
key = "legend.key",
key.height = "legend.key.height",
key.width = "legend.key.width",
Expand Down Expand Up @@ -436,13 +455,32 @@ GuideLegend <- ggproto(
elements$text$size %||% 11
gap <- unit(gap * 0.5, "pt")
# Should maybe be elements$spacing.{x/y} instead of the theme's spacing?
elements$hgap <- width_cm( theme$legend.spacing.x %||% gap)
elements$vgap <- height_cm(theme$legend.spacing.y %||% gap)

if (params$direction == "vertical") {
# For backward compatibility, vertical default is no spacing
vgap <- params$key.spacing.y %||% unit(0, "pt")
} else {
vgap <- params$key.spacing.y %||% gap
}

elements$hgap <- width_cm( params$key.spacing.x %||% gap)
elements$vgap <- height_cm(vgap)
elements$padding <- convertUnit(
elements$margin %||% margin(),
"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
}

teunbrand marked this conversation as resolved.
Show resolved Hide resolved
# Evaluate backgrounds early
if (!is.null(elements$background)) {
elements$background <- ggname(
Expand Down Expand Up @@ -527,22 +565,23 @@ 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),
list(pmax(label_widths, widths), hgap * (!byrow))
"left" = list(label_widths, widths, hgap),
"right" = list(widths, label_widths, hgap),
list(pmax(label_widths, widths), hgap)
)
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),
list(pmax(label_heights, heights), vgap * (byrow))
"top" = list(label_heights, heights, vgap),
"bottom" = list(heights, label_heights, vgap),
list(pmax(label_heights, heights), vgap)
)
heights <- head(vec_interleave(!!!heights), -1)

has_title <- !is.zero(grobs$title)

if (has_title) {
# Measure title
title_width <- width_cm(grobs$title)
Expand All @@ -551,14 +590,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 @@ -595,20 +634,20 @@ 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
}
)

Expand All @@ -617,8 +656,8 @@ GuideLegend <- ggproto(
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 @@ -627,8 +666,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
8 changes: 8 additions & 0 deletions man/guide_legend.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading