Skip to content

Use new {scales} syntax #5544

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

Merged
merged 4 commits into from
Dec 4, 2023
Merged
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Imports:
MASS,
mgcv,
rlang (>= 1.1.0),
scales (>= 1.2.0),
scales (>= 1.3.0),
stats,
tibble,
vctrs (>= 0.5.0),
Expand Down
2 changes: 1 addition & 1 deletion R/axis-secondary.R
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
},

# Temporary scale for the purpose of calling break_info()
create_scale = function(self, range, trans = identity_trans()) {
create_scale = function(self, range, trans = transform_identity()) {
scale <- ggproto(NULL, ScaleContinuousPosition,
name = self$name,
breaks = self$breaks,
Expand Down
10 changes: 5 additions & 5 deletions R/coord-transform.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#' no guarantee that straight lines will continue to be straight.
#'
#' Transformations only work with continuous values: see
#' [scales::trans_new()] for list of transformations, and instructions
#' [scales::new_transform()] for list of transformations, and instructions
#' on how to create your own.
#'
#' @inheritParams coord_cartesian
Expand Down Expand Up @@ -60,7 +60,7 @@
#' geom_smooth(method = "lm") +
#' scale_x_log10() +
#' scale_y_log10() +
#' coord_trans(x = scales::exp_trans(10), y = scales::exp_trans(10))
#' coord_trans(x = scales::transform_exp(10), y = scales::transform_exp(10))
#'
#' # cf.
#' ggplot(diamonds, aes(carat, price)) +
Expand Down Expand Up @@ -90,8 +90,8 @@ coord_trans <- function(x = "identity", y = "identity", xlim = NULL, ylim = NULL
check_coord_limits(ylim)

# resolve transformers
if (is.character(x)) x <- as.trans(x)
if (is.character(y)) y <- as.trans(y)
if (is.character(x)) x <- as.transform(x)
if (is.character(y)) y <- as.transform(y)

ggproto(NULL, CoordTrans,
trans = list(x = x, y = y),
Expand Down Expand Up @@ -190,7 +190,7 @@ transform_value <- function(trans, value, range) {
# TODO: can we merge this with view_scales_from_scale()?
view_scales_from_scale_with_coord_trans <- function(scale, coord_limits, trans, expand = TRUE) {
expansion <- default_expansion(scale, expand = expand)
scale_trans <- scale$trans %||% identity_trans()
scale_trans <- scale$trans %||% transform_identity()
coord_limits <- coord_limits %||% scale_trans$inverse(c(NA, NA))
scale_limits <- scale$get_limits()

Expand Down
2 changes: 1 addition & 1 deletion R/guide-axis-logticks.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ GuideAxisLogticks <- ggproto(
"{.field {trans_name}} transformation in log-tick positioning."
))
}
trans <- log_trans(base = params$prescale_base)
trans <- transform_log(base = params$prescale_base)
} else {
trans <- scale$scale$trans
}
Expand Down
16 changes: 8 additions & 8 deletions R/scale-.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
#' that should be used for error messages associated with this scale.
#' @param palette A palette function that when called with a numeric vector with
#' values between 0 and 1 returns the corresponding output values
#' (e.g., [scales::area_pal()]).
#' (e.g., [scales::pal_area()]).
#' @param name The name of the scale. Used as the axis or legend title. If
#' `waiver()`, the default, the name of the scale is taken from the first
#' mapping used for that aesthetic. If `NULL`, the legend title will be
#' omitted.
#' @param breaks One of:
#' - `NULL` for no breaks
#' - `waiver()` for the default breaks computed by the
#' [transformation object][scales::trans_new()]
#' [transformation object][scales::new_transform()]
#' - A numeric vector of positions
#' - A function that takes the limits as input and returns breaks
#' as output (e.g., a function returned by [scales::extended_breaks()]).
Expand Down Expand Up @@ -75,8 +75,8 @@
#' and methods for generating breaks and labels. Transformation objects
#' are defined in the scales package, and are called `<name>_trans`. If
#' transformations require arguments, you can call them from the scales
#' package, e.g. [`scales::boxcox_trans(p = 2)`][scales::boxcox_trans].
#' You can create your own transformation with [scales::trans_new()].
#' package, e.g. [`scales::transform_boxcox(p = 2)`][scales::transform_boxcox].
#' You can create your own transformation with [scales::new_transform()].
#' @param guide A function used to create a guide or its name. See
#' [guides()] for more information.
#' @param expand For position scales, a vector of range expansion constants used to add some
Expand Down Expand Up @@ -113,7 +113,7 @@ continuous_scale <- function(aesthetics, scale_name = deprecated(), palette, nam
guide <- "none"
}

trans <- as.trans(trans)
trans <- as.transform(trans)
if (!is.null(limits) && !is.function(limits)) {
limits <- trans$transform(limits)
}
Expand Down Expand Up @@ -157,7 +157,7 @@ continuous_scale <- function(aesthetics, scale_name = deprecated(), palette, nam
#' @inheritParams continuous_scale
#' @param palette A palette function that when called with a single integer
#' argument (the number of levels in the scale) returns the values that
#' they should take (e.g., [scales::hue_pal()]).
#' they should take (e.g., [scales::pal_hue()]).
#' @param breaks One of:
#' - `NULL` for no breaks
#' - `waiver()` for the default breaks (the scale limits)
Expand Down Expand Up @@ -278,7 +278,7 @@ binned_scale <- function(aesthetics, scale_name = deprecated(), palette, name =
guide <- "none"
}

trans <- as.trans(trans)
trans <- as.transform(trans)
if (!is.null(limits)) {
limits <- trans$transform(limits)
}
Expand Down Expand Up @@ -603,7 +603,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
oob = censor,
minor_breaks = waiver(),
n.breaks = NULL,
trans = identity_trans(),
trans = transform_identity(),

is_discrete = function() FALSE,

Expand Down
8 changes: 4 additions & 4 deletions R/scale-alpha.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#' p + scale_alpha("cylinders")
#' p + scale_alpha(range = c(0.4, 0.8))
scale_alpha <- function(..., range = c(0.1, 1)) {
continuous_scale("alpha", palette = rescale_pal(range), ...)
continuous_scale("alpha", palette = pal_rescale(range), ...)
}

#' @rdname scale_alpha
Expand All @@ -34,7 +34,7 @@ scale_alpha_continuous <- scale_alpha
#' @rdname scale_alpha
#' @export
scale_alpha_binned <- function(..., range = c(0.1, 1)) {
binned_scale("alpha", palette = rescale_pal(range), ...)
binned_scale("alpha", palette = pal_rescale(range), ...)
}

#' @rdname scale_alpha
Expand All @@ -60,12 +60,12 @@ scale_alpha_ordinal <- function(..., range = c(0.1, 1)) {
#' @export
#' @usage NULL
scale_alpha_datetime <- function(..., range = c(0.1, 1)) {
datetime_scale("alpha", "time", palette = rescale_pal(range), ...)
datetime_scale("alpha", "time", palette = pal_rescale(range), ...)
}

#' @rdname scale_alpha
#' @export
#' @usage NULL
scale_alpha_date <- function(..., range = c(0.1, 1)){
datetime_scale("alpha", "date", palette = rescale_pal(range), ...)
datetime_scale("alpha", "date", palette = pal_rescale(range), ...)
}
20 changes: 10 additions & 10 deletions R/scale-brewer.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#'
#' @note
#' The `distiller` scales extend `brewer` scales by smoothly
#' interpolating 7 colours from any palette to a continuous scale.
#' interpolating 7 colours from any palette to a continuous scale.
#' The `distiller` scales have a default direction = -1. To reverse, use direction = 1.
#' The `fermenter` scales provide binned versions of the `brewer` scales.
#'
Expand All @@ -27,10 +27,10 @@
#' }
#' Modify the palette through the `palette` argument.
#'
#' @inheritParams scales::brewer_pal
#' @inheritParams scales::pal_brewer
#' @inheritParams scale_colour_hue
#' @inheritParams scale_colour_gradient
#' @inheritParams scales::gradient_n_pal
#' @inheritParams scales::pal_gradient_n
#' @param palette If a string, will use that named palette. If a number, will index into
#' the list of palettes of appropriate `type`. The list of available palettes can found
#' in the Palettes section.
Expand All @@ -52,7 +52,7 @@
#' # Change scale label
#' d + scale_colour_brewer("Diamond\nclarity")
#'
#' # Select brewer palette to use, see ?scales::brewer_pal for more details
#' # Select brewer palette to use, see ?scales::pal_brewer for more details
#' d + scale_colour_brewer(palette = "Greens")
#' d + scale_colour_brewer(palette = "Set1")
#'
Expand Down Expand Up @@ -84,13 +84,13 @@
#' v + scale_fill_fermenter()
#'
scale_colour_brewer <- function(..., type = "seq", palette = 1, direction = 1, aesthetics = "colour") {
discrete_scale(aesthetics, palette = brewer_pal(type, palette, direction), ...)
discrete_scale(aesthetics, palette = pal_brewer(type, palette, direction), ...)
}

#' @export
#' @rdname scale_brewer
scale_fill_brewer <- function(..., type = "seq", palette = 1, direction = 1, aesthetics = "fill") {
discrete_scale(aesthetics, palette = brewer_pal(type, palette, direction), ...)
discrete_scale(aesthetics, palette = pal_brewer(type, palette, direction), ...)
}

#' @export
Expand All @@ -106,7 +106,7 @@ scale_colour_distiller <- function(..., type = "seq", palette = 1, direction = -
}
continuous_scale(
aesthetics,
palette = gradient_n_pal(brewer_pal(type, palette, direction)(7), values, space),
palette = pal_gradient_n(pal_brewer(type, palette, direction)(7), values, space),
na.value = na.value, guide = guide, ...
)
# NB: 6-7 colours per palette gives nice gradients; more results in more saturated colours which do not look as good
Expand All @@ -125,7 +125,7 @@ scale_fill_distiller <- function(..., type = "seq", palette = 1, direction = -1,
}
continuous_scale(
aesthetics,
palette = gradient_n_pal(brewer_pal(type, palette, direction)(7), values, space),
palette = pal_gradient_n(pal_brewer(type, palette, direction)(7), values, space),
na.value = na.value, guide = guide, ...
)
}
Expand All @@ -141,7 +141,7 @@ scale_colour_fermenter <- function(..., type = "seq", palette = 1, direction = -
"i" = "Consider using {.code type = \"seq\"} or {.code type = \"div\"} instead"
))
}
binned_scale(aesthetics, palette = binned_pal(brewer_pal(type, palette, direction)), na.value = na.value, guide = guide, ...)
binned_scale(aesthetics, palette = pal_binned(pal_brewer(type, palette, direction)), na.value = na.value, guide = guide, ...)
}

#' @export
Expand All @@ -154,5 +154,5 @@ scale_fill_fermenter <- function(..., type = "seq", palette = 1, direction = -1,
"i" = "Consider using {.code type = \"seq\"} or {.code type = \"div\"} instead"
))
}
binned_scale(aesthetics, palette = binned_pal(brewer_pal(type, palette, direction)), na.value = na.value, guide = guide, ...)
binned_scale(aesthetics, palette = pal_binned(pal_brewer(type, palette, direction)), na.value = na.value, guide = guide, ...)
}
16 changes: 8 additions & 8 deletions R/scale-continuous.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@
#' p1 + scale_y_reverse()
#'
#' # Or you can supply a transformation in the `trans` argument:
#' p1 + scale_y_continuous(trans = scales::reciprocal_trans())
#' p1 + scale_y_continuous(trans = scales::transform_reciprocal())
#'
#' # You can also create your own. See ?scales::trans_new
#' # You can also create your own. See ?scales::new_transform
#'
#' @name scale_continuous
#' @aliases NULL
Expand Down Expand Up @@ -169,30 +169,30 @@ ScaleContinuousPosition <- ggproto("ScaleContinuousPosition", ScaleContinuous,
#' @rdname scale_continuous
#' @export
scale_x_log10 <- function(...) {
scale_x_continuous(..., trans = log10_trans())
scale_x_continuous(..., trans = transform_log10())
}
#' @rdname scale_continuous
#' @export
scale_y_log10 <- function(...) {
scale_y_continuous(..., trans = log10_trans())
scale_y_continuous(..., trans = transform_log10())
}
#' @rdname scale_continuous
#' @export
scale_x_reverse <- function(...) {
scale_x_continuous(..., trans = reverse_trans())
scale_x_continuous(..., trans = transform_reverse())
}
#' @rdname scale_continuous
#' @export
scale_y_reverse <- function(...) {
scale_y_continuous(..., trans = reverse_trans())
scale_y_continuous(..., trans = transform_reverse())
}
#' @rdname scale_continuous
#' @export
scale_x_sqrt <- function(...) {
scale_x_continuous(..., trans = sqrt_trans())
scale_x_continuous(..., trans = transform_sqrt())
}
#' @rdname scale_continuous
#' @export
scale_y_sqrt <- function(...) {
scale_y_continuous(..., trans = sqrt_trans())
scale_y_continuous(..., trans = transform_sqrt())
}
10 changes: 5 additions & 5 deletions R/scale-date.R
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ scale_x_time <- function(name = waiver(),
na.value = na.value,
guide = guide,
position = position,
trans = scales::hms_trans(),
trans = scales::transform_hms(),
sec.axis = sec.axis
)
}
Expand Down Expand Up @@ -273,7 +273,7 @@ scale_y_time <- function(name = waiver(),
na.value = na.value,
guide = guide,
position = position,
trans = scales::hms_trans(),
trans = scales::transform_hms(),
sec.axis = sec.axis
)
}
Expand Down Expand Up @@ -326,8 +326,8 @@ datetime_scale <- function(aesthetics, trans, palette,
}

