diff --git a/NEWS.md b/NEWS.md index 381ba4c097..89f06dfb20 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # ggplot2 (development version) +* Themes gain an additional `header_family` argument to easily set the font + for headers and titles (#5886). +* The `plot.subtitle`, `plot.caption` and `plot.tag` theme elements now inherit + from the root `text` element instead of the `title` element (#5886). * ggplot2 no longer imports {glue} (@teunbrand, #5986). * `geom_rect()` can now derive the required corners positions from `x`/`width` or `y`/`height` parameterisation (@teunbrand, #5861). diff --git a/R/theme-defaults.R b/R/theme-defaults.R index 6ecd6d68c0..951f20a01c 100644 --- a/R/theme-defaults.R +++ b/R/theme-defaults.R @@ -6,6 +6,9 @@ #' #' @param base_size base font size, given in pts. #' @param base_family base font family +#' @param header_family font family for titles and headers. The default, `NULL`, +#' uses theme inheritance to set the font. This setting affects axis titles, +#' legend titles, the plot title and tag text. #' @param base_line_size base size for line elements #' @param base_rect_size base size for rect elements #' @@ -101,6 +104,7 @@ NULL #' @export #' @rdname ggtheme theme_grey <- function(base_size = 11, base_family = "", + header_family = NULL, base_line_size = base_size / 22, base_rect_size = base_size / 22) { @@ -133,6 +137,9 @@ theme_grey <- function(base_size = 11, base_family = "", lineheight = 0.9, hjust = 0.5, vjust = 0.5, angle = 0, margin = margin(), debug = FALSE ), + + title = element_text(family = header_family), + spacing = unit(half_line, "pt"), margins = margin(half_line, half_line, half_line, half_line), @@ -257,12 +264,14 @@ theme_gray <- theme_grey #' @export #' @rdname ggtheme theme_bw <- function(base_size = 11, base_family = "", + header_family = NULL, base_line_size = base_size / 22, base_rect_size = base_size / 22) { # Starts with theme_grey and then modify some parts theme_grey( base_size = base_size, base_family = base_family, + header_family = header_family, base_line_size = base_line_size, base_rect_size = base_rect_size ) %+replace% @@ -283,6 +292,7 @@ theme_bw <- function(base_size = 11, base_family = "", #' @export #' @rdname ggtheme theme_linedraw <- function(base_size = 11, base_family = "", + header_family = NULL, base_line_size = base_size / 22, base_rect_size = base_size / 22) { half_line <- base_size / 2 @@ -292,6 +302,7 @@ theme_linedraw <- function(base_size = 11, base_family = "", theme_bw( base_size = base_size, base_family = base_family, + header_family = header_family, base_line_size = base_line_size, base_rect_size = base_rect_size ) %+replace% @@ -323,6 +334,7 @@ theme_linedraw <- function(base_size = 11, base_family = "", #' @export #' @rdname ggtheme theme_light <- function(base_size = 11, base_family = "", + header_family = NULL, base_line_size = base_size / 22, base_rect_size = base_size / 22) { half_line <- base_size / 2 @@ -331,6 +343,7 @@ theme_light <- function(base_size = 11, base_family = "", theme_grey( base_size = base_size, base_family = base_family, + header_family = header_family, base_line_size = base_line_size, base_rect_size = base_rect_size ) %+replace% @@ -363,6 +376,7 @@ theme_light <- function(base_size = 11, base_family = "", #' @export #' @rdname ggtheme theme_dark <- function(base_size = 11, base_family = "", + header_family = NULL, base_line_size = base_size / 22, base_rect_size = base_size / 22) { half_line <- base_size / 2 @@ -371,6 +385,7 @@ theme_dark <- function(base_size = 11, base_family = "", theme_grey( base_size = base_size, base_family = base_family, + header_family = header_family, base_line_size = base_line_size, base_rect_size = base_rect_size ) %+replace% @@ -401,12 +416,14 @@ theme_dark <- function(base_size = 11, base_family = "", #' @export #' @rdname ggtheme theme_minimal <- function(base_size = 11, base_family = "", + header_family = NULL, base_line_size = base_size / 22, base_rect_size = base_size / 22) { # Starts with theme_bw and remove most parts theme_bw( base_size = base_size, base_family = base_family, + header_family = header_family, base_line_size = base_line_size, base_rect_size = base_rect_size ) %+replace% @@ -426,11 +443,13 @@ theme_minimal <- function(base_size = 11, base_family = "", #' @export #' @rdname ggtheme theme_classic <- function(base_size = 11, base_family = "", + header_family = NULL, base_line_size = base_size / 22, base_rect_size = base_size / 22) { theme_bw( base_size = base_size, base_family = base_family, + header_family = header_family, base_line_size = base_line_size, base_rect_size = base_rect_size ) %+replace% @@ -454,6 +473,7 @@ theme_classic <- function(base_size = 11, base_family = "", #' @export #' @rdname ggtheme theme_void <- function(base_size = 11, base_family = "", + header_family = NULL, base_line_size = base_size / 22, base_rect_size = base_size / 22) { half_line <- base_size / 2 @@ -468,6 +488,7 @@ theme_void <- function(base_size = 11, base_family = "", lineheight = 0.9, hjust = 0.5, vjust = 0.5, angle = 0, margin = margin(), debug = FALSE ), + title = element_text(family = header_family), spacing = unit(half_line, "pt"), margins = margin(half_line, half_line, half_line, half_line), axis.text = element_blank(), @@ -530,6 +551,7 @@ theme_void <- function(base_size = 11, base_family = "", #' @export #' @rdname ggtheme theme_test <- function(base_size = 11, base_family = "", + header_family = NULL, base_line_size = base_size / 22, base_rect_size = base_size / 22) { half_line <- base_size / 2 @@ -549,9 +571,9 @@ theme_test <- function(base_size = 11, base_family = "", lineheight = 0.9, hjust = 0.5, vjust = 0.5, angle = 0, margin = margin(), debug = FALSE ), + title = element_text(family = header_family), spacing = unit(half_line, "pt"), margins = margin(half_line, half_line, half_line, half_line), - axis.line = element_blank(), axis.line.x = NULL, axis.line.y = NULL, diff --git a/R/theme-elements.R b/R/theme-elements.R index bf3c693f3b..4a0bfce774 100644 --- a/R/theme-elements.R +++ b/R/theme-elements.R @@ -600,10 +600,10 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) { plot.background = el_def("element_rect", "rect"), plot.title = el_def("element_text", "title"), plot.title.position = el_def("character"), - plot.subtitle = el_def("element_text", "title"), - plot.caption = el_def("element_text", "title"), + plot.subtitle = el_def("element_text", "text"), + plot.caption = el_def("element_text", "text"), plot.caption.position = el_def("character"), - plot.tag = el_def("element_text", "title"), + plot.tag = el_def("element_text", "text"), plot.tag.position = el_def(c("character", "numeric", "integer")), # Need to also accept numbers plot.tag.location = el_def("character"), plot.margin = el_def(c("margin", "unit", "rel"), "margins"), diff --git a/man/ggtheme.Rd b/man/ggtheme.Rd index b3594db3f2..642319bcc9 100644 --- a/man/ggtheme.Rd +++ b/man/ggtheme.Rd @@ -16,6 +16,7 @@ theme_grey( base_size = 11, base_family = "", + header_family = NULL, base_line_size = base_size/22, base_rect_size = base_size/22 ) @@ -23,6 +24,7 @@ theme_grey( theme_gray( base_size = 11, base_family = "", + header_family = NULL, base_line_size = base_size/22, base_rect_size = base_size/22 ) @@ -30,6 +32,7 @@ theme_gray( theme_bw( base_size = 11, base_family = "", + header_family = NULL, base_line_size = base_size/22, base_rect_size = base_size/22 ) @@ -37,6 +40,7 @@ theme_bw( theme_linedraw( base_size = 11, base_family = "", + header_family = NULL, base_line_size = base_size/22, base_rect_size = base_size/22 ) @@ -44,6 +48,7 @@ theme_linedraw( theme_light( base_size = 11, base_family = "", + header_family = NULL, base_line_size = base_size/22, base_rect_size = base_size/22 ) @@ -51,6 +56,7 @@ theme_light( theme_dark( base_size = 11, base_family = "", + header_family = NULL, base_line_size = base_size/22, base_rect_size = base_size/22 ) @@ -58,6 +64,7 @@ theme_dark( theme_minimal( base_size = 11, base_family = "", + header_family = NULL, base_line_size = base_size/22, base_rect_size = base_size/22 ) @@ -65,6 +72,7 @@ theme_minimal( theme_classic( base_size = 11, base_family = "", + header_family = NULL, base_line_size = base_size/22, base_rect_size = base_size/22 ) @@ -72,6 +80,7 @@ theme_classic( theme_void( base_size = 11, base_family = "", + header_family = NULL, base_line_size = base_size/22, base_rect_size = base_size/22 ) @@ -79,6 +88,7 @@ theme_void( theme_test( base_size = 11, base_family = "", + header_family = NULL, base_line_size = base_size/22, base_rect_size = base_size/22 ) @@ -88,6 +98,10 @@ theme_test( \item{base_family}{base font family} +\item{header_family}{font family for titles and headers. The default, \code{NULL}, +uses theme inheritance to set the font. This setting affects axis titles, +legend titles, the plot title and tag text.} + \item{base_line_size}{base size for line elements} \item{base_rect_size}{base size for rect elements} diff --git a/tests/testthat/_snaps/prohibited-functions.md b/tests/testthat/_snaps/prohibited-functions.md index f2aa9bf4d2..afc77c3c60 100644 --- a/tests/testthat/_snaps/prohibited-functions.md +++ b/tests/testthat/_snaps/prohibited-functions.md @@ -184,34 +184,44 @@ [1] "contour_var" $theme_bw - [1] "base_size" "base_family" "base_line_size" "base_rect_size" + [1] "base_size" "base_family" "header_family" "base_line_size" + [5] "base_rect_size" $theme_classic - [1] "base_size" "base_family" "base_line_size" "base_rect_size" + [1] "base_size" "base_family" "header_family" "base_line_size" + [5] "base_rect_size" $theme_dark - [1] "base_size" "base_family" "base_line_size" "base_rect_size" + [1] "base_size" "base_family" "header_family" "base_line_size" + [5] "base_rect_size" $theme_gray - [1] "base_size" "base_family" "base_line_size" "base_rect_size" + [1] "base_size" "base_family" "header_family" "base_line_size" + [5] "base_rect_size" $theme_grey - [1] "base_size" "base_family" "base_line_size" "base_rect_size" + [1] "base_size" "base_family" "header_family" "base_line_size" + [5] "base_rect_size" $theme_light - [1] "base_size" "base_family" "base_line_size" "base_rect_size" + [1] "base_size" "base_family" "header_family" "base_line_size" + [5] "base_rect_size" $theme_linedraw - [1] "base_size" "base_family" "base_line_size" "base_rect_size" + [1] "base_size" "base_family" "header_family" "base_line_size" + [5] "base_rect_size" $theme_minimal - [1] "base_size" "base_family" "base_line_size" "base_rect_size" + [1] "base_size" "base_family" "header_family" "base_line_size" + [5] "base_rect_size" $theme_test - [1] "base_size" "base_family" "base_line_size" "base_rect_size" + [1] "base_size" "base_family" "header_family" "base_line_size" + [5] "base_rect_size" $theme_void - [1] "base_size" "base_family" "base_line_size" "base_rect_size" + [1] "base_size" "base_family" "header_family" "base_line_size" + [5] "base_rect_size" $transform_position [1] "trans_x" "trans_y" diff --git a/tests/testthat/test-theme.R b/tests/testthat/test-theme.R index b201602963..a9568cfd72 100644 --- a/tests/testthat/test-theme.R +++ b/tests/testthat/test-theme.R @@ -583,6 +583,17 @@ test_that("Minor tick length supports biparental inheritance", { ) }) +test_that("header_family is passed on correctly", { + + td <- theme_dark(base_family = "x", header_family = "y") + + test <- calc_element("plot.title", td) + expect_equal(test$family, "y") + + test <- calc_element("plot.subtitle", td) + expect_equal(test$family, "x") +}) + test_that("complete_theme completes a theme", { # `NULL` should match default gray <- theme_gray()