Skip to content

Commit

Permalink
Function to reset all aesthetics defaults (#5976)
Browse files Browse the repository at this point in the history
* reset functions

* document

* add test

* add news bullet

* fix typo
  • Loading branch information
teunbrand committed Aug 26, 2024
1 parent c108758 commit c38606f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
36 changes: 34 additions & 2 deletions R/geom-defaults.R
Original file line number Diff line number Diff line change
@@ -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`).
Expand All @@ -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
Expand All @@ -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())
Expand All @@ -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()) {
Expand Down Expand Up @@ -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))
}
17 changes: 14 additions & 3 deletions man/update_defaults.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/testthat/test-geom-.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down

0 comments on commit c38606f

Please sign in to comment.