Skip to content

Commit 096b966

Browse files
authored
Themes accept header font family (#5887)
* new `header_family` theme argument * allow new underscore argument for consistency * caption and subtitle inherit from root text * add news bullets * add test * reoxygenate * set the default `header_family` to `NULL` * document * `plot.tag` inherits from root `text` element
1 parent 76bb2cd commit 096b966

File tree

6 files changed

+75
-14
lines changed

6 files changed

+75
-14
lines changed

NEWS.md

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

3+
* Themes gain an additional `header_family` argument to easily set the font
4+
for headers and titles (#5886).
5+
* The `plot.subtitle`, `plot.caption` and `plot.tag` theme elements now inherit
6+
from the root `text` element instead of the `title` element (#5886).
37
* ggplot2 no longer imports {glue} (@teunbrand, #5986).
48
* `geom_rect()` can now derive the required corners positions from `x`/`width`
59
or `y`/`height` parameterisation (@teunbrand, #5861).

R/theme-defaults.R

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#'
77
#' @param base_size base font size, given in pts.
88
#' @param base_family base font family
9+
#' @param header_family font family for titles and headers. The default, `NULL`,
10+
#' uses theme inheritance to set the font. This setting affects axis titles,
11+
#' legend titles, the plot title and tag text.
912
#' @param base_line_size base size for line elements
1013
#' @param base_rect_size base size for rect elements
1114
#'
@@ -101,6 +104,7 @@ NULL
101104
#' @export
102105
#' @rdname ggtheme
103106
theme_grey <- function(base_size = 11, base_family = "",
107+
header_family = NULL,
104108
base_line_size = base_size / 22,
105109
base_rect_size = base_size / 22) {
106110

@@ -133,6 +137,9 @@ theme_grey <- function(base_size = 11, base_family = "",
133137
lineheight = 0.9, hjust = 0.5, vjust = 0.5, angle = 0,
134138
margin = margin(), debug = FALSE
135139
),
140+
141+
title = element_text(family = header_family),
142+
136143
spacing = unit(half_line, "pt"),
137144
margins = margin(half_line, half_line, half_line, half_line),
138145

@@ -257,12 +264,14 @@ theme_gray <- theme_grey
257264
#' @export
258265
#' @rdname ggtheme
259266
theme_bw <- function(base_size = 11, base_family = "",
267+
header_family = NULL,
260268
base_line_size = base_size / 22,
261269
base_rect_size = base_size / 22) {
262270
# Starts with theme_grey and then modify some parts
263271
theme_grey(
264272
base_size = base_size,
265273
base_family = base_family,
274+
header_family = header_family,
266275
base_line_size = base_line_size,
267276
base_rect_size = base_rect_size
268277
) %+replace%
@@ -283,6 +292,7 @@ theme_bw <- function(base_size = 11, base_family = "",
283292
#' @export
284293
#' @rdname ggtheme
285294
theme_linedraw <- function(base_size = 11, base_family = "",
295+
header_family = NULL,
286296
base_line_size = base_size / 22,
287297
base_rect_size = base_size / 22) {
288298
half_line <- base_size / 2
@@ -292,6 +302,7 @@ theme_linedraw <- function(base_size = 11, base_family = "",
292302
theme_bw(
293303
base_size = base_size,
294304
base_family = base_family,
305+
header_family = header_family,
295306
base_line_size = base_line_size,
296307
base_rect_size = base_rect_size
297308
) %+replace%
@@ -323,6 +334,7 @@ theme_linedraw <- function(base_size = 11, base_family = "",
323334
#' @export
324335
#' @rdname ggtheme
325336
theme_light <- function(base_size = 11, base_family = "",
337+
header_family = NULL,
326338
base_line_size = base_size / 22,
327339
base_rect_size = base_size / 22) {
328340
half_line <- base_size / 2
@@ -331,6 +343,7 @@ theme_light <- function(base_size = 11, base_family = "",
331343
theme_grey(
332344
base_size = base_size,
333345
base_family = base_family,
346+
header_family = header_family,
334347
base_line_size = base_line_size,
335348
base_rect_size = base_rect_size
336349
) %+replace%
@@ -363,6 +376,7 @@ theme_light <- function(base_size = 11, base_family = "",
363376
#' @export
364377
#' @rdname ggtheme
365378
theme_dark <- function(base_size = 11, base_family = "",
379+
header_family = NULL,
366380
base_line_size = base_size / 22,
367381
base_rect_size = base_size / 22) {
368382
half_line <- base_size / 2
@@ -371,6 +385,7 @@ theme_dark <- function(base_size = 11, base_family = "",
371385
theme_grey(
372386
base_size = base_size,
373387
base_family = base_family,
388+
header_family = header_family,
374389
base_line_size = base_line_size,
375390
base_rect_size = base_rect_size
376391
) %+replace%
@@ -401,12 +416,14 @@ theme_dark <- function(base_size = 11, base_family = "",
401416
#' @export
402417
#' @rdname ggtheme
403418
theme_minimal <- function(base_size = 11, base_family = "",
419+
header_family = NULL,
404420
base_line_size = base_size / 22,
405421
base_rect_size = base_size / 22) {
406422
# Starts with theme_bw and remove most parts
407423
theme_bw(
408424
base_size = base_size,
409425
base_family = base_family,
426+
header_family = header_family,
410427
base_line_size = base_line_size,
411428
base_rect_size = base_rect_size
412429
) %+replace%
@@ -426,11 +443,13 @@ theme_minimal <- function(base_size = 11, base_family = "",
426443
#' @export
427444
#' @rdname ggtheme
428445
theme_classic <- function(base_size = 11, base_family = "",
446+
header_family = NULL,
429447
base_line_size = base_size / 22,
430448
base_rect_size = base_size / 22) {
431449
theme_bw(
432450
base_size = base_size,
433451
base_family = base_family,
452+
header_family = header_family,
434453
base_line_size = base_line_size,
435454
base_rect_size = base_rect_size
436455
) %+replace%
@@ -454,6 +473,7 @@ theme_classic <- function(base_size = 11, base_family = "",
454473
#' @export
455474
#' @rdname ggtheme
456475
theme_void <- function(base_size = 11, base_family = "",
476+
header_family = NULL,
457477
base_line_size = base_size / 22,
458478
base_rect_size = base_size / 22) {
459479
half_line <- base_size / 2
@@ -468,6 +488,7 @@ theme_void <- function(base_size = 11, base_family = "",
468488
lineheight = 0.9, hjust = 0.5, vjust = 0.5, angle = 0,
469489
margin = margin(), debug = FALSE
470490
),
491+
title = element_text(family = header_family),
471492
spacing = unit(half_line, "pt"),
472493
margins = margin(half_line, half_line, half_line, half_line),
473494
axis.text = element_blank(),
@@ -530,6 +551,7 @@ theme_void <- function(base_size = 11, base_family = "",
530551
#' @export
531552
#' @rdname ggtheme
532553
theme_test <- function(base_size = 11, base_family = "",
554+
header_family = NULL,
533555
base_line_size = base_size / 22,
534556
base_rect_size = base_size / 22) {
535557
half_line <- base_size / 2
@@ -549,9 +571,9 @@ theme_test <- function(base_size = 11, base_family = "",
549571
lineheight = 0.9, hjust = 0.5, vjust = 0.5, angle = 0,
550572
margin = margin(), debug = FALSE
551573
),
574+
title = element_text(family = header_family),
552575
spacing = unit(half_line, "pt"),
553576
margins = margin(half_line, half_line, half_line, half_line),
554-
555577
axis.line = element_blank(),
556578
axis.line.x = NULL,
557579
axis.line.y = NULL,

R/theme-elements.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -600,10 +600,10 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
600600
plot.background = el_def("element_rect", "rect"),
601601
plot.title = el_def("element_text", "title"),
602602
plot.title.position = el_def("character"),
603-
plot.subtitle = el_def("element_text", "title"),
604-
plot.caption = el_def("element_text", "title"),
603+
plot.subtitle = el_def("element_text", "text"),
604+
plot.caption = el_def("element_text", "text"),
605605
plot.caption.position = el_def("character"),
606-
plot.tag = el_def("element_text", "title"),
606+
plot.tag = el_def("element_text", "text"),
607607
plot.tag.position = el_def(c("character", "numeric", "integer")), # Need to also accept numbers
608608
plot.tag.location = el_def("character"),
609609
plot.margin = el_def(c("margin", "unit", "rel"), "margins"),

man/ggtheme.Rd

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

tests/testthat/_snaps/prohibited-functions.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,34 +184,44 @@
184184
[1] "contour_var"
185185
186186
$theme_bw
187-
[1] "base_size" "base_family" "base_line_size" "base_rect_size"
187+
[1] "base_size" "base_family" "header_family" "base_line_size"
188+
[5] "base_rect_size"
188189
189190
$theme_classic
190-
[1] "base_size" "base_family" "base_line_size" "base_rect_size"
191+
[1] "base_size" "base_family" "header_family" "base_line_size"
192+
[5] "base_rect_size"
191193
192194
$theme_dark
193-
[1] "base_size" "base_family" "base_line_size" "base_rect_size"
195+
[1] "base_size" "base_family" "header_family" "base_line_size"
196+
[5] "base_rect_size"
194197
195198
$theme_gray
196-
[1] "base_size" "base_family" "base_line_size" "base_rect_size"
199+
[1] "base_size" "base_family" "header_family" "base_line_size"
200+
[5] "base_rect_size"
197201
198202
$theme_grey
199-
[1] "base_size" "base_family" "base_line_size" "base_rect_size"
203+
[1] "base_size" "base_family" "header_family" "base_line_size"
204+
[5] "base_rect_size"
200205
201206
$theme_light
202-
[1] "base_size" "base_family" "base_line_size" "base_rect_size"
207+
[1] "base_size" "base_family" "header_family" "base_line_size"
208+
[5] "base_rect_size"
203209
204210
$theme_linedraw
205-
[1] "base_size" "base_family" "base_line_size" "base_rect_size"
211+
[1] "base_size" "base_family" "header_family" "base_line_size"
212+
[5] "base_rect_size"
206213
207214
$theme_minimal
208-
[1] "base_size" "base_family" "base_line_size" "base_rect_size"
215+
[1] "base_size" "base_family" "header_family" "base_line_size"
216+
[5] "base_rect_size"
209217
210218
$theme_test
211-
[1] "base_size" "base_family" "base_line_size" "base_rect_size"
219+
[1] "base_size" "base_family" "header_family" "base_line_size"
220+
[5] "base_rect_size"
212221
213222
$theme_void
214-
[1] "base_size" "base_family" "base_line_size" "base_rect_size"
223+
[1] "base_size" "base_family" "header_family" "base_line_size"
224+
[5] "base_rect_size"
215225
216226
$transform_position
217227
[1] "trans_x" "trans_y"

tests/testthat/test-theme.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,17 @@ test_that("Minor tick length supports biparental inheritance", {
583583
)
584584
})
585585

586+
test_that("header_family is passed on correctly", {
587+
588+
td <- theme_dark(base_family = "x", header_family = "y")
589+
590+
test <- calc_element("plot.title", td)
591+
expect_equal(test$family, "y")
592+
593+
test <- calc_element("plot.subtitle", td)
594+
expect_equal(test$family, "x")
595+
})
596+
586597
test_that("complete_theme completes a theme", {
587598
# `NULL` should match default
588599
gray <- theme_gray()

0 commit comments

Comments
 (0)