Skip to content

WIP: set common scale parameters outside of the scale function #4271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ S3method(ggplot_add,default)
S3method(ggplot_add,guides)
S3method(ggplot_add,labels)
S3method(ggplot_add,list)
S3method(ggplot_add,scales_params)
S3method(ggplot_add,theme)
S3method(ggplot_add,uneval)
S3method(ggplot_build,ggplot)
Expand Down
4 changes: 4 additions & 0 deletions R/plot-build.r
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ ggplot_build.ggplot <- function(plot) {
layer_data <- lapply(layers, function(y) y$layer_data(plot$data))

scales <- plot$scales
# Make sure all scales are provided with any parameters that were separately
# provided. Note: this excludes missing but required scales, which don't yet
# exist at this time.
scales$update_scales_params()
# Apply function to layer and matching data
by_layer <- function(f) {
out <- vector("list", length(data))
Expand Down
5 changes: 5 additions & 0 deletions R/plot-construction.r
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ ggplot_add.Scale <- function(object, plot, object_name) {
plot
}
#' @export
ggplot_add.scales_params <- function(object, plot, object_name) {
plot$scales$add_params(object)
plot
}
#' @export
ggplot_add.labels <- function(object, plot, object_name) {
update_labels(plot, object)
}
Expand Down
12 changes: 12 additions & 0 deletions R/scale-.r
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ binned_scale <- function(aesthetics, scale_name, palette, name = waiver(),
#' (for non-position scales) or when the `Layout` calculates the x and y labels
#' (position scales).
#'
#' - `update_params()` Method to feed scale parameters into scale object at the end of
#' plot construction. For ggplot2 internal use only.
#'
#' These methods are only valid for position (x and y) scales:
#'
#' - `dimension()` For continuous scales, the dimension is the same concept as the limits.
Expand Down Expand Up @@ -388,6 +391,8 @@ Scale <- ggproto("Scale", NULL,
guide = "legend",
position = "left",

update_params = function(self, params) {
},

is_discrete = function() {
abort("Not implemented")
Expand Down Expand Up @@ -548,6 +553,13 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
n.breaks = NULL,
trans = identity_trans(),

update_params = function(self, params) {
self$name <- params$name %||% self$name
self$breaks <- params$breaks %||% self$breaks
self$labels <- params$labels %||% self$labels
self$expand <- params$expand %||% self$expand
},

is_discrete = function() FALSE,

train = function(self, x) {
Expand Down
57 changes: 54 additions & 3 deletions R/scales-.r
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ scales_list <- function() {

ScalesList <- ggproto("ScalesList", NULL,
scales = NULL,
scales_params = list(),

find = function(self, aesthetic) {
vapply(self$scales, function(x) any(aesthetic %in% x$aesthetics), logical(1))
Expand Down Expand Up @@ -36,6 +37,33 @@ ScalesList <- ggproto("ScalesList", NULL,
self$scales <- c(self$scales[!prev_aes], list(scale))
},

# Save parameters for a scale, to be applied later
# via `update_scales_params()`. The `params` object
# should be created with `scales_params()`.
add_params = function(self, params) {
aesthetic <- params$aesthetic
self$scales_params[[aesthetic]] <-
defaults(params$params, self$scales_params[[aesthetic]])
},

# update the parameters for all scales currently stored
update_scales_params = function(self) {
for (scale in self$scales) {
self$update_params_for_scale(scale)
}
},

# update the parameters for one specific scale object
update_params_for_scale = function(self, scale) {
# get a list of all the params objects that are possibly relevant
params_list <- self$scales_params[scale$aesthetics]
# update the scale with each params object
lapply(params_list, function(x) {
if (!is.null(x)) scale$update_params(x)
})
invisible()
},

n = function(self) {
length(self$scales)
},
Expand Down Expand Up @@ -100,7 +128,12 @@ scales_add_defaults <- function(scales, data, aesthetics, env) {
datacols <- compact(datacols)

for (aes in names(datacols)) {
scales$add(find_scale(aes, datacols[[aes]], env))
# find the appropriate scale object
scale <- find_scale(aes, datacols[[aes]], env)
# make sure it has the latest available parameters
scales$update_params_for_scale(scale)
# add to scales list
scales$add(scale)
}

}
Expand All @@ -115,9 +148,27 @@ scales_add_missing <- function(plot, aesthetics, env) {
for (aes in aesthetics) {
scale_name <- paste("scale", aes, "continuous", sep = "_")

scale_f <- find_global(scale_name, env, mode = "function")
plot$scales$add(scale_f())
# find the appropriate scale object
scale_fun <- find_global(scale_name, env, mode = "function")
scale <- scale_fun()
# make sure it has the latest available parameters
scales$update_params_for_scale(scale)
# add to scales list
plot$scales$add(scale)
}
}


# create a data structure to store parameters to be added to scales later on
# @param aesthetic A single aesthetic, *not* a vector of aesthetics.
# @param ... Parameters to be provided to the scale.
scales_params <- function(aesthetic, ...) {
structure(
list(
aesthetic = aesthetic,
params = list(...)
),
class = "scales_params"
)
}

2 changes: 2 additions & 0 deletions man/ggplot2-ggproto.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.