trans <- switch(trans,
date = date_trans(),
time = time_trans(timezone)
date = transform_date(),
time = transform_time(timezone)
)

sc <- continuous_scale(
Expand Down Expand Up @@ -357,7 +357,7 @@ ScaleContinuousDatetime <- ggproto("ScaleContinuousDatetime", ScaleContinuous,
tz <- attr(x, "tzone")
if (is.null(self$timezone) && !is.null(tz)) {
self$timezone <- tz
self$trans <- time_trans(self$timezone)
self$trans <- transform_time(self$timezone)
}
ggproto_parent(ScaleContinuous, self)$transform(x)
},
Expand Down
4 changes: 2 additions & 2 deletions R/scale-expansion.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ expand_limits_discrete <- function(limits, expand = expansion(0, 0), coord_limit
}

expand_limits_continuous_trans <- function(limits, expand = expansion(0, 0),
coord_limits = c(NA, NA), trans = identity_trans()) {
coord_limits = c(NA, NA), trans = transform_identity()) {

# let non-NA coord_limits override the scale limits
limits <- ifelse(is.na(coord_limits), limits, coord_limits)
Expand Down Expand Up @@ -198,7 +198,7 @@ expand_limits_continuous_trans <- function(limits, expand = expansion(0, 0),
}

expand_limits_discrete_trans <- function(limits, expand = expansion(0, 0),
coord_limits = c(NA, NA), trans = identity_trans(),
coord_limits = c(NA, NA), trans = transform_identity(),
range_continuous = NULL) {
if (is.discrete(limits)) {
n_discrete_limits <- length(limits)
Expand Down
Loading