Skip to content

Commit e007685

Browse files
author
koneill
committed
Change behaviour of theme_update to be more of an update, while adding theme_replace that has the same behaviour as the old theme_update. Also expanded and clarified the documentation for these.
1 parent ec5b4d7 commit e007685

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ export(theme_grey)
460460
export(theme_light)
461461
export(theme_linedraw)
462462
export(theme_minimal)
463+
export(theme_replace)
463464
export(theme_set)
464465
export(theme_update)
465466
export(theme_void)

R/theme.r

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
#' Get, set and update themes.
22
#'
3-
#' Use \code{theme_update} to modify a small number of elements of the current
4-
#' theme or use \code{theme_set} to completely override it.
3+
#' Use \code{theme_get} to get the current theme, and \code{theme_set} to
4+
#' completely override it. \code{theme_update} and \code{theme_replace} are
5+
#' shorthands for changing individual elements in the current theme.
6+
#' \code{theme_update} uses the \code{+} operator, so that any unspecified
7+
#' values in the theme element will default to the values they are set in the
8+
#' theme. \code{theme_replace} will completely replace the element, so any
9+
#' unspecified values will overwrite the current value in the theme with \code{NULL}s.
10+
#'
511
#'
612
#' @param ... named list of theme settings
713
#' @seealso \code{\link{\%+replace\%}} and \code{\link{+.gg}}
@@ -15,18 +21,40 @@
1521
#' theme_set(old)
1622
#' p
1723
#'
24+
#' #theme_replace NULLs out the fill attribute of panel.background,
25+
#' #resulting in a white background:
26+
#' theme_get()$panel.background
27+
#' old <- theme_replace(panel.background = element_rect(colour = "pink"))
28+
#' theme_get()$panel.background
29+
#' p
30+
#' theme_set(old)
31+
#'
32+
#' #theme_update only changes the colour attribute, leaving the others intact:
1833
#' old <- theme_update(panel.background = element_rect(colour = "pink"))
34+
#' theme_get()$panel.background
1935
#' p
2036
#' theme_set(old)
37+
#'
2138
#' theme_get()
2239
#'
40+
#'
2341
#' ggplot(mtcars, aes(mpg, wt)) +
2442
#' geom_point(aes(color = mpg)) +
2543
#' theme(legend.position = c(0.95, 0.95),
2644
#' legend.justification = c(1, 1))
2745
#' last_plot() +
2846
#' theme(legend.background = element_rect(fill = "white", colour = "white", size = 3))
47+
#'
2948
theme_update <- function(...) {
49+
# Make a call to theme, then add to theme
50+
theme_set(theme_get() + theme(...))
51+
}
52+
53+
#' @rdname theme_update
54+
#' @param ... named list of theme settings
55+
#' @export
56+
57+
theme_replace <- function(...) {
3058
# Make a call to theme, then add to theme
3159
theme_set(theme_get() %+replace% theme(...))
3260
}

man/theme_update.Rd

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

0 commit comments

Comments
 (0)