Skip to content

Commit f765685

Browse files
committed
positions populate defaults
1 parent b341653 commit f765685

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

R/layer.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@
5858
#' `NA`, the default, includes if any aesthetics are mapped.
5959
#' `FALSE` never includes, and `TRUE` always includes.
6060
#' It can also be a named logical vector to finely select the aesthetics to
61-
#' display. To include legend keys for all levels, even
62-
#' when no data exists, use `TRUE`. If `NA`, all levels are shown in legend,
61+
#' display. To include legend keys for all levels, even
62+
#' when no data exists, use `TRUE`. If `NA`, all levels are shown in legend,
6363
#' but unobserved levels are omitted.
6464
#' @param inherit.aes If `FALSE`, overrides the default aesthetics,
6565
#' rather than combining with them. This is most useful for helper functions
@@ -437,6 +437,7 @@ Layer <- ggproto("Layer", NULL,
437437
compute_position = function(self, data, layout) {
438438
if (empty(data)) return(data_frame0())
439439

440+
data <- self$position$use_defaults(data, self$aes_params)
440441
params <- self$position$setup_params(data)
441442
data <- self$position$setup_data(data, params)
442443

R/position-.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,28 @@ Position <- ggproto("Position",
7676
required_aes <- unlist(strsplit(self$required_aes, "|", fixed = TRUE))
7777
}
7878
c(union(required_aes, names(self$default_aes)))
79+
},
80+
81+
use_defaults = function(self, data, params = list()) {
82+
83+
aes <- self$aesthetics()
84+
defaults <- self$default_aes
85+
86+
params <- params[intersect(names(params), aes)]
87+
params <- params[setdiff(names(params), names(data))]
88+
defaults <- defaults[setdiff(names(defaults), c(names(params), names(data)))]
89+
90+
if ((length(params) + length(defaults)) < 1) {
91+
return(data)
92+
}
93+
94+
new <- compact(lapply(defaults, eval_tidy, data = data))
95+
new[names(params)] <- params
96+
check_aesthetics(new, nrow(data))
97+
98+
data[names(new)] <- new
99+
data
100+
79101
}
80102
)
81103

0 commit comments

Comments
 (0)