Skip to content

Commit 579e2d5

Browse files
authored
Using after_scale() in Geom$default_aes field (#6137)
* append to modifiers * add test * include test for `get_geom_defaults()` * add news bullet
1 parent 8a4c25a commit 579e2d5

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ggplot2 (development version)
22

3+
* (internal) Using `after_scale()` in the `Geom*$default_aes()` field is now
4+
evaluated in the context of data (@teunbrand, #6135)
35
* Fixed bug where binned scales wouldn't simultaneously accept transformations
46
and function-limits (@teunbrand, #6144).
57
* Fixed bug where the `ggplot2::`-prefix did not work with `stage()`

R/geom-.R

+7
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ Geom <- ggproto("Geom",
136136
themed_defaults <- eval_from_theme(default_aes, theme)
137137
default_aes[names(themed_defaults)] <- themed_defaults
138138

139+
# Mark staged/scaled defaults as modifier (#6135)
140+
delayed <- is_scaled_aes(default_aes) | is_staged_aes(default_aes)
141+
if (any(delayed)) {
142+
modifiers <- defaults(modifiers, default_aes[delayed])
143+
default_aes <- default_aes[!delayed]
144+
}
145+
139146
missing_eval <- lapply(default_aes, eval_tidy)
140147
# Needed for geoms with defaults set to NULL (e.g. GeomSf)
141148
missing_eval <- compact(missing_eval)

tests/testthat/test-aes-calculated.R

+23
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,26 @@ test_that("stage allows aesthetics that are only mapped to start", {
146146
)
147147

148148
})
149+
150+
test_that("A geom can have scaled defaults (#6135)", {
151+
152+
test_geom <- ggproto(
153+
NULL, GeomPoint,
154+
default_aes = modify_list(
155+
GeomPoint$default_aes,
156+
aes(colour = after_scale(alpha(fill, 0.5)), fill = "black")
157+
)
158+
)
159+
160+
df <- data.frame(x = 1:3, fill = c("#FF0000", "#00FF00", "#0000FF"))
161+
162+
ld <- layer_data(
163+
ggplot(df, aes(x, x, fill = I(fill))) +
164+
stat_identity(geom = test_geom)
165+
)
166+
167+
expect_equal(ld$colour, c("#FF000080", "#00FF0080", '#0000FF80'))
168+
169+
defaults <- get_geom_defaults(test_geom)
170+
expect_equal(defaults$colour, c("#00000080"))
171+
})

0 commit comments

Comments
 (0)