Skip to content

Commit 7e6d125

Browse files
authored
update theme documentation reference links & clarify differences between theme_set and theme_update() (#3782)
* add complete themes reference link to theme docs, clarify difference between theme_set() and theme_update() with examples, references #3294 * fixes to comply with PR review. add theme_replace in bullet list summary, merge all update/set/replace examples into one section, references #3294 * theme_replace example made similar to theme_update example for better comparison, references #3294 * fix theme_update and theme_replace example to better show differences, references #3294
1 parent 6db4368 commit 7e6d125

File tree

4 files changed

+84
-45
lines changed

4 files changed

+84
-45
lines changed

R/theme-current.R

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,36 @@ NULL
44

55
#' Get, set, and modify the active theme
66
#'
7-
#' The current/active theme is automatically applied to every plot you draw.
8-
#' Use `theme_get` to get the current theme, and `theme_set` to
9-
#' completely override it. `theme_update` and `theme_replace` are
10-
#' shorthands for changing individual elements.
7+
#' The current/active theme (see [theme()]) is automatically applied to every
8+
#' plot you draw. Use `theme_get` to get the current theme, and `theme_set` to
9+
#' completely override it. `theme_update` and `theme_replace` are shorthands for
10+
#' changing individual elements.
1111
#'
1212
#' @section Adding on to a theme:
1313
#'
14-
#' `+` and `%+replace%` can be used to modify elements in themes.
14+
#' `+` and `%+replace%` can be used to modify elements in themes.
1515
#'
16-
#' `+` updates the elements of e1 that differ from elements specified (not
17-
#' NULL) in e2. Thus this operator can be used to incrementally add or modify
18-
#' attributes of a ggplot theme.
16+
#' `+` updates the elements of e1 that differ from elements specified (not
17+
#' NULL) in e2. Thus this operator can be used to incrementally add or modify
18+
#' attributes of a ggplot theme.
1919
#'
20-
#' In contrast, `%+replace%` replaces the entire element; any element of
21-
#' a theme not specified in e2 will not be present in the resulting theme (i.e.
22-
#' NULL). Thus this operator can be used to overwrite an entire theme.
20+
#' In contrast, `%+replace%` replaces the entire element; any element of a
21+
#' theme not specified in e2 will not be present in the resulting theme (i.e.
22+
#' NULL). Thus this operator can be used to overwrite an entire theme.
2323
#'
24-
#' `theme_update` uses the `+` operator, so that any unspecified
25-
#' values in the theme element will default to the values they are set in the
26-
#' theme. `theme_replace` uses `%+replace%` to completely replace
27-
#' the element, so any unspecified values will overwrite the current value in
28-
#' the theme with `NULL`s.
24+
#' `theme_update` uses the `+` operator, so that any unspecified values in the
25+
#' theme element will default to the values they are set in the theme.
26+
#' `theme_replace` uses `%+replace%` to completely replace the element, so any
27+
#' unspecified values will overwrite the current value in the theme with
28+
#' `NULL`.
29+
#'
30+
#' In summary, the main differences between `theme_set()`, `theme_update()`,
31+
#' and `theme_replace()` are:
32+
#' * `theme_set()` completely overrides the current theme.
33+
#' * `theme_update()` modifies a particular element of the current theme
34+
#' using the `+` operator.
35+
#' * `theme_replace()` modifies a particular element of the current theme
36+
#' using the `%+replace%` operator.
2937
#'
3038
#' @param ... named list of theme settings
3139
#' @param e1,e2 Theme and element to combine
@@ -40,11 +48,23 @@ NULL
4048
#' p
4149
#'
4250
#' # Use theme_set() to completely override the current theme.
51+
#' # theme_update() and theme_replace() are similar except they
52+
#' # apply directly to the current/active theme.
53+
#' # theme_update() modifies a particular element of the current theme.
4354
#' # Here we have the old theme so we can later restore it.
4455
#' # Note that the theme is applied when the plot is drawn, not
4556
#' # when it is created.
4657
#' old <- theme_set(theme_bw())
4758
#' p
59+
#'
60+
#' theme_set(old)
61+
#' theme_update(panel.grid.minor = element_line(colour = "red"))
62+
#' p
63+
#'
64+
#' theme_set(old)
65+
#' theme_replace(panel.grid.minor = element_line(colour = "red"))
66+
#' p
67+
#'
4868
#' theme_set(old)
4969
#' p
5070
#'
@@ -62,8 +82,6 @@ NULL
6282
#' theme(text = element_text(family = "Times"))
6383
#' rep_el$text
6484
#'
65-
#' # theme_update() and theme_replace() are similar except they
66-
#' # apply directly to the current/active theme.
6785
theme_get <- function() {
6886
ggplot_global$theme_current
6987
}

R/theme.r

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#' Modify components of a theme
22
#'
3-
#' Themes are a powerful way to customize the non-data components of your
4-
#' plots: i.e. titles, labels, fonts, background, gridlines, and legends.
5-
#' Themes can be used to give plots a consistent customized look.
6-
#' Modify a single plot's theme using `theme()`; see [theme_update()] if
7-
#' you want modify the active theme, to affect all subsequent plots. Theme
8-
#' elements are documented together according to inheritance, read more
9-
#' about theme inheritance below.
3+
#' Themes are a powerful way to customize the non-data components of your plots:
4+
#' i.e. titles, labels, fonts, background, gridlines, and legends. Themes can be
5+
#' used to give plots a consistent customized look. Modify a single plot's theme
6+
#' using `theme()`; see [theme_update()] if you want modify the active theme, to
7+
#' affect all subsequent plots. Use the themes available in [complete
8+
#' themes][theme_bw] if you would like to use a complete theme such as
9+
#' `theme_bw()`, `theme_minimal()`, and more. Theme elements are documented
10+
#' together according to inheritance, read more about theme inheritance below.
1011
#'
1112
#' @section Theme inheritance:
1213
#' Theme elements inherit properties from other theme elements hierarchically.

man/theme.Rd

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

man/theme_get.Rd

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

0 commit comments

Comments
 (0)