Skip to content

Commit f0fccce

Browse files
authored
Inheritance of theme-based aesthetics (#6285)
* allow unregistered `geom.*` elements in theme * allow inheritance of geom elements * add test * add colour/fill to element_geom * use ink/paper bypass in defaults * include default values in 'Aesthetics' section * document
1 parent d2cf9db commit f0fccce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+526
-472
lines changed

R/annotation-logticks.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ GeomLogticks <- ggproto("GeomLogticks", Geom,
229229
},
230230

231231
default_aes = aes(
232-
colour = from_theme(ink),
232+
colour = from_theme(colour %||% ink),
233233
linewidth = from_theme(linewidth),
234234
linetype = from_theme(linetype),
235235
alpha = 1

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)
@@ -239,13 +239,33 @@ Geom <- ggproto("Geom",
239239
#' @rdname is_tests
240240
is.geom <- function(x) inherits(x, "Geom")
241241

242-
eval_from_theme <- function(aesthetics, theme) {
242+
eval_from_theme <- function(aesthetics, theme, class = NULL) {
243243
themed <- is_themed_aes(aesthetics)
244244
if (!any(themed)) {
245245
return(aesthetics)
246246
}
247-
settings <- calc_element("geom", theme) %||% .default_geom_element
248-
lapply(aesthetics[themed], eval_tidy, data = settings)
247+
248+
element <- calc_element("geom", theme) %||% .default_geom_element
249+
class <- setdiff(class, c("Geom", "ggproto", "gg"))
250+
251+
if (length(class) > 0) {
252+
253+
# CamelCase to dot.case
254+
class <- gsub("([A-Za-z])([A-Z])([a-z])", "\\1.\\2\\3", class)
255+
class <- gsub("([a-z])([A-Z])", "\\1.\\2", class)
256+
class <- to_lower_ascii(class)
257+
258+
class <- class[class %in% names(theme)]
259+
260+
# Inherit up to parent geom class
261+
if (length(class) > 0) {
262+
for (cls in rev(class)) {
263+
element <- combine_elements(theme[[cls]], element)
264+
}
265+
}
266+
}
267+
268+
lapply(aesthetics[themed], eval_tidy, data = element)
249269
}
250270

251271
#' Graphical units

R/geom-abline.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ GeomAbline <- ggproto("GeomAbline", Geom,
147147
},
148148

149149
default_aes = aes(
150-
colour = from_theme(ink),
150+
colour = from_theme(colour %||% ink),
151151
linewidth = from_theme(linewidth),
152152
linetype = from_theme(linetype),
153153
alpha = NA

R/geom-boxplot.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,8 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
395395
draw_key = draw_key_boxplot,
396396

397397
default_aes = aes(
398-
weight = 1, colour = from_theme(col_mix(ink, paper, 0.2)),
399-
fill = from_theme(paper), size = from_theme(pointsize),
398+
weight = 1, colour = from_theme(colour %||% col_mix(ink, paper, 0.2)),
399+
fill = from_theme(fill %||% paper), size = from_theme(pointsize),
400400
alpha = NA, shape = from_theme(pointshape), linetype = from_theme(bordertype),
401401
linewidth = from_theme(borderwidth),
402402
width = 0.9

R/geom-contour.R

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,7 @@ geom_contour_filled <- function(mapping = NULL, data = NULL,
124124
#' @export
125125
#' @include geom-path.R
126126
GeomContour <- ggproto("GeomContour", GeomPath,
127-
default_aes = aes(
128-
weight = 1,
129-
colour = from_theme(accent),
130-
linewidth = from_theme(linewidth),
131-
linetype = from_theme(linetype),
132-
alpha = NA
133-
)
127+
default_aes = aes(weight = 1, !!!GeomPath$default_aes)
134128
)
135129

136130
#' @rdname ggplot2-ggproto

R/geom-crossbar.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ GeomCrossbar <- ggproto("GeomCrossbar", Geom,
7979
},
8080

8181
default_aes = aes(
82-
colour = from_theme(ink),
83-
fill = NA,
82+
colour = from_theme(colour %||% ink),
83+
fill = from_theme(fill %||% NA),
8484
linewidth = from_theme(borderwidth),
8585
linetype = from_theme(bordertype),
8686
alpha = NA

R/geom-curve.R

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,6 @@ geom_curve <- function(mapping = NULL, data = NULL,
4141
#' @export
4242
GeomCurve <- ggproto("GeomCurve", GeomSegment,
4343

44-
default_aes = aes(
45-
colour = from_theme(ink),
46-
linewidth = from_theme(linewidth),
47-
linetype = from_theme(linetype),
48-
alpha = NA
49-
),
50-
5144
draw_panel = function(data, panel_params, coord, curvature = 0.5, angle = 90,
5245
ncp = 5, arrow = NULL, arrow.fill = NULL, lineend = "butt", na.rm = FALSE) {
5346

R/geom-density.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,12 @@ geom_density <- function(mapping = NULL, data = NULL,
9292
#' @export
9393
#' @include geom-ribbon.R
9494
GeomDensity <- ggproto("GeomDensity", GeomArea,
95-
default_aes = defaults(
96-
aes(fill = NA, weight = 1, colour = from_theme(ink), alpha = NA),
97-
GeomArea$default_aes
95+
default_aes = aes(
96+
colour = from_theme(colour %||% ink),
97+
fill = from_theme(fill %||% NA),
98+
weight = 1,
99+
alpha = NA,
100+
linewidth = from_theme(borderwidth),
101+
linetype = from_theme(bordertype)
98102
)
99103
)

R/geom-density2d.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ geom_density2d <- geom_density_2d
107107
#' @export
108108
GeomDensity2d <- ggproto("GeomDensity2d", GeomPath,
109109
default_aes = aes(
110-
colour = from_theme(accent),
110+
colour = from_theme(colour %||% accent),
111111
linewidth = from_theme(linewidth),
112112
linetype = from_theme(linetype),
113113
alpha = NA

R/geom-dotplot.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ GeomDotplot <- ggproto("GeomDotplot", Geom,
189189
non_missing_aes = c("size", "shape"),
190190

191191
default_aes = aes(
192-
colour = from_theme(ink),
193-
fill = from_theme(ink),
192+
colour = from_theme(colour %||% ink),
193+
fill = from_theme(fill %||% ink),
194194
alpha = NA,
195195
stroke = from_theme(borderwidth * 2),
196196
linetype = from_theme(linetype),

0 commit comments

Comments
 (0)