Skip to content

Commit 912c29e

Browse files
committed
allow inheritance of geom elements
1 parent a76d1ab commit 912c29e

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

R/geom-.R

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ Geom <- ggproto("Geom",
133133
# Fill in missing aesthetics with their defaults
134134
missing_aes <- setdiff(names(default_aes), names(data))
135135
default_aes <- default_aes[missing_aes]
136-
themed_defaults <- eval_from_theme(default_aes, theme)
136+
themed_defaults <- eval_from_theme(default_aes, theme, class(self))
137137
default_aes[names(themed_defaults)] <- themed_defaults
138138

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

246-
eval_from_theme <- function(aesthetics, theme) {
246+
eval_from_theme <- function(aesthetics, theme, class = NULL) {
247247
themed <- is_themed_aes(aesthetics)
248248
if (!any(themed)) {
249249
return(aesthetics)
250250
}
251-
settings <- calc_element("geom", theme) %||% .default_geom_element
252-
lapply(aesthetics[themed], eval_tidy, data = settings)
251+
252+
element <- calc_element("geom", theme) %||% .default_geom_element
253+
class <- setdiff(class, c("Geom", "ggproto", "gg"))
254+
255+
if (length(class) > 0) {
256+
257+
# CamelCase to dot.case
258+
class <- gsub("([A-Za-z])([A-Z])([a-z])", "\\1.\\2\\3", class)
259+
class <- gsub("([a-z])([A-Z])", "\\1.\\2", class)
260+
class <- to_lower_ascii(class)
261+
262+
class <- class[class %in% names(theme)]
263+
264+
# Inherit up to parent geom class
265+
if (length(class) > 0) {
266+
for (cls in rev(class)) {
267+
element <- combine_elements(theme[[cls]], element)
268+
}
269+
}
270+
}
271+
272+
lapply(aesthetics[themed], eval_tidy, data = element)
253273
}
254274

255275
#' Graphical units

0 commit comments

Comments
 (0)