Skip to content

Commit e6afd5c

Browse files
committed
getter for geom defaults
1 parent 0d682f1 commit e6afd5c

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Config/testthat/edition: 3
7878
Encoding: UTF-8
7979
LazyData: true
8080
Roxygen: list(markdown = TRUE)
81-
RoxygenNote: 7.3.1
81+
RoxygenNote: 7.3.2
8282
Collate:
8383
'ggproto.R'
8484
'ggplot-global.R'

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ export(geom_violin)
424424
export(geom_vline)
425425
export(get_alt_text)
426426
export(get_element_tree)
427+
export(get_geom_defaults)
427428
export(get_guide_data)
428429
export(get_last_plot)
429430
export(get_layer_data)

R/geom-defaults.R

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,58 @@ update_stat_defaults <- function(stat, new) {
4747
update_defaults(stat, "Stat", new, env = parent.frame())
4848
}
4949

50+
#' Resolve and get geom defaults
51+
#'
52+
#' @param geom Some definition of a geom:
53+
#' * A `function` that creates a layer, e.g. `geom_path()`.
54+
#' * A layer created by such function
55+
#' * A string naming a geom class in snake case without the `geom_`-prefix,
56+
#' e.g. `"contour_filled"`.
57+
#' * A geom class object.
58+
#' @param theme A [`theme`] object. Defaults to the current global theme.
59+
#'
60+
#' @return A list of aesthetics
61+
#' @export
62+
#' @keywords internal
63+
#'
64+
#' @examples
65+
#' # Using a function
66+
#' get_geom_defaults(geom_raster)
67+
#'
68+
#' # Using a layer includes static aesthetics as default
69+
#' get_geom_defaults(geom_tile(fill = "white"))
70+
#'
71+
#' # Using a class name
72+
#' get_geom_defaults("density_2d")
73+
#'
74+
#' # Using a class
75+
#' get_geom_defaults(GeomPoint)
76+
#'
77+
#' # Changed theme
78+
#' get_geom_defaults("point", theme(geom = element_geom(ink = "purple")))
79+
get_geom_defaults <- function(geom, theme = theme_get()) {
80+
theme <- theme %||% list(geom = .default_geom_element)
81+
82+
if (is.function(geom)) {
83+
geom <- geom()
84+
}
85+
if (is.layer(geom)) {
86+
data <- data_frame0(.id = 1L)
87+
data <- geom$compute_geom_2(data = data, theme = theme)
88+
data$.id <- NULL
89+
return(data)
90+
}
91+
if (is.character(geom)) {
92+
geom <- check_subclass(geom, "Geom")
93+
}
94+
if (inherits(geom, "Geom")) {
95+
out <- geom$use_defaults(data = NULL, theme = theme)
96+
return(out)
97+
}
98+
stop_input_type(geom, as_cli("a layer function, string or {.cls Geom} object"))
99+
}
100+
101+
50102
cache_defaults <- new_environment()
51103

52104
update_defaults <- function(name, subclass, new, env = parent.frame()) {

man/get_geom_defaults.Rd

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

0 commit comments

Comments
 (0)