Skip to content

Commit 399f90d

Browse files
committed
Merge branch 'oneillkza-master'
2 parents e3bd311 + 1a791f7 commit 399f90d

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ export(theme_grey)
469469
export(theme_light)
470470
export(theme_linedraw)
471471
export(theme_minimal)
472+
export(theme_replace)
472473
export(theme_set)
473474
export(theme_update)
474475
export(theme_void)

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
* `layer()` now automatically adds a `na.rm` parameter if none is explicitly
99
supplied.
1010

11+
* `theme_update()` now uses the `+` operator instead of `%+replace%`, so that
12+
unspecified values will no longer be `NULL`ed out. `theme_replace()`
13+
preserves the old behaviour if desired (@oneillkza, #1519).
14+
1115
* `layer()` now accepts a function as the data argument. The function will be
1216
applied to the data passed to the `ggplot()` function and must return a
1317
data.frame (#1527).

R/theme.r

Lines changed: 27 additions & 3 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,19 +21,37 @@
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(...) {
30-
# Make a call to theme, then add to theme
49+
theme_set(theme_get() + theme(...))
50+
}
51+
52+
#' @rdname theme_update
53+
#' @export
54+
theme_replace <- function(...) {
3155
theme_set(theme_get() %+replace% theme(...))
3256
}
3357

man/theme_update.Rd

Lines changed: 23 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)