Skip to content
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 @@ -627,6 +627,7 @@ export(scale_linewidth_discrete)
export(scale_linewidth_identity)
export(scale_linewidth_manual)
export(scale_linewidth_ordinal)
export(scale_params)
export(scale_radius)
export(scale_shape)
export(scale_shape_binned)
Expand Down
33 changes: 33 additions & 0 deletions R/all-classes.R
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,39 @@ class_ggplot_built <- S7::new_class(
}
)

## Scale params -----------------------------------------------------------

#' Setting scale parameters
#'
#' @param aesthetics The name of the aesthetics for which to update the scale.
#' @param ... Named arguments to one of the scale constructors,
#' [`continuous_scale()`], [`discrete_scale()`] or [`binned_scale()`].
#'
#' @return A `ggplot2::scale_params` object that can be added to a plot.
#' @export
#'
#' @examples
#' ggplot(mpg, aes(displ, hwy)) +
#' geom_point() +
#' scale_params("x", limits = c(0, 10)) +
#' scale_params("y", transform = "sqrt")
scale_params <- S7::new_class(
"scale_params", parent = class_gg,
properties = list(
aesthetics = S7::class_character,
params = S7::class_list
),
constructor = function(aesthetics, ...) {
params <- list2(...)
# TODO: use name check if #6766 is merged
S7::new_object(
S7::S7_object(),
aesthetics = standardise_aes_names(aesthetics),
params = params
)
}
)

# Methods -----------------------------------------------------------------

#' @importFrom S7 convert
Expand Down
6 changes: 6 additions & 0 deletions R/plot-construction.R
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,12 @@ S7::method(update_ggplot, list(S7::new_S3_class("by"), class_ggplot)) <-
ggplot_add(unclass(object), plot, object_name)
}

S7::method(update_ggplot, list(scale_params, class_ggplot)) <-
function(object, plot, ...) {
plot$scales$add_params(object@aesthetics, object@params)
plot
}

# TODO: the S3 generic should be phased out once S7 is adopted more widely
# For backward compatibility, ggplot_add still exists but by default it wraps
# `update_ggplot()`
Expand Down
Loading