Skip to content

Commit

Permalink
FEAT: Let geoms define scale parameters and pass those to the scales
Browse files Browse the repository at this point in the history
  • Loading branch information
zeehio committed Nov 6, 2022
1 parent f995ea1 commit 7d1dc95
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
16 changes: 14 additions & 2 deletions R/geom-.r
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,20 @@ Geom <- ggproto("Geom",
},

# Should the geom rename size to linewidth?
rename_size = FALSE

rename_size = FALSE,
# Parameters to pass to scale$map() for each aesthetic:
# This can be either a named list (names are aesthetics, values are lists of parameters)
# or a function that takes a list of geom parameters as input and returns the list.
# The scale_params can be used to tell the scale map function a mapping method.
# e.g. scale_params = list(fill = list(mapping_method = "raw"))
# See the map method for ScaleContinuous in R/scale-.r for further details.
#
# The scale_params will be used to tell the scale map function the expected colour
# format, in case the geom prefers native colours (because it uses nativeRaster objects)
# instead of the default character vector:
# e.g. scale_params = list(fill = list("color_fmt" = "character")) # "#00FF00"
# e.g. scale_params = list(fill = list("color_fmt" = "native")) # from nativeRaster
scale_params = list()
)


Expand Down
8 changes: 8 additions & 0 deletions R/layer.r
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,14 @@ Layer <- ggproto("Layer", NULL,
self$geom$setup_data(data, self$computed_geom_params)
},

get_scale_params = function(self) {
if (is.function(self$geom$scale_params)) {
self$geom$scale_params(params = self$computed_geom_params)
} else {
self$geom$scale_params
}
},

compute_position = function(self, data, layout) {
if (empty(data)) return(data_frame0())

Expand Down
3 changes: 2 additions & 1 deletion R/plot-build.r
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ ggplot_build.ggplot <- function(plot) {
npscales <- scales$non_position_scales()
if (npscales$n() > 0) {
lapply(data, scales_train_df, scales = npscales)
data <- lapply(data, scales_map_df, scales = npscales)
scale_params <- by_layer(function(l, d) l$get_scale_params(), layers, data, "getting scale_params")
data <- mapply(scales_map_df, df = data, scale_params = scale_params, MoreArgs = list(scales = npscales), SIMPLIFY = FALSE)
}

# Fill in defaults etc.
Expand Down

0 comments on commit 7d1dc95

Please sign in to comment.