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

Deprecate legend.title.align and legend.text.align #5362

Merged
merged 7 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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).

Expand Down
4 changes: 1 addition & 3 deletions R/guide-colorbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...) {
Expand Down
10 changes: 3 additions & 7 deletions R/guide-legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
4 changes: 0 additions & 4 deletions R/theme-defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 0 additions & 2 deletions R/theme-elements.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down
32 changes: 26 additions & 6 deletions R/theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 0 additions & 8 deletions man/theme.Rd

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

6 changes: 3 additions & 3 deletions vignettes/articles/faq-customising.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
)
```

</details>
Expand Down
Loading