Skip to content

Commit a747da3

Browse files
authored
Deprecate legend.title.align and legend.text.align (#5362)
* soft-deprecate `legend.text/title.align` * Remove `legend.text/title.align` from element tree * Absolve default themes * Absolve guides * Don't recommend `legend.title.align` * Add news bullet
1 parent af4cc02 commit a747da3

File tree

8 files changed

+38
-33
lines changed

8 files changed

+38
-33
lines changed

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# ggplot2 (development version)
22

3+
* The `legend.text.align` and `legend.title.align` arguments in `theme()` are
4+
deprecated. The `hjust` setting of the `legend.text` and `legend.title`
5+
elements continues to fulfil the role of text alignment (@teunbrand, #5347).
6+
37
* Integers are once again valid input to theme arguments that expect numeric
48
input (@teunbrand, #5369)
59

10+
611
* Nicer error messages for xlim/ylim arguments in coord-* functions
712
(@92amartins, #4601, #5297).
813

R/guide-colorbar.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,7 @@ GuideColourbar <- ggproto(
314314
key.height = "legend.key.height",
315315
key.width = "legend.key.width",
316316
text = "legend.text",
317-
text.align = "legend.text.align",
318-
theme.title = "legend.title",
319-
title.align = "legend.title.align"
317+
theme.title = "legend.title"
320318
),
321319

322320
extract_decor = function(scale, aesthetic, nbin = 300, reverse = FALSE, ...) {

R/guide-legend.R

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,7 @@ GuideLegend <- ggproto(
256256
key.height = "legend.key.height",
257257
key.width = "legend.key.width",
258258
text = "legend.text",
259-
text.align = "legend.text.align",
260-
theme.title = "legend.title",
261-
title.align = "legend.title.align"
259+
theme.title = "legend.title"
262260
),
263261

264262
extract_params = function(scale, params, hashables,
@@ -395,8 +393,7 @@ GuideLegend <- ggproto(
395393

396394
# Title
397395
title <- combine_elements(params$title.theme, elements$theme.title)
398-
title$hjust <- params$title.hjust %||% elements$title.align %||%
399-
title$hjust %||% 0
396+
title$hjust <- params$title.hjust %||% title$hjust %||% 0
400397
title$vjust <- params$title.vjust %||% title$vjust %||% 0.5
401398
elements$title <- title
402399

@@ -421,8 +418,7 @@ GuideLegend <- ggproto(
421418
is.null(theme$legend.text$vjust)) {
422419
label$vjust <- NULL
423420
}
424-
label$hjust <- params$label.hjust %||% elements$text.align %||%
425-
label$hjust %||% hjust
421+
label$hjust <- params$label.hjust %||% label$hjust %||% hjust
426422
label$vjust <- params$label.vjust %||% label$vjust %||% vjust
427423
}
428424
elements$text <- label

R/theme-defaults.R

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,7 @@ theme_grey <- function(base_size = 11, base_family = "",
176176
legend.key.height = NULL,
177177
legend.key.width = NULL,
178178
legend.text = element_text(size = rel(0.8)),
179-
legend.text.align = NULL,
180179
legend.title = element_text(hjust = 0),
181-
legend.title.align = NULL,
182180
legend.position = "right",
183181
legend.direction = NULL,
184182
legend.justification = "center",
@@ -590,9 +588,7 @@ theme_test <- function(base_size = 11, base_family = "",
590588
legend.key.height = NULL,
591589
legend.key.width = NULL,
592590
legend.text = element_text(size = rel(0.8)),
593-
legend.text.align = NULL,
594591
legend.title = element_text(hjust = 0),
595-
legend.title.align = NULL,
596592
legend.position = "right",
597593
legend.direction = NULL,
598594
legend.justification = "center",

R/theme-elements.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,7 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
476476
legend.key.height = el_def("unit", "legend.key.size"),
477477
legend.key.width = el_def("unit", "legend.key.size"),
478478
legend.text = el_def("element_text", "text"),
479-
legend.text.align = el_def(c("numeric", "integer")),
480479
legend.title = el_def("element_text", "title"),
481-
legend.title.align = el_def(c("numeric", "integer")),
482480
legend.position = el_def(c("character", "numeric", "integer")),
483481
legend.direction = el_def("character"),
484482
legend.justification = el_def(c("character", "numeric", "integer")),

R/theme.R

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,8 @@
7171
#' `legend.key.size` or can be specified separately
7272
#' @param legend.text legend item labels ([element_text()]; inherits from
7373
#' `text`)
74-
#' @param legend.text.align alignment of legend labels (number from 0 (left) to
75-
#' 1 (right))
7674
#' @param legend.title title of legend ([element_text()]; inherits from
7775
#' `title`)
78-
#' @param legend.title.align alignment of legend title (number from 0 (left) to
79-
#' 1 (right))
8076
#' @param legend.position the position of legends ("none", "left", "right",
8177
#' "bottom", "top", or two-element numeric vector)
8278
#' @param legend.direction layout of items in legends ("horizontal" or
@@ -330,9 +326,7 @@ theme <- function(line,
330326
legend.key.height,
331327
legend.key.width,
332328
legend.text,
333-
legend.text.align,
334329
legend.title,
335-
legend.title.align,
336330
legend.position,
337331
legend.direction,
338332
legend.justification,
@@ -419,6 +413,32 @@ theme <- function(line,
419413
elements$legend.spacing <- elements$legend.margin
420414
elements$legend.margin <- margin()
421415
}
416+
if (!is.null(elements$legend.title.align)) {
417+
deprecate_soft0(
418+
"3.5.0", "theme(legend.title.align)",
419+
I("theme(legend.title = element_text(hjust))")
420+
)
421+
if (is.null(elements[["legend.title"]])) {
422+
elements$legend.title <- element_text(hjust = elements$legend.title.align)
423+
} else {
424+
elements$legend.title$hjust <- elements$legend.title$hjust %||%
425+
elements$legend.title.align
426+
}
427+
elements$legend.title.align <- NULL
428+
}
429+
if (!is.null(elements$legend.text.align)) {
430+
deprecate_soft0(
431+
"3.5.0", "theme(legend.text.align)",
432+
I("theme(legend.text = element_text(hjust))")
433+
)
434+
if (is.null(elements[["legend.text"]])) {
435+
elements$legend.text <- element_text(hjust = elements$legend.text.align)
436+
} else {
437+
elements$legend.text$hjust <- elements$legend.text$hjust %||%
438+
elements$legend.text.align
439+
}
440+
elements$legend.text.align <- NULL
441+
}
422442

423443
# If complete theme set all non-blank elements to inherit from blanks
424444
if (complete) {

man/theme.Rd

Lines changed: 0 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/articles/faq-customising.Rmd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ ggplot(mpg, aes(x = hwy, y = cty, color = drv)) +
130130
```
131131

132132
Note that the legend title is no longer aligned with the keys with this approach.
133-
You can also shift it over with `legend.title.align`.
133+
You can also shift it over with the `hjust` setting of `legend.title`.
134134

135135
```{r}
136136
#| 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)) +
143143
theme(
144144
legend.key.size = unit(1.5, "cm"),
145145
legend.key = element_rect(color = NA, fill = NA),
146-
legend.title.align = 0.5
147-
)
146+
legend.title = element_text(hjust = 0.5)
147+
)
148148
```
149149

150150
</details>

0 commit comments

Comments
 (0)