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

Themes accept header font family #5887

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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)

* 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 `arrow.fill` parameter is now applied to more line-based functions:
`geom_path()`, `geom_line()`, `geom_step()` `geom_function()`, line
geometries in `geom_sf()` and `element_line()`.
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
4 changes: 2 additions & 2 deletions R/theme-elements.R
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,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
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