From 5030d8baec246ec6befde5297d3c56d91366414f Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Fri, 10 May 2024 09:05:27 +0200 Subject: [PATCH 1/9] new `header_family` theme argument --- R/theme-defaults.R | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/R/theme-defaults.R b/R/theme-defaults.R index 05260557e9..656dda26dc 100644 --- a/R/theme-defaults.R +++ b/R/theme-defaults.R @@ -6,6 +6,8 @@ #' #' @param base_size base font size, given in pts. #' @param base_family base font family +#' @param header_family font family for titles and headers. Defaults to +#' `base_family`. #' @param base_line_size base size for line elements #' @param base_rect_size base size for rect elements #' @@ -101,6 +103,7 @@ NULL #' @export #' @rdname ggtheme theme_grey <- function(base_size = 11, base_family = "", + header_family = base_family, base_line_size = base_size / 22, base_rect_size = base_size / 22) { @@ -134,6 +137,10 @@ theme_grey <- function(base_size = 11, base_family = "", margin = margin(), debug = FALSE ), + title = element_text( + family = header_family + ), + axis.line = element_blank(), axis.line.x = NULL, axis.line.y = NULL, @@ -255,12 +262,14 @@ theme_gray <- theme_grey #' @export #' @rdname ggtheme theme_bw <- function(base_size = 11, base_family = "", + header_family = base_family, 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% @@ -281,6 +290,7 @@ theme_bw <- function(base_size = 11, base_family = "", #' @export #' @rdname ggtheme theme_linedraw <- function(base_size = 11, base_family = "", + header_family = base_family, base_line_size = base_size / 22, base_rect_size = base_size / 22) { half_line <- base_size / 2 @@ -290,6 +300,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% @@ -321,6 +332,7 @@ theme_linedraw <- function(base_size = 11, base_family = "", #' @export #' @rdname ggtheme theme_light <- function(base_size = 11, base_family = "", + header_family = base_family, base_line_size = base_size / 22, base_rect_size = base_size / 22) { half_line <- base_size / 2 @@ -329,6 +341,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% @@ -361,6 +374,7 @@ theme_light <- function(base_size = 11, base_family = "", #' @export #' @rdname ggtheme theme_dark <- function(base_size = 11, base_family = "", + header_family = base_family, base_line_size = base_size / 22, base_rect_size = base_size / 22) { half_line <- base_size / 2 @@ -369,6 +383,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% @@ -399,12 +414,14 @@ theme_dark <- function(base_size = 11, base_family = "", #' @export #' @rdname ggtheme theme_minimal <- function(base_size = 11, base_family = "", + header_family = base_family, 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% @@ -424,11 +441,13 @@ theme_minimal <- function(base_size = 11, base_family = "", #' @export #' @rdname ggtheme theme_classic <- function(base_size = 11, base_family = "", + header_family = base_family, 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% @@ -452,6 +471,7 @@ theme_classic <- function(base_size = 11, base_family = "", #' @export #' @rdname ggtheme theme_void <- function(base_size = 11, base_family = "", + header_family = base_family, base_line_size = base_size / 22, base_rect_size = base_size / 22) { half_line <- base_size / 2 @@ -466,6 +486,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), axis.text = element_blank(), axis.title = element_blank(), axis.ticks.length = unit(0, "pt"), @@ -523,6 +544,7 @@ theme_void <- function(base_size = 11, base_family = "", #' @export #' @rdname ggtheme theme_test <- function(base_size = 11, base_family = "", + header_family = base_family, base_line_size = base_size / 22, base_rect_size = base_size / 22) { half_line <- base_size / 2 @@ -542,6 +564,7 @@ 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), axis.line = element_blank(), axis.line.x = NULL, From 2a6e9b1112b5af4053916f7b82f10cc4d7593dad Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Fri, 10 May 2024 09:05:43 +0200 Subject: [PATCH 2/9] allow new underscore argument for consistency --- tests/testthat/_snaps/prohibited-functions.md | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/tests/testthat/_snaps/prohibited-functions.md b/tests/testthat/_snaps/prohibited-functions.md index 87c26e26a8..cbd0084beb 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" From 2fc6275f5770c27231876cd4e4c2764f698c3b62 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Fri, 10 May 2024 09:11:01 +0200 Subject: [PATCH 3/9] caption and subtitle inherit from root text --- R/theme-elements.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/theme-elements.R b/R/theme-elements.R index 41b989df7e..2e86d5fdd5 100644 --- a/R/theme-elements.R +++ b/R/theme-elements.R @@ -586,8 +586,8 @@ 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.position = el_def(c("character", "numeric", "integer")), # Need to also accept numbers From cc6981c35f3c25fde5c030aab0d853c4d0044cb0 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Fri, 10 May 2024 09:14:32 +0200 Subject: [PATCH 4/9] add news bullets --- NEWS.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/NEWS.md b/NEWS.md index 070c74dd40..c4190147d8 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` and `plot.caption` theme elements now inherit from the + root `text` element instead of the `title` element (#5886). * (Internal) Applying defaults in `geom_sf()` has moved from the internal `sf_grob()` to `GeomSf$use_defaults()` (@teunbrand). * `facet_wrap()` has new options for the `dir` argument to more precisely From 1279eeecc4446170c68bc6d756bd42d808fce525 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Fri, 10 May 2024 09:16:22 +0200 Subject: [PATCH 5/9] add test --- tests/testthat/test-theme.R | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/testthat/test-theme.R b/tests/testthat/test-theme.R index 895d4cf9fc..efe82ab19b 100644 --- a/tests/testthat/test-theme.R +++ b/tests/testthat/test-theme.R @@ -583,6 +583,18 @@ 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") + +}) + # Visual tests ------------------------------------------------------------ test_that("aspect ratio is honored", { From 0810973550087be2284576fb15a07d2a33d9b1e6 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Fri, 10 May 2024 09:30:19 +0200 Subject: [PATCH 6/9] reoxygenate --- man/ggtheme.Rd | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/man/ggtheme.Rd b/man/ggtheme.Rd index b3594db3f2..2ade2388e0 100644 --- a/man/ggtheme.Rd +++ b/man/ggtheme.Rd @@ -16,6 +16,7 @@ theme_grey( base_size = 11, base_family = "", + header_family = base_family, 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 = base_family, 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 = base_family, 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 = base_family, 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 = base_family, 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 = base_family, 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 = base_family, 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 = base_family, 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 = base_family, 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 = base_family, base_line_size = base_size/22, base_rect_size = base_size/22 ) @@ -88,6 +98,9 @@ theme_test( \item{base_family}{base font family} +\item{header_family}{font family for titles and headers. Defaults to +\code{base_family}.} + \item{base_line_size}{base size for line elements} \item{base_rect_size}{base size for rect elements} From 96f386b8f9a3d3cd8f898f6678a739a13f84450a Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 21 May 2024 14:37:37 +0200 Subject: [PATCH 7/9] set the default `header_family` to `NULL` --- R/theme-defaults.R | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/R/theme-defaults.R b/R/theme-defaults.R index 656dda26dc..295398f467 100644 --- a/R/theme-defaults.R +++ b/R/theme-defaults.R @@ -103,7 +103,7 @@ NULL #' @export #' @rdname ggtheme theme_grey <- function(base_size = 11, base_family = "", - header_family = base_family, + header_family = NULL, base_line_size = base_size / 22, base_rect_size = base_size / 22) { @@ -262,7 +262,7 @@ theme_gray <- theme_grey #' @export #' @rdname ggtheme theme_bw <- function(base_size = 11, base_family = "", - header_family = 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 @@ -290,7 +290,7 @@ theme_bw <- function(base_size = 11, base_family = "", #' @export #' @rdname ggtheme theme_linedraw <- function(base_size = 11, base_family = "", - header_family = base_family, + header_family = NULL, base_line_size = base_size / 22, base_rect_size = base_size / 22) { half_line <- base_size / 2 @@ -332,7 +332,7 @@ theme_linedraw <- function(base_size = 11, base_family = "", #' @export #' @rdname ggtheme theme_light <- function(base_size = 11, base_family = "", - header_family = base_family, + header_family = NULL, base_line_size = base_size / 22, base_rect_size = base_size / 22) { half_line <- base_size / 2 @@ -374,7 +374,7 @@ theme_light <- function(base_size = 11, base_family = "", #' @export #' @rdname ggtheme theme_dark <- function(base_size = 11, base_family = "", - header_family = base_family, + header_family = NULL, base_line_size = base_size / 22, base_rect_size = base_size / 22) { half_line <- base_size / 2 @@ -414,7 +414,7 @@ theme_dark <- function(base_size = 11, base_family = "", #' @export #' @rdname ggtheme theme_minimal <- function(base_size = 11, base_family = "", - header_family = 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 @@ -441,7 +441,7 @@ theme_minimal <- function(base_size = 11, base_family = "", #' @export #' @rdname ggtheme theme_classic <- function(base_size = 11, base_family = "", - header_family = base_family, + header_family = NULL, base_line_size = base_size / 22, base_rect_size = base_size / 22) { theme_bw( @@ -471,7 +471,7 @@ theme_classic <- function(base_size = 11, base_family = "", #' @export #' @rdname ggtheme theme_void <- function(base_size = 11, base_family = "", - header_family = base_family, + header_family = NULL, base_line_size = base_size / 22, base_rect_size = base_size / 22) { half_line <- base_size / 2 @@ -544,7 +544,7 @@ theme_void <- function(base_size = 11, base_family = "", #' @export #' @rdname ggtheme theme_test <- function(base_size = 11, base_family = "", - header_family = base_family, + header_family = NULL, base_line_size = base_size / 22, base_rect_size = base_size / 22) { half_line <- base_size / 2 From c827efd7289e50154d80cf758dd43039a78d5aaa Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 21 May 2024 14:41:55 +0200 Subject: [PATCH 8/9] document --- R/theme-defaults.R | 5 +++-- man/ggtheme.Rd | 25 +++++++++++++------------ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/R/theme-defaults.R b/R/theme-defaults.R index 295398f467..dcf0cf6115 100644 --- a/R/theme-defaults.R +++ b/R/theme-defaults.R @@ -6,8 +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. Defaults to -#' `base_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 #' diff --git a/man/ggtheme.Rd b/man/ggtheme.Rd index 2ade2388e0..642319bcc9 100644 --- a/man/ggtheme.Rd +++ b/man/ggtheme.Rd @@ -16,7 +16,7 @@ theme_grey( base_size = 11, base_family = "", - header_family = base_family, + header_family = NULL, base_line_size = base_size/22, base_rect_size = base_size/22 ) @@ -24,7 +24,7 @@ theme_grey( theme_gray( base_size = 11, base_family = "", - header_family = base_family, + header_family = NULL, base_line_size = base_size/22, base_rect_size = base_size/22 ) @@ -32,7 +32,7 @@ theme_gray( theme_bw( base_size = 11, base_family = "", - header_family = base_family, + header_family = NULL, base_line_size = base_size/22, base_rect_size = base_size/22 ) @@ -40,7 +40,7 @@ theme_bw( theme_linedraw( base_size = 11, base_family = "", - header_family = base_family, + header_family = NULL, base_line_size = base_size/22, base_rect_size = base_size/22 ) @@ -48,7 +48,7 @@ theme_linedraw( theme_light( base_size = 11, base_family = "", - header_family = base_family, + header_family = NULL, base_line_size = base_size/22, base_rect_size = base_size/22 ) @@ -56,7 +56,7 @@ theme_light( theme_dark( base_size = 11, base_family = "", - header_family = base_family, + header_family = NULL, base_line_size = base_size/22, base_rect_size = base_size/22 ) @@ -64,7 +64,7 @@ theme_dark( theme_minimal( base_size = 11, base_family = "", - header_family = base_family, + header_family = NULL, base_line_size = base_size/22, base_rect_size = base_size/22 ) @@ -72,7 +72,7 @@ theme_minimal( theme_classic( base_size = 11, base_family = "", - header_family = base_family, + header_family = NULL, base_line_size = base_size/22, base_rect_size = base_size/22 ) @@ -80,7 +80,7 @@ theme_classic( theme_void( base_size = 11, base_family = "", - header_family = base_family, + header_family = NULL, base_line_size = base_size/22, base_rect_size = base_size/22 ) @@ -88,7 +88,7 @@ theme_void( theme_test( base_size = 11, base_family = "", - header_family = base_family, + header_family = NULL, base_line_size = base_size/22, base_rect_size = base_size/22 ) @@ -98,8 +98,9 @@ theme_test( \item{base_family}{base font family} -\item{header_family}{font family for titles and headers. Defaults to -\code{base_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} From 1c645c4679db5a9201b37856a60ec275a9600bfc Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Thu, 11 Jul 2024 16:23:33 +0200 Subject: [PATCH 9/9] `plot.tag` inherits from root `text` element --- NEWS.md | 4 ++-- R/theme-elements.R | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index 3477ea08c5..89f06dfb20 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,8 +2,8 @@ * Themes gain an additional `header_family` argument to easily set the font for headers and titles (#5886). -* The `plot.subtitle` and `plot.caption` theme elements now inherit from the - root `text` element instead of the `title` element (#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-elements.R b/R/theme-elements.R index f888ac0be0..4a0bfce774 100644 --- a/R/theme-elements.R +++ b/R/theme-elements.R @@ -603,7 +603,7 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) { 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"),