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

+1-1
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

+24-4
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

+1-1
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

+2-2
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

+1-7
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

+2-2
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

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

+7-3
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

+1-1
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

+2-2
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),

R/geom-errorbar.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ geom_errorbarh <- function(mapping = NULL, data = NULL,
5959
GeomErrorbar <- ggproto("GeomErrorbar", Geom,
6060

6161
default_aes = aes(
62-
colour = from_theme(ink),
62+
colour = from_theme(colour %||% ink),
6363
linewidth = from_theme(linewidth),
6464
linetype = from_theme(linetype),
6565
width = 0.9,

R/geom-hex.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ GeomHex <- ggproto("GeomHex", Geom,
107107
required_aes = c("x", "y"),
108108

109109
default_aes = aes(
110-
colour = NA,
111-
fill = from_theme(col_mix(ink, paper)),
110+
colour = from_theme(colour %||% NA),
111+
fill = from_theme(fill %||% col_mix(ink, paper)),
112112
linewidth = from_theme(borderwidth),
113113
linetype = from_theme(bordertype),
114114
alpha = NA

R/geom-hline.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ GeomHline <- ggproto("GeomHline", Geom,
5757
},
5858

5959
default_aes = aes(
60-
colour = from_theme(ink),
60+
colour = from_theme(colour %||% ink),
6161
linewidth = from_theme(linewidth),
6262
linetype = from_theme(linetype),
6363
alpha = NA

R/geom-label.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ GeomLabel <- ggproto("GeomLabel", Geom,
6262
required_aes = c("x", "y", "label"),
6363

6464
default_aes = aes(
65-
colour = from_theme(ink), fill = from_theme(paper),
65+
colour = from_theme(colour %||% ink),
66+
fill = from_theme(fill %||% paper),
6667
family = from_theme(family),
6768
size = from_theme(fontsize),
6869
angle = 0,

R/geom-linerange.R

+1-6
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,7 @@ geom_linerange <- function(mapping = NULL, data = NULL,
9191
#' @export
9292
GeomLinerange <- ggproto("GeomLinerange", Geom,
9393

94-
default_aes = aes(
95-
colour = from_theme(ink),
96-
linewidth = from_theme(linewidth),
97-
linetype = from_theme(linetype),
98-
alpha = NA
99-
),
94+
default_aes = GeomPath$default_aes,
10095

10196
draw_key = draw_key_linerange,
10297

R/geom-point.R

+5-2
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,11 @@ GeomPoint <- ggproto("GeomPoint", Geom,
143143
non_missing_aes = c("size", "shape", "colour"),
144144
default_aes = aes(
145145
shape = from_theme(pointshape),
146-
colour = from_theme(ink), size = from_theme(pointsize), fill = NA,
147-
alpha = NA, stroke = from_theme(borderwidth)
146+
colour = from_theme(colour %||% ink),
147+
fill = from_theme(fill %||% NA),
148+
size = from_theme(pointsize),
149+
alpha = NA,
150+
stroke = from_theme(borderwidth)
148151
),
149152

150153
draw_panel = function(self, data, panel_params, coord, na.rm = FALSE) {

R/geom-pointrange.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ geom_pointrange <- function(mapping = NULL, data = NULL,
3131
#' @export
3232
GeomPointrange <- ggproto("GeomPointrange", Geom,
3333
default_aes = aes(
34-
colour = from_theme(ink), size = from_theme(pointsize / 3),
34+
colour = from_theme(colour %||% ink), size = from_theme(pointsize / 3),
3535
linewidth = from_theme(linewidth), linetype = from_theme(linetype),
36-
shape = from_theme(pointshape), fill = NA, alpha = NA,
36+
shape = from_theme(pointshape), fill = from_theme(fill %||% NA), alpha = NA,
3737
stroke = from_theme(borderwidth * 2)
3838
),
3939

R/geom-polygon.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ GeomPolygon <- ggproto("GeomPolygon", Geom,
176176
},
177177

178178
default_aes = aes(
179-
colour = NA,
180-
fill = from_theme(col_mix(ink, paper, 0.2)),
179+
colour = from_theme(colour %||% NA),
180+
fill = from_theme(fill %||% col_mix(ink, paper, 0.2)),
181181
linewidth = from_theme(borderwidth),
182182
linetype = from_theme(bordertype),
183183
alpha = NA, subgroup = NULL

R/geom-quantile.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ geom_quantile <- function(mapping = NULL, data = NULL,
6565
#' @export
6666
#' @include geom-path.R
6767
GeomQuantile <- ggproto("GeomQuantile", GeomPath,
68-
default_aes = defaults(
69-
aes(weight = 1, colour = from_theme(accent)),
68+
default_aes = aes(!!!defaults(
69+
aes(weight = 1, colour = from_theme(colour %||% accent)),
7070
GeomPath$default_aes
71-
)
71+
))
7272
)

R/geom-raster.R

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ geom_raster <- function(mapping = NULL, data = NULL,
4444
#' @usage NULL
4545
#' @export
4646
GeomRaster <- ggproto("GeomRaster", Geom,
47-
default_aes = aes(fill = from_theme(col_mix(ink, paper, 0.2)), alpha = NA),
47+
default_aes = aes(
48+
fill = from_theme(fill %||% col_mix(ink, paper, 0.2)),
49+
alpha = NA
50+
),
4851
non_missing_aes = c("fill", "xmin", "xmax", "ymin", "ymax"),
4952
required_aes = c("x", "y"),
5053

R/geom-rect.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ geom_rect <- function(mapping = NULL, data = NULL,
2929
#' @export
3030
GeomRect <- ggproto("GeomRect", Geom,
3131
default_aes = aes(
32-
colour = NA, fill = from_theme(col_mix(ink, paper, 0.35)),
32+
colour = from_theme(colour %||% NA),
33+
fill = from_theme(fill %||% col_mix(ink, paper, 0.35)),
3334
linewidth = from_theme(borderwidth), linetype = from_theme(bordertype),
3435
alpha = NA
3536
),

R/geom-ribbon.R

+5-11
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,14 @@ geom_ribbon <- function(mapping = NULL, data = NULL,
9696
#' @usage NULL
9797
#' @export
9898
GeomRibbon <- ggproto("GeomRibbon", Geom,
99+
99100
default_aes = aes(
100-
colour = NA,
101-
fill = from_theme(col_mix(ink, paper, 0.2)),
101+
colour = from_theme(colour %||% NA),
102+
fill = from_theme(fill %||% col_mix(ink, paper, 0.2)),
102103
linewidth = from_theme(borderwidth),
103104
linetype = from_theme(bordertype),
104-
alpha = NA),
105+
alpha = NA
106+
),
105107

106108
required_aes = c("x|y", "ymin|xmin", "ymax|xmax"),
107109

@@ -320,14 +322,6 @@ geom_area <- function(mapping = NULL, data = NULL, stat = "align",
320322
#' @export
321323
GeomArea <- ggproto("GeomArea", GeomRibbon,
322324

323-
default_aes = aes(
324-
colour = NA,
325-
fill = from_theme(col_mix(ink, paper, 0.2)),
326-
linewidth = from_theme(borderwidth),
327-
linetype = from_theme(bordertype),
328-
alpha = NA
329-
),
330-
331325
required_aes = c("x", "y"),
332326

333327
setup_params = function(data, params) {

R/geom-rug.R

+1-6
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,7 @@ GeomRug <- ggproto("GeomRug", Geom,
153153
gTree(children = inject(gList(!!!rugs)))
154154
},
155155

156-
default_aes = aes(
157-
colour = from_theme(ink),
158-
linewidth = from_theme(linewidth),
159-
linetype = from_theme(linetype),
160-
alpha = NA
161-
),
156+
default_aes = GeomPath$default_aes,
162157

163158
draw_key = draw_key_path,
164159

R/geom-segment.R

+1-6
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,7 @@ GeomSegment <- ggproto("GeomSegment", Geom,
105105
required_aes = c("x", "y", "xend|yend"),
106106
non_missing_aes = c("linetype", "linewidth"),
107107

108-
default_aes = aes(
109-
colour = from_theme(ink),
110-
linewidth = from_theme(linewidth),
111-
linetype = from_theme(linetype),
112-
alpha = NA
113-
),
108+
default_aes = GeomPath$default_aes,
114109

115110
draw_panel = function(self, data, panel_params, coord, arrow = NULL, arrow.fill = NULL,
116111
lineend = "butt", linejoin = "round", na.rm = FALSE) {

R/geom-sf.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ GeomSf <- ggproto("GeomSf", Geom,
171171
other_default <- modify_list(
172172
GeomPolygon$default_aes,
173173
aes(
174-
fill = from_theme(col_mix(ink, paper, 0.9)),
175-
colour = from_theme(col_mix(ink, paper, 0.35)),
174+
fill = from_theme(fill %||% col_mix(ink, paper, 0.9)),
175+
colour = from_theme(colour %||% col_mix(ink, paper, 0.35)),
176176
linewidth = from_theme(0.4 * borderwidth)
177177
)
178178
)

R/geom-smooth.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ GeomSmooth <- ggproto("GeomSmooth", Geom,
169169
optional_aes = c("ymin", "ymax"),
170170

171171
default_aes = aes(
172-
colour = from_theme(accent),
173-
fill = from_theme(col_mix(ink, paper, 0.6)),
172+
colour = from_theme(colour %||% accent),
173+
fill = from_theme(fill %||% col_mix(ink, paper, 0.6)),
174174
linewidth = from_theme(2 * linewidth),
175175
linetype = from_theme(linetype),
176176
weight = 1, alpha = 0.4

R/geom-text.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ GeomText <- ggproto("GeomText", Geom,
190190
non_missing_aes = "angle",
191191

192192
default_aes = aes(
193-
colour = from_theme(ink),
193+
colour = from_theme(colour %||% ink),
194194
family = from_theme(family),
195195
size = from_theme(fontsize),
196196
angle = 0, hjust = 0.5,

R/geom-tile.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ GeomTile <- ggproto("GeomTile", GeomRect,
130130
},
131131

132132
default_aes = aes(
133-
fill = from_theme(col_mix(ink, paper, 0.2)),
134-
colour = NA,
133+
fill = from_theme(fill %||% col_mix(ink, paper, 0.2)),
134+
colour = from_theme(colour %||% NA),
135135
linewidth = from_theme(0.4 * borderwidth),
136136
linetype = from_theme(bordertype),
137137
alpha = NA, width = 1, height = 1

R/geom-violin.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ GeomViolin <- ggproto("GeomViolin", Geom,
230230

231231
default_aes = aes(
232232
weight = 1,
233-
colour = from_theme(col_mix(ink, paper, 0.2)),
234-
fill = from_theme(paper),
233+
colour = from_theme(colour %||% col_mix(ink, paper, 0.2)),
234+
fill = from_theme(fill %||% paper),
235235
linewidth = from_theme(borderwidth),
236236
linetype = from_theme(bordertype),
237237
alpha = NA,

0 commit comments

Comments
 (0)