Skip to content

Commit c38606f

Browse files
authored
Function to reset all aesthetics defaults (#5976)
* reset functions * document * add test * add news bullet * fix typo
1 parent c108758 commit c38606f

File tree

5 files changed

+59
-5
lines changed

5 files changed

+59
-5
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,8 @@ export(remove_missing)
513513
export(render_axes)
514514
export(render_strips)
515515
export(replace_theme)
516+
export(reset_geom_defaults)
517+
export(reset_stat_defaults)
516518
export(reset_theme_settings)
517519
export(resolution)
518520
export(scale_alpha)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ggplot2 (development version)
22

3+
* New `reset_geom_defaults()` and `reset_stat_defaults()` to restore all geom or
4+
stat default aesthetics at once (@teunbrand, #5975).
35
* `facet_wrap()` can have `space = "free_x"` with 1-row layouts and
46
`space = "free_y"` with 1-column layouts (@teunbrand)
57
* Secondary axes respect `n.breaks` setting in continuous scales (@teunbrand, #4483).

R/geom-defaults.R

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#' Modify geom/stat aesthetic defaults for future plots
22
#'
3+
#' Functions to update or reset the default aesthetics of geoms and stats.
4+
#'
35
#' @param stat,geom Name of geom/stat to modify (like `"point"` or
46
#' `"bin"`), or a Geom/Stat object (like `GeomPoint` or
57
#' `StatBin`).
@@ -17,9 +19,11 @@
1719
#' GeomPoint$default_aes
1820
#' ggplot(mtcars, aes(mpg, wt)) + geom_point()
1921
#'
20-
#' # reset default
22+
#' # reset single default
2123
#' update_geom_defaults("point", NULL)
2224
#'
25+
#' # reset all defaults
26+
#' reset_geom_defaults()
2327
#'
2428
#' # updating a stat's default aesthetic settings
2529
#' # example: change stat_bin()'s default y-axis to the density scale
@@ -30,9 +34,12 @@
3034
#' geom_histogram() +
3135
#' geom_function(fun = dnorm, color = "red")
3236
#'
33-
#' # reset default
37+
#' # reset single default
3438
#' update_stat_defaults("bin", NULL)
3539
#'
40+
#' # reset all defaults
41+
#' reset_stat_defaults()
42+
#'
3643
#' @rdname update_defaults
3744
update_geom_defaults <- function(geom, new) {
3845
update_defaults(geom, "Geom", new, env = parent.frame())
@@ -44,6 +51,14 @@ update_stat_defaults <- function(stat, new) {
4451
update_defaults(stat, "Stat", new, env = parent.frame())
4552
}
4653

54+
#' @rdname update_defaults
55+
#' @export
56+
reset_geom_defaults <- function() reset_defaults("geom")
57+
58+
#' @rdname update_defaults
59+
#' @export
60+
reset_stat_defaults <- function() reset_defaults("stat")
61+
4762
cache_defaults <- new_environment()
4863

4964
update_defaults <- function(name, subclass, new, env = parent.frame()) {
@@ -73,3 +88,20 @@ update_defaults <- function(name, subclass, new, env = parent.frame()) {
7388

7489
}
7590
}
91+
92+
reset_defaults <- function(type) {
93+
# Lookup matching names in cache
94+
prefix <- paste0("^", type, "_")
95+
full_names <- grep(prefix, ls(cache_defaults), value = TRUE)
96+
# Early exit if there is nothing to reset
97+
if (length(full_names) < 1) {
98+
return(invisible())
99+
}
100+
# Format names without prefix
101+
short_names <- gsub(prefix, "", full_names)
102+
names(short_names) <- full_names
103+
104+
# Run updates
105+
update <- switch(type, geom = update_geom_defaults, update_stat_defaults)
106+
invisible(lapply(short_names, update, new = NULL))
107+
}

man/update_defaults.Rd

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

tests/testthat/test-geom-.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ test_that("geom defaults can be set and reset", {
2020
test <- l$geom$use_defaults(data_frame0())
2121
expect_equal(test$colour, "black")
2222
expect_equal(inv$colour, "red")
23+
24+
inv <- update_geom_defaults("line", list(colour = "blue"))
25+
reset <- reset_geom_defaults()
26+
27+
expect_equal(reset$geom_line$colour, "blue")
28+
expect_equal(reset$geom_point$colour, GeomPoint$default_aes$colour)
29+
expect_equal(GeomLine$default_aes$colour, inv$colour)
2330
})
2431

2532
test_that("updating geom aesthetic defaults preserves class and order", {

0 commit comments

Comments
 (0)