Skip to content

Commit

Permalink
Themes accept header font family (#5887)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
teunbrand authored Jul 11, 2024
1 parent 76bb2cd commit 096b966
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 14 deletions.
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)

* 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).
Expand Down
24 changes: 23 additions & 1 deletion R/theme-defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
#'
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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),

Expand Down Expand Up @@ -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%
Expand All @@ -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
Expand All @@ -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%
Expand Down Expand Up @@ -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
Expand All @@ -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%
Expand Down Expand Up @@ -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
Expand All @@ -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%
Expand Down Expand Up @@ -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%
Expand All @@ -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%
Expand All @@ -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
Expand All @@ -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(),
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions R/theme-elements.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
14 changes: 14 additions & 0 deletions man/ggtheme.Rd

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

30 changes: 20 additions & 10 deletions tests/testthat/_snaps/prohibited-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 096b966

Please sign in to comment.