From 50df8cccb7665e7eedb011d053b129b09da75901 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 24 Jul 2023 20:30:04 +0200 Subject: [PATCH 1/6] soft-deprecate `legend.text/title.align` --- R/theme.R | 32 ++++++++++++++++++++++++++------ man/theme.Rd | 8 -------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/R/theme.R b/R/theme.R index 086c35174e..2bfdd5a835 100644 --- a/R/theme.R +++ b/R/theme.R @@ -71,12 +71,8 @@ #' `legend.key.size` or can be specified separately #' @param legend.text legend item labels ([element_text()]; inherits from #' `text`) -#' @param legend.text.align alignment of legend labels (number from 0 (left) to -#' 1 (right)) #' @param legend.title title of legend ([element_text()]; inherits from #' `title`) -#' @param legend.title.align alignment of legend title (number from 0 (left) to -#' 1 (right)) #' @param legend.position the position of legends ("none", "left", "right", #' "bottom", "top", or two-element numeric vector) #' @param legend.direction layout of items in legends ("horizontal" or @@ -330,9 +326,7 @@ theme <- function(line, legend.key.height, legend.key.width, legend.text, - legend.text.align, legend.title, - legend.title.align, legend.position, legend.direction, legend.justification, @@ -419,6 +413,32 @@ theme <- function(line, elements$legend.spacing <- elements$legend.margin elements$legend.margin <- margin() } + if (!is.null(elements$legend.title.align)) { + deprecate_soft0( + "3.5.0", "theme(legend.title.align)", + I("theme(legend.title = element_text(hjust))") + ) + if (is.null(elements[["legend.title"]])) { + elements$legend.title <- element_text(hjust = elements$legend.title.align) + } else { + elements$legend.title$hjust <- elements$legend.title$hjust %||% + elements$legend.title.align + } + elements$legend.title.align <- NULL + } + if (!is.null(elements$legend.text.align)) { + deprecate_soft0( + "3.5.0", "theme(legend.text.align)", + I("theme(legend.text = element_text(hjust))") + ) + if (is.null(elements[["legend.text"]])) { + elements$legend.text <- element_text(hjust = elements$legend.text.align) + } else { + elements$legend.text$hjust <- elements$legend.text$hjust %||% + elements$legend.text.align + } + elements$legend.text.align <- NULL + } # If complete theme set all non-blank elements to inherit from blanks if (complete) { diff --git a/man/theme.Rd b/man/theme.Rd index 29cfe9f9b3..e433fe7206 100644 --- a/man/theme.Rd +++ b/man/theme.Rd @@ -55,9 +55,7 @@ theme( legend.key.height, legend.key.width, legend.text, - legend.text.align, legend.title, - legend.title.align, legend.position, legend.direction, legend.justification, @@ -167,15 +165,9 @@ inherits from \code{rect})} \item{legend.text}{legend item labels (\code{\link[=element_text]{element_text()}}; inherits from \code{text})} -\item{legend.text.align}{alignment of legend labels (number from 0 (left) to -1 (right))} - \item{legend.title}{title of legend (\code{\link[=element_text]{element_text()}}; inherits from \code{title})} -\item{legend.title.align}{alignment of legend title (number from 0 (left) to -1 (right))} - \item{legend.position}{the position of legends ("none", "left", "right", "bottom", "top", or two-element numeric vector)} From 725479af7232eb7ece804a9f99c48663476949d8 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 24 Jul 2023 20:30:35 +0200 Subject: [PATCH 2/6] Remove `legend.text/title.align` from element tree --- R/theme-elements.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/R/theme-elements.R b/R/theme-elements.R index ab8624a761..e2a482f937 100644 --- a/R/theme-elements.R +++ b/R/theme-elements.R @@ -476,9 +476,7 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) { legend.key.height = el_def("unit", "legend.key.size"), legend.key.width = el_def("unit", "legend.key.size"), legend.text = el_def("element_text", "text"), - legend.text.align = el_def("numeric"), legend.title = el_def("element_text", "title"), - legend.title.align = el_def("numeric"), legend.position = el_def(c("character", "numeric")), legend.direction = el_def("character"), legend.justification = el_def(c("character", "numeric")), From 763ed789c46f395deab5467e5ca48a7d5dd6acb1 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 24 Jul 2023 20:30:55 +0200 Subject: [PATCH 3/6] Absolve default themes --- R/theme-defaults.R | 4 ---- 1 file changed, 4 deletions(-) diff --git a/R/theme-defaults.R b/R/theme-defaults.R index 5c1018e85f..fd565307bb 100644 --- a/R/theme-defaults.R +++ b/R/theme-defaults.R @@ -176,9 +176,7 @@ theme_grey <- function(base_size = 11, base_family = "", legend.key.height = NULL, legend.key.width = NULL, legend.text = element_text(size = rel(0.8)), - legend.text.align = NULL, legend.title = element_text(hjust = 0), - legend.title.align = NULL, legend.position = "right", legend.direction = NULL, legend.justification = "center", @@ -590,9 +588,7 @@ theme_test <- function(base_size = 11, base_family = "", legend.key.height = NULL, legend.key.width = NULL, legend.text = element_text(size = rel(0.8)), - legend.text.align = NULL, legend.title = element_text(hjust = 0), - legend.title.align = NULL, legend.position = "right", legend.direction = NULL, legend.justification = "center", From a6b183c119fd98e3835cabe3fa4e6c0c5981331a Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 24 Jul 2023 20:31:07 +0200 Subject: [PATCH 4/6] Absolve guides --- R/guide-colorbar.R | 4 +--- R/guide-legend.R | 10 +++------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/R/guide-colorbar.R b/R/guide-colorbar.R index 65291f37cc..b7ffdb9abf 100644 --- a/R/guide-colorbar.R +++ b/R/guide-colorbar.R @@ -314,9 +314,7 @@ GuideColourbar <- ggproto( key.height = "legend.key.height", key.width = "legend.key.width", text = "legend.text", - text.align = "legend.text.align", - theme.title = "legend.title", - title.align = "legend.title.align" + theme.title = "legend.title" ), extract_decor = function(scale, aesthetic, nbin = 300, reverse = FALSE, ...) { diff --git a/R/guide-legend.R b/R/guide-legend.R index 0e6193aa24..2ac35f05b8 100644 --- a/R/guide-legend.R +++ b/R/guide-legend.R @@ -256,9 +256,7 @@ GuideLegend <- ggproto( key.height = "legend.key.height", key.width = "legend.key.width", text = "legend.text", - text.align = "legend.text.align", - theme.title = "legend.title", - title.align = "legend.title.align" + theme.title = "legend.title" ), extract_params = function(scale, params, hashables, @@ -395,8 +393,7 @@ GuideLegend <- ggproto( # Title title <- combine_elements(params$title.theme, elements$theme.title) - title$hjust <- params$title.hjust %||% elements$title.align %||% - title$hjust %||% 0 + title$hjust <- params$title.hjust %||% title$hjust %||% 0 title$vjust <- params$title.vjust %||% title$vjust %||% 0.5 elements$title <- title @@ -421,8 +418,7 @@ GuideLegend <- ggproto( is.null(theme$legend.text$vjust)) { label$vjust <- NULL } - label$hjust <- params$label.hjust %||% elements$text.align %||% - label$hjust %||% hjust + label$hjust <- params$label.hjust %||% label$hjust %||% hjust label$vjust <- params$label.vjust %||% label$vjust %||% vjust } elements$text <- label From 8e6952c35820a6b1f104de239d57aed342965c80 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 24 Jul 2023 20:31:28 +0200 Subject: [PATCH 5/6] Don't recommend `legend.title.align` --- vignettes/articles/faq-customising.Rmd | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vignettes/articles/faq-customising.Rmd b/vignettes/articles/faq-customising.Rmd index 3ae048e4fd..bf9cbef6d2 100644 --- a/vignettes/articles/faq-customising.Rmd +++ b/vignettes/articles/faq-customising.Rmd @@ -130,7 +130,7 @@ ggplot(mpg, aes(x = hwy, y = cty, color = drv)) + ``` Note that the legend title is no longer aligned with the keys with this approach. -You can also shift it over with `legend.title.align`. +You can also shift it over with the `hjust` setting of `legend.title`. ```{r} #| fig.alt = "A scatter plot showing the highway miles per gallon on the x-axis @@ -143,8 +143,8 @@ ggplot(mpg, aes(x = hwy, y = cty, color = drv)) + theme( legend.key.size = unit(1.5, "cm"), legend.key = element_rect(color = NA, fill = NA), - legend.title.align = 0.5 - ) + legend.title = element_text(hjust = 0.5) + ) ``` From a12c789c8d9ec01a56527b869744e151dcdb743c Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 24 Jul 2023 20:36:54 +0200 Subject: [PATCH 6/6] Add news bullet --- NEWS.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/NEWS.md b/NEWS.md index 6fb136dcfc..dd2ffc425e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # ggplot2 (development version) +* The `legend.text.align` and `legend.title.align` arguments in `theme()` are + deprecated. The `hjust` setting of the `legend.text` and `legend.title` + elements continues to fulfil the role of text alignment (@teunbrand, #5347). + * Nicer error messages for xlim/ylim arguments in coord-* functions (@92amartins, #4601, #5297).