Skip to content

Commit

Permalink
allow inheritance of geom elements
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand committed Jan 21, 2025
1 parent a76d1ab commit 912c29e
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions R/geom-.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Geom <- ggproto("Geom",
# Fill in missing aesthetics with their defaults
missing_aes <- setdiff(names(default_aes), names(data))
default_aes <- default_aes[missing_aes]
themed_defaults <- eval_from_theme(default_aes, theme)
themed_defaults <- eval_from_theme(default_aes, theme, class(self))
default_aes[names(themed_defaults)] <- themed_defaults

# Mark staged/scaled defaults as modifier (#6135)
Expand Down Expand Up @@ -243,13 +243,33 @@ Geom <- ggproto("Geom",
#' @rdname is_tests
is.geom <- function(x) inherits(x, "Geom")

eval_from_theme <- function(aesthetics, theme) {
eval_from_theme <- function(aesthetics, theme, class = NULL) {
themed <- is_themed_aes(aesthetics)
if (!any(themed)) {
return(aesthetics)
}
settings <- calc_element("geom", theme) %||% .default_geom_element
lapply(aesthetics[themed], eval_tidy, data = settings)

element <- calc_element("geom", theme) %||% .default_geom_element
class <- setdiff(class, c("Geom", "ggproto", "gg"))

if (length(class) > 0) {

# CamelCase to dot.case
class <- gsub("([A-Za-z])([A-Z])([a-z])", "\\1.\\2\\3", class)
class <- gsub("([a-z])([A-Z])", "\\1.\\2", class)
class <- to_lower_ascii(class)

class <- class[class %in% names(theme)]

# Inherit up to parent geom class
if (length(class) > 0) {
for (cls in rev(class)) {
element <- combine_elements(theme[[cls]], element)
}
}
}

lapply(aesthetics[themed], eval_tidy, data = element)
}

#' Graphical units
Expand Down

0 comments on commit 912c29e

Please sign in to comment.