Skip to content

Function to reset all aesthetics defaults #5976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_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")

Check warning on line 60 in R/geom-defaults.R

View check run for this annotation

Codecov / codecov/patch

R/geom-defaults.R#L60

Added line #L60 was not covered by tests

cache_defaults <- new_environment()

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

}
}

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())

Check warning on line 98 in R/geom-defaults.R

View check run for this annotation

Codecov / codecov/patch

R/geom-defaults.R#L98

Added line #L98 was not covered by tests
}
# 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
Loading