diff --git a/NAMESPACE b/NAMESPACE index 6a57c5132d..ea62e9b076 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -513,6 +513,8 @@ export(remove_missing) export(render_axes) export(render_strips) export(replace_theme) +export(reset_geom_defaults) +export(reset_stat_defaults) export(reset_theme_settings) export(resolution) export(scale_alpha) diff --git a/NEWS.md b/NEWS.md index 7fbbc1967b..39fab0861b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # ggplot2 (development version) +* New `reset_geom_defaults()` and `reset_stat_defaults()` to restore all geom or + stat default aesthetics at once (@teunbrand, #5975). * `facet_wrap()` can have `space = "free_x"` with 1-row layouts and `space = "free_y"` with 1-column layouts (@teunbrand) * Secondary axes respect `n.breaks` setting in continuous scales (@teunbrand, #4483). diff --git a/R/geom-defaults.R b/R/geom-defaults.R index 8b81eeef94..e4e09ce71c 100644 --- a/R/geom-defaults.R +++ b/R/geom-defaults.R @@ -1,5 +1,7 @@ #' Modify geom/stat aesthetic defaults for future plots #' +#' Functions to update or reset the default aesthetics of geoms and stats. +#' #' @param stat,geom Name of geom/stat to modify (like `"point"` or #' `"bin"`), or a Geom/Stat object (like `GeomPoint` or #' `StatBin`). @@ -17,9 +19,11 @@ #' GeomPoint$default_aes #' ggplot(mtcars, aes(mpg, wt)) + geom_point() #' -#' # reset default +#' # reset single default #' update_geom_defaults("point", NULL) #' +#' # reset all defaults +#' reset_geom_defaults() #' #' # updating a stat's default aesthetic settings #' # example: change stat_bin()'s default y-axis to the density scale @@ -30,9 +34,12 @@ #' geom_histogram() + #' geom_function(fun = dnorm, color = "red") #' -#' # reset default +#' # reset single default #' update_stat_defaults("bin", NULL) #' +#' # reset all defaults +#' reset_stat_defaults() +#' #' @rdname update_defaults update_geom_defaults <- function(geom, new) { update_defaults(geom, "Geom", new, env = parent.frame()) @@ -44,6 +51,14 @@ update_stat_defaults <- function(stat, new) { update_defaults(stat, "Stat", new, env = parent.frame()) } +#' @rdname update_defaults +#' @export +reset_geom_defaults <- function() reset_defaults("geom") + +#' @rdname update_defaults +#' @export +reset_stat_defaults <- function() reset_defaults("stat") + cache_defaults <- new_environment() update_defaults <- function(name, subclass, new, env = parent.frame()) { @@ -73,3 +88,20 @@ update_defaults <- function(name, subclass, new, env = parent.frame()) { } } + +reset_defaults <- function(type) { + # Lookup matching names in cache + prefix <- paste0("^", type, "_") + full_names <- grep(prefix, ls(cache_defaults), value = TRUE) + # Early exit if there is nothing to reset + if (length(full_names) < 1) { + return(invisible()) + } + # Format names without prefix + short_names <- gsub(prefix, "", full_names) + names(short_names) <- full_names + + # Run updates + update <- switch(type, geom = update_geom_defaults, update_stat_defaults) + invisible(lapply(short_names, update, new = NULL)) +} diff --git a/man/update_defaults.Rd b/man/update_defaults.Rd index 8006bf8246..334dffed8e 100644 --- a/man/update_defaults.Rd +++ b/man/update_defaults.Rd @@ -3,11 +3,17 @@ \name{update_geom_defaults} \alias{update_geom_defaults} \alias{update_stat_defaults} +\alias{reset_geom_defaults} +\alias{reset_stat_defaults} \title{Modify geom/stat aesthetic defaults for future plots} \usage{ update_geom_defaults(geom, new) update_stat_defaults(stat, new) + +reset_geom_defaults() + +reset_stat_defaults() } \arguments{ \item{new}{One of the following: @@ -21,7 +27,7 @@ update_stat_defaults(stat, new) \code{StatBin}).} } \description{ -Modify geom/stat aesthetic defaults for future plots +Functions to update or reset the default aesthetics of geoms and stats. } \examples{ @@ -32,9 +38,11 @@ update_geom_defaults("point", aes(color = "red")) GeomPoint$default_aes ggplot(mtcars, aes(mpg, wt)) + geom_point() -# reset default +# reset single default update_geom_defaults("point", NULL) +# reset all defaults +reset_geom_defaults() # updating a stat's default aesthetic settings # example: change stat_bin()'s default y-axis to the density scale @@ -45,8 +53,11 @@ ggplot(data.frame(x = rnorm(1e3)), aes(x)) + geom_histogram() + geom_function(fun = dnorm, color = "red") -# reset default +# reset single default update_stat_defaults("bin", NULL) +# reset all defaults +reset_stat_defaults() + } \keyword{internal} diff --git a/tests/testthat/test-geom-.R b/tests/testthat/test-geom-.R index 61063d5d95..e0a0ca060a 100644 --- a/tests/testthat/test-geom-.R +++ b/tests/testthat/test-geom-.R @@ -20,6 +20,13 @@ test_that("geom defaults can be set and reset", { test <- l$geom$use_defaults(data_frame0()) expect_equal(test$colour, "black") expect_equal(inv$colour, "red") + + inv <- update_geom_defaults("line", list(colour = "blue")) + reset <- reset_geom_defaults() + + expect_equal(reset$geom_line$colour, "blue") + expect_equal(reset$geom_point$colour, GeomPoint$default_aes$colour) + expect_equal(GeomLine$default_aes$colour, inv$colour) }) test_that("updating geom aesthetic defaults preserves class and order", {