Skip to content

Commit 1d7c7b3

Browse files
committed
FEAT: Let geoms define scale parameters and pass those to the scales
1 parent 5a14abc commit 1d7c7b3

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

R/geom-.r

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,20 @@ Geom <- ggproto("Geom",
202202
},
203203

204204
# Should the geom rename size to linewidth?
205-
rename_size = FALSE
206-
205+
rename_size = FALSE,
206+
# Parameters to pass to scale$map() for each aesthetic:
207+
# This can be either a named list (names are aesthetics, values are lists of parameters)
208+
# or a function that takes a list of geom parameters as input and returns the list.
209+
# The scale_params can be used to tell the scale map function a mapping method.
210+
# e.g. scale_params = list(fill = list(mapping_method = "raw"))
211+
# See the map method for ScaleContinuous in R/scale-.r for further details.
212+
#
213+
# The scale_params will be used to tell the scale map function the expected colour
214+
# format, in case the geom prefers native colours (because it uses nativeRaster objects)
215+
# instead of the default character vector:
216+
# e.g. scale_params = list(fill = list("color_fmt" = "character")) # "#00FF00"
217+
# e.g. scale_params = list(fill = list("color_fmt" = "native")) # from nativeRaster
218+
scale_params = list()
207219
)
208220

209221

R/layer.r

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,14 @@ Layer <- ggproto("Layer", NULL,
391391
self$geom$setup_data(data, self$computed_geom_params)
392392
},
393393

394+
get_scale_params = function(self) {
395+
if (is.function(self$geom$scale_params)) {
396+
self$geom$scale_params(params = self$computed_geom_params)
397+
} else {
398+
self$geom$scale_params
399+
}
400+
},
401+
394402
compute_position = function(self, data, layout) {
395403
if (empty(data)) return(data_frame0())
396404

R/plot-build.r

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ ggplot_build.ggplot <- function(plot) {
8888
npscales <- scales$non_position_scales()
8989
if (npscales$n() > 0) {
9090
lapply(data, scales_train_df, scales = npscales)
91-
data <- lapply(data, scales_map_df, scales = npscales)
91+
scale_params <- by_layer(function(l, d) l$get_scale_params(), layers, data, "getting scale_params")
92+
data <- mapply(scales_map_df, df = data, scale_params = scale_params, MoreArgs = list(scales = npscales), SIMPLIFY = FALSE)
9293
}
9394

9495
# Fill in defaults etc.

0 commit comments

Comments
 (0)