Skip to content

Commit b790f5a

Browse files
committed
Move setup_layer() to plot build.
1 parent a9e1fc0 commit b790f5a

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

R/layer.r

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,10 @@ Layer <- ggproto("Layer", NULL,
199199
}
200200
},
201201

202-
# hook to allow a layer access to global plot data
203-
# as the plot is constructed.
204-
setup_layer = function(self, plot) {
202+
# hook to allow a layer access to the final layer data
203+
# in input form and to global plot info
204+
setup_layer = function(self, data, plot) {
205+
data
205206
},
206207

207208
compute_aesthetics = function(self, data, plot) {

R/plot-build.r

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,15 @@ ggplot_build.ggplot <- function(plot) {
3939
out
4040
}
4141

42+
# Allow all layers to make any final adjustments based
43+
# on raw input data and plot info
44+
data <- layer_data
45+
data <- by_layer(function(l, d) l$setup_layer(d, plot))
46+
4247
# Initialise panels, add extra data for margins & missing faceting
4348
# variables, and add on a PANEL variable to data
4449
layout <- create_layout(plot$facet, plot$coordinates)
45-
data <- layout$setup(layer_data, plot$data, plot$plot_env)
50+
data <- layout$setup(data, plot$data, plot$plot_env)
4651

4752
# Compute aesthetics to produce data with generalised variable names
4853
data <- by_layer(function(l, d) l$compute_aesthetics(d, plot))

R/plot-construction.r

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,6 @@ ggplot_add.list <- function(object, plot, object_name) {
153153
}
154154
#' @export
155155
ggplot_add.Layer <- function(object, plot, object_name) {
156-
# allow the layer to modify itself based on plot object
157-
# useful for layers that need to access the global data
158-
object$setup_layer(plot)
159-
160156
plot$layers <- append(plot$layers, object)
161157

162158
# Add any new labels

R/sf.R

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,20 @@ stat_sf <- function(mapping = NULL, data = NULL, geom = "rect",
148148
#' @usage NULL
149149
#' @format NULL
150150
LayerSf <- ggproto("LayerSf", Layer,
151-
setup_layer = function(self, plot) {
152-
# Automatically determine the name of the geometry column
153-
# and add the mapping if it doesn't exist
154-
data <- self$layer_data(plot$data)
151+
setup_layer = function(self, data, plot) {
152+
# process generic layer setup first
153+
data <- ggproto_parent(Layer, self)$setup_layer(data, plot)
155154

155+
# automatically determine the name of the geometry column
156+
# and add the mapping if it doesn't exist
156157
if ((isTRUE(self$inherit.aes) && is.null(self$mapping$geometry) && is.null(plot$mapping$geometry)) ||
157158
(!isTRUE(self$inherit.aes) && is.null(self$mapping$geometry))) {
158-
if (!is.waive(data) && is_sf(data)) {
159+
if (is_sf(data)) {
159160
geometry_col <- attr(data, "sf_column")
160161
self$mapping$geometry <- as.name(geometry_col)
161162
}
162163
}
163-
164-
# call parent for generic layer setup
165-
ggproto_parent(Layer, self)$setup_layer(plot)
164+
data
166165
}
167166
)
168167

0 commit comments

Comments
 (0)