From e4b065b44714f5544aaa7a14ca5f930ba0c72ae3 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 4 Dec 2023 15:52:24 +0100 Subject: [PATCH 01/18] switch `trans` -> `transform` in constructors --- R/scale-.R | 33 +++++++++++++++++++++++---------- R/scale-date.R | 9 +++++---- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/R/scale-.R b/R/scale-.R index f49fb8e2c7..8aad9af97d 100644 --- a/R/scale-.R +++ b/R/scale-.R @@ -65,7 +65,7 @@ #' - [scales::squish()] for squishing out of bounds values into range. #' - [scales::squish_infinite()] for squishing infinite values into range. #' @param na.value Missing values will be replaced with this value. -#' @param trans For continuous scales, the name of a transformation object +#' @param transform For continuous scales, the name of a transformation object #' or the object itself. Built-in transformations include "asn", "atanh", #' "boxcox", "date", "exp", "hms", "identity", "log", "log10", "log1p", "log2", #' "logit", "modulus", "probability", "probit", "pseudo_log", "reciprocal", @@ -73,10 +73,12 @@ #' #' A transformation object bundles together a transform, its inverse, #' and methods for generating breaks and labels. Transformation objects -#' are defined in the scales package, and are called `_trans`. If +#' are defined in the scales package, and are called `transform_`. If #' transformations require arguments, you can call them from the scales #' package, e.g. [`scales::transform_boxcox(p = 2)`][scales::transform_boxcox]. #' You can create your own transformation with [scales::new_transform()]. +#' @param trans `r lifecycle::badge("deprecated")` Deprecated in favour of +#' `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 @@ -94,13 +96,18 @@ continuous_scale <- function(aesthetics, scale_name = deprecated(), palette, nam breaks = waiver(), minor_breaks = waiver(), n.breaks = NULL, labels = waiver(), limits = NULL, rescaler = rescale, oob = censor, expand = waiver(), na.value = NA_real_, - trans = "identity", guide = "legend", position = "left", + transform = "identity", trans = lifecycle::deprecated(), + guide = "legend", position = "left", call = caller_call(), super = ScaleContinuous) { call <- call %||% current_call() if (lifecycle::is_present(scale_name)) { deprecate_soft0("3.5.0", "continuous_scale(scale_name)") } + if (lifecycle::is_present(trans)) { + deprecate_soft0("3.5.0", "continuous_scale(trans)", "continuous_scale(transform)") + transform <- trans + } aesthetics <- standardise_aes_names(aesthetics) @@ -113,9 +120,9 @@ continuous_scale <- function(aesthetics, scale_name = deprecated(), palette, nam guide <- "none" } - trans <- as.transform(trans) + transform <- as.transform(transform) if (!is.null(limits) && !is.function(limits)) { - limits <- trans$transform(limits) + limits <- transform$transform(limits) } # Convert formula to function if appropriate @@ -134,7 +141,7 @@ continuous_scale <- function(aesthetics, scale_name = deprecated(), palette, nam range = ContinuousRange$new(), limits = limits, - trans = trans, + trans = transform, na.value = na.value, expand = expand, rescaler = rescaler, @@ -259,13 +266,19 @@ binned_scale <- function(aesthetics, scale_name = deprecated(), palette, name = breaks = waiver(), labels = waiver(), limits = NULL, rescaler = rescale, oob = squish, expand = waiver(), na.value = NA_real_, n.breaks = NULL, nice.breaks = TRUE, - right = TRUE, trans = "identity", show.limits = FALSE, + right = TRUE, transform = "identity", + trans = lifecycle::deprecated(), show.limits = FALSE, guide = "bins", position = "left", call = caller_call(), super = ScaleBinned) { if (lifecycle::is_present(scale_name)) { deprecate_soft0("3.5.0", "binned_scale(scale_name)") } + if (lifecycle::is_present(trans)) { + deprecate_soft0("3.5.0", "binned_scale(trans)", "binned_scale(transform)") + transform <- trans + } + call <- call %||% current_call() aesthetics <- standardise_aes_names(aesthetics) @@ -278,9 +291,9 @@ binned_scale <- function(aesthetics, scale_name = deprecated(), palette, name = guide <- "none" } - trans <- as.transform(trans) + transform <- as.transform(transform) if (!is.null(limits)) { - limits <- trans$transform(limits) + limits <- transform$transform(limits) } # Convert formula input to function if appropriate @@ -298,7 +311,7 @@ binned_scale <- function(aesthetics, scale_name = deprecated(), palette, name = range = ContinuousRange$new(), limits = limits, - trans = trans, + trans = transform, na.value = na.value, expand = expand, rescaler = rescaler, diff --git a/R/scale-date.R b/R/scale-date.R index 6dfc419a1a..df2445a2d1 100644 --- a/R/scale-date.R +++ b/R/scale-date.R @@ -288,8 +288,8 @@ scale_y_time <- function(name = waiver(), #' #' @export #' @keywords internal -datetime_scale <- function(aesthetics, trans, palette, - breaks = pretty_breaks(), minor_breaks = waiver(), +datetime_scale <- function(aesthetics, transform, trans = lifecycle::deprecated(), + palette, breaks = pretty_breaks(), minor_breaks = waiver(), labels = waiver(), date_breaks = waiver(), date_labels = waiver(), date_minor_breaks = waiver(), timezone = NULL, @@ -317,7 +317,7 @@ datetime_scale <- function(aesthetics, trans, palette, # ScaleContinuousDatetime; others use ScaleContinuous if (all(aesthetics %in% c("x", "xmin", "xmax", "xend", "y", "ymin", "ymax", "yend"))) { scale_class <- switch( - trans, + transform, date = ScaleContinuousDate, time = ScaleContinuousDatetime ) @@ -325,7 +325,7 @@ datetime_scale <- function(aesthetics, trans, palette, scale_class <- ScaleContinuous } - trans <- switch(trans, + transform <- switch(transform, date = transform_date(), time = transform_time(timezone) ) @@ -337,6 +337,7 @@ datetime_scale <- function(aesthetics, trans, palette, minor_breaks = minor_breaks, labels = labels, guide = guide, + transform = transform, trans = trans, call = call, ..., From ddd8084a3c51c95af12d6c3fb58bbccc14b36571 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 4 Dec 2023 15:53:19 +0100 Subject: [PATCH 02/18] swap `trans` -> `transform` in secondary axes --- R/axis-secondary.R | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/R/axis-secondary.R b/R/axis-secondary.R index 1d44fe967b..9aabb1b84f 100644 --- a/R/axis-secondary.R +++ b/R/axis-secondary.R @@ -94,14 +94,20 @@ #' ) #' #' @export -sec_axis <- function(trans = NULL, name = waiver(), breaks = waiver(), labels = waiver(), - guide = waiver()) { +sec_axis <- function(transform = NULL, + name = waiver(), breaks = waiver(), labels = waiver(), + guide = waiver(), trans = lifecycle::deprecated()) { + if (lifecycle::is_present(trans)) { + deprecate_soft0("3.5.0", "sec_axis(trans)", "sec_axis(transform)") + transform <- trans + } + # sec_axis() historically accepted two-sided formula, so be permissive. - if (length(trans) > 2) trans <- trans[c(1,3)] + if (length(transform) > 2) transform <- transform[c(1,3)] - trans <- as_function(trans) + transform <- as_function(transform) ggproto(NULL, AxisSecondary, - trans = trans, + trans = transform, name = name, breaks = breaks, labels = labels, @@ -111,8 +117,9 @@ sec_axis <- function(trans = NULL, name = waiver(), breaks = waiver(), labels = #' @rdname sec_axis #' #' @export -dup_axis <- function(trans = ~., name = derive(), breaks = derive(), labels = derive(), guide = derive()) { - sec_axis(trans, name, breaks, labels, guide) +dup_axis <- function(transform = ~., trans = lifecycle::deprecated(), + name = derive(), breaks = derive(), labels = derive(), guide = derive()) { + sec_axis(transform, trans = trans, name, breaks, labels, guide) } is.sec_axis <- function(x) { From 4cb5c611d59c0e10192f18144aa1679ee72b03e6 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 4 Dec 2023 15:54:02 +0100 Subject: [PATCH 03/18] migrate `trans` -> `transform` in scale functions --- R/scale-binned.R | 19 ++++++++++++------- R/scale-continuous.R | 30 ++++++++++++++++-------------- R/scale-date.R | 4 ++-- R/scale-linewidth.R | 15 +++++++++------ R/scale-size.R | 23 ++++++++++++++--------- 5 files changed, 53 insertions(+), 38 deletions(-) diff --git a/R/scale-binned.R b/R/scale-binned.R index 1fb5444696..14647cce1f 100644 --- a/R/scale-binned.R +++ b/R/scale-binned.R @@ -26,14 +26,17 @@ NULL scale_x_binned <- function(name = waiver(), n.breaks = 10, nice.breaks = TRUE, breaks = waiver(), labels = waiver(), limits = NULL, expand = waiver(), oob = squish, na.value = NA_real_, - right = TRUE, show.limits = FALSE, trans = "identity", + right = TRUE, show.limits = FALSE, transform = "identity", + trans = lifecycle::deprecated(), guide = waiver(), position = "bottom") { binned_scale( ggplot_global$x_aes, palette = identity, name = name, breaks = breaks, - labels = labels, limits = limits, expand = expand, oob = oob, na.value = na.value, - n.breaks = n.breaks, nice.breaks = nice.breaks, right = right, trans = trans, - show.limits = show.limits, guide = guide, position = position, super = ScaleBinnedPosition + labels = labels, limits = limits, expand = expand, oob = oob, + na.value = na.value, n.breaks = n.breaks, nice.breaks = nice.breaks, + right = right, transform = transform, trans = trans, + show.limits = show.limits, guide = guide, position = position, + super = ScaleBinnedPosition ) } @@ -43,14 +46,16 @@ scale_x_binned <- function(name = waiver(), n.breaks = 10, nice.breaks = TRUE, scale_y_binned <- function(name = waiver(), n.breaks = 10, nice.breaks = TRUE, breaks = waiver(), labels = waiver(), limits = NULL, expand = waiver(), oob = squish, na.value = NA_real_, - right = TRUE, show.limits = FALSE, trans = "identity", + right = TRUE, show.limits = FALSE, transform = "identity", + trans = lifecycle::deprecated(), guide = waiver(), position = "left") { binned_scale( ggplot_global$y_aes, palette = identity, name = name, breaks = breaks, labels = labels, limits = limits, expand = expand, oob = oob, na.value = na.value, - n.breaks = n.breaks, nice.breaks = nice.breaks, right = right, trans = trans, - show.limits = show.limits, guide = guide, position = position, super = ScaleBinnedPosition + n.breaks = n.breaks, nice.breaks = nice.breaks, right = right, + transform = transform, trans = trans, show.limits = show.limits, + guide = guide, position = position, super = ScaleBinnedPosition ) } diff --git a/R/scale-continuous.R b/R/scale-continuous.R index 002e03316a..da01d8f323 100644 --- a/R/scale-continuous.R +++ b/R/scale-continuous.R @@ -2,7 +2,7 @@ #' #' `scale_x_continuous()` and `scale_y_continuous()` are the default #' scales for continuous x and y aesthetics. There are three variants -#' that set the `trans` argument for commonly used transformations: +#' that set the `transform` argument for commonly used transformations: #' `scale_*_log10()`, `scale_*_sqrt()` and `scale_*_reverse()`. #' #' For simple manipulation of labels and limits, you may wish to use @@ -64,7 +64,7 @@ #' p1 + scale_y_reverse() #' #' # Or you can supply a transformation in the `trans` argument: -#' p1 + scale_y_continuous(trans = scales::transform_reciprocal()) +#' p1 + scale_y_continuous(transform = scales::transform_reciprocal()) #' #' # You can also create your own. See ?scales::new_transform #' @@ -81,7 +81,8 @@ scale_x_continuous <- function(name = waiver(), breaks = waiver(), minor_breaks = waiver(), n.breaks = NULL, labels = waiver(), limits = NULL, expand = waiver(), oob = censor, - na.value = NA_real_, trans = "identity", + na.value = NA_real_, transform = "identity", + trans = lifecycle::deprecated(), guide = waiver(), position = "bottom", sec.axis = waiver()) { call <- caller_call() @@ -92,8 +93,8 @@ scale_x_continuous <- function(name = waiver(), breaks = waiver(), ggplot_global$x_aes, palette = identity, name = name, breaks = breaks, n.breaks = n.breaks, minor_breaks = minor_breaks, labels = labels, limits = limits, - expand = expand, oob = oob, na.value = na.value, trans = trans, - guide = guide, position = position, call = call, + expand = expand, oob = oob, na.value = na.value, transform = transform, + trans = trans, guide = guide, position = position, call = call, super = ScaleContinuousPosition ) @@ -107,7 +108,8 @@ scale_y_continuous <- function(name = waiver(), breaks = waiver(), minor_breaks = waiver(), n.breaks = NULL, labels = waiver(), limits = NULL, expand = waiver(), oob = censor, - na.value = NA_real_, trans = "identity", + na.value = NA_real_, transform = "identity", + trans = lifecycle::deprecated(), guide = waiver(), position = "left", sec.axis = waiver()) { call <- caller_call() @@ -118,8 +120,8 @@ scale_y_continuous <- function(name = waiver(), breaks = waiver(), ggplot_global$y_aes, palette = identity, name = name, breaks = breaks, n.breaks = n.breaks, minor_breaks = minor_breaks, labels = labels, limits = limits, - expand = expand, oob = oob, na.value = na.value, trans = trans, - guide = guide, position = position, call = call, + expand = expand, oob = oob, na.value = na.value, transform = transform, + trans = trans, guide = guide, position = position, call = call, super = ScaleContinuousPosition ) @@ -169,30 +171,30 @@ ScaleContinuousPosition <- ggproto("ScaleContinuousPosition", ScaleContinuous, #' @rdname scale_continuous #' @export scale_x_log10 <- function(...) { - scale_x_continuous(..., trans = transform_log10()) + scale_x_continuous(..., transform = transform_log10()) } #' @rdname scale_continuous #' @export scale_y_log10 <- function(...) { - scale_y_continuous(..., trans = transform_log10()) + scale_y_continuous(..., transform = transform_log10()) } #' @rdname scale_continuous #' @export scale_x_reverse <- function(...) { - scale_x_continuous(..., trans = transform_reverse()) + scale_x_continuous(..., transform = transform_reverse()) } #' @rdname scale_continuous #' @export scale_y_reverse <- function(...) { - scale_y_continuous(..., trans = transform_reverse()) + scale_y_continuous(..., transform = transform_reverse()) } #' @rdname scale_continuous #' @export scale_x_sqrt <- function(...) { - scale_x_continuous(..., trans = transform_sqrt()) + scale_x_continuous(..., transform = transform_sqrt()) } #' @rdname scale_continuous #' @export scale_y_sqrt <- function(...) { - scale_y_continuous(..., trans = transform_sqrt()) + scale_y_continuous(..., transform = transform_sqrt()) } diff --git a/R/scale-date.R b/R/scale-date.R index df2445a2d1..3b77e1ecfd 100644 --- a/R/scale-date.R +++ b/R/scale-date.R @@ -242,7 +242,7 @@ scale_x_time <- function(name = waiver(), na.value = na.value, guide = guide, position = position, - trans = scales::transform_hms(), + transform = scales::transform_hms(), sec.axis = sec.axis ) } @@ -273,7 +273,7 @@ scale_y_time <- function(name = waiver(), na.value = na.value, guide = guide, position = position, - trans = scales::transform_hms(), + transform = scales::transform_hms(), sec.axis = sec.axis ) } diff --git a/R/scale-linewidth.R b/R/scale-linewidth.R index 71c87b199c..4fb086cd41 100644 --- a/R/scale-linewidth.R +++ b/R/scale-linewidth.R @@ -29,11 +29,12 @@ NULL #' @usage NULL scale_linewidth_continuous <- function(name = waiver(), breaks = waiver(), labels = waiver(), limits = NULL, - range = c(1, 6), trans = "identity", + range = c(1, 6), transform = "identity", + trans = lifecycle::deprecated(), guide = "legend") { continuous_scale("linewidth", palette = pal_rescale(range), name = name, - breaks = breaks, labels = labels, limits = limits, trans = trans, - guide = guide) + breaks = breaks, labels = labels, limits = limits, + transform = transform, trans = trans, guide = guide) } #' @rdname scale_linewidth @@ -44,10 +45,12 @@ scale_linewidth <- scale_linewidth_continuous #' @export scale_linewidth_binned <- function(name = waiver(), breaks = waiver(), labels = waiver(), limits = NULL, range = c(1, 6), n.breaks = NULL, - nice.breaks = TRUE, trans = "identity", guide = "bins") { + nice.breaks = TRUE, transform = "identity", + trans = lifecycle::deprecated(), guide = "bins") { binned_scale("linewidth", palette = pal_rescale(range), name = name, - breaks = breaks, labels = labels, limits = limits, trans = trans, - n.breaks = n.breaks, nice.breaks = nice.breaks, guide = guide) + breaks = breaks, labels = labels, limits = limits, + transform = transform, trans = trans, n.breaks = n.breaks, + nice.breaks = nice.breaks, guide = guide) } #' @rdname scale_linewidth diff --git a/R/scale-size.R b/R/scale-size.R index 07fd89f442..dad388ee9a 100644 --- a/R/scale-size.R +++ b/R/scale-size.R @@ -51,10 +51,12 @@ NULL #' @usage NULL scale_size_continuous <- function(name = waiver(), breaks = waiver(), labels = waiver(), limits = NULL, range = c(1, 6), - trans = "identity", guide = "legend") { + transform = "identity", + trans = lifecycle::deprecated(), + guide = "legend") { continuous_scale("size", palette = pal_area(range), name = name, - breaks = breaks, labels = labels, limits = limits, trans = trans, - guide = guide) + breaks = breaks, labels = labels, limits = limits, + transform = transform, trans = trans, guide = guide) } #' @rdname scale_size @@ -65,20 +67,23 @@ scale_size <- scale_size_continuous #' @export scale_radius <- function(name = waiver(), breaks = waiver(), labels = waiver(), limits = NULL, range = c(1, 6), - trans = "identity", guide = "legend") { + transform = "identity", trans = lifecycle::deprecated(), + guide = "legend") { continuous_scale("size", palette = pal_rescale(range), name = name, - breaks = breaks, labels = labels, limits = limits, trans = trans, - guide = guide) + breaks = breaks, labels = labels, limits = limits, transform = transform, + trans = trans, guide = guide) } #' @rdname scale_size #' @export scale_size_binned <- function(name = waiver(), breaks = waiver(), labels = waiver(), limits = NULL, range = c(1, 6), n.breaks = NULL, - nice.breaks = TRUE, trans = "identity", guide = "bins") { + nice.breaks = TRUE, transform = "identity", + trans = lifecycle::deprecated(), guide = "bins") { binned_scale("size", palette = pal_area(range), name = name, - breaks = breaks, labels = labels, limits = limits, trans = trans, - n.breaks = n.breaks, nice.breaks = nice.breaks, guide = guide) + breaks = breaks, labels = labels, limits = limits, + transform = transform, trans = trans, n.breaks = n.breaks, + nice.breaks = nice.breaks, guide = guide) } #' @rdname scale_size From 84b54c893b06c4979c4c5660cb7fb297e78a4eca Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 4 Dec 2023 15:54:22 +0100 Subject: [PATCH 04/18] propagate `trans` -> `transform` in other functions --- R/fortify-multcomp.R | 2 +- R/guide-axis-logticks.R | 2 +- R/limits.R | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R/fortify-multcomp.R b/R/fortify-multcomp.R index 0c79c75784..79714b2a68 100644 --- a/R/fortify-multcomp.R +++ b/R/fortify-multcomp.R @@ -22,7 +22,7 @@ #' ggplot(mapping = aes(lhs, estimate)) + #' geom_linerange(aes(ymin = lwr, ymax = upr), data = CI) + #' geom_point(aes(size = p), data = summary(wht)) + -#' scale_size(trans = "reverse") +#' scale_size(transform = "reverse") #' #' cld <- cld(wht) #' fortify(cld) diff --git a/R/guide-axis-logticks.R b/R/guide-axis-logticks.R index 699b52aee2..15f35c30bf 100644 --- a/R/guide-axis-logticks.R +++ b/R/guide-axis-logticks.R @@ -55,7 +55,7 @@ NULL #' geom_density() + #' scale_x_continuous( #' breaks = c(-10^(4:0), 0, 10^(0:4)), -#' trans = "pseudo_log" +#' transform = "pseudo_log" #' ) #' #' # The log ticks are mirrored when 0 is included diff --git a/R/limits.R b/R/limits.R index 727df98326..4fabb36a02 100644 --- a/R/limits.R +++ b/R/limits.R @@ -122,7 +122,7 @@ limits.numeric <- function(lims, var, call = caller_env()) { trans <- "identity" } - make_scale("continuous", var, limits = lims, trans = trans, call = call) + make_scale("continuous", var, limits = lims, transform = trans, call = call) } make_scale <- function(type, var, ..., call = NULL) { From 190ba179712ee33bfdf462a0fe6882e68c05d173 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 4 Dec 2023 15:55:40 +0100 Subject: [PATCH 05/18] adjust test verbiage --- tests/testthat/test-coord-transform.R | 2 +- tests/testthat/test-guides.R | 10 ++++++---- tests/testthat/test-plot-summary-api.R | 2 +- tests/testthat/test-scale-binned.R | 6 +++--- tests/testthat/test-scales-breaks-labels.R | 12 ++++++------ tests/testthat/test-scales.R | 18 +++++++++--------- tests/testthat/test-sec-axis.R | 19 ++++++++++--------- 7 files changed, 36 insertions(+), 33 deletions(-) diff --git a/tests/testthat/test-coord-transform.R b/tests/testthat/test-coord-transform.R index 1f58bdd5fe..abb05a3cae 100644 --- a/tests/testthat/test-coord-transform.R +++ b/tests/testthat/test-coord-transform.R @@ -113,7 +113,7 @@ test_that("second axes display in coord_trans()", { geom_point() + scale_y_continuous( sec.axis = sec_axis( - trans = ~log2(.), + transform = ~log2(.), breaks = c(3.5, 4, 4.5, 5, 5.5), name = "log2(hwy)" ), diff --git a/tests/testthat/test-guides.R b/tests/testthat/test-guides.R index 96c90efe96..29fbe98352 100644 --- a/tests/testthat/test-guides.R +++ b/tests/testthat/test-guides.R @@ -350,8 +350,8 @@ test_that("legend directions are set correctly", { test_that("guide_axis_logticks calculates appropriate ticks", { - test_scale <- function(trans = transform_identity(), limits = c(NA, NA)) { - scale <- scale_x_continuous(trans = trans) + test_scale <- function(transform = transform_identity(), limits = c(NA, NA)) { + scale <- scale_x_continuous(transform = transform) scale$train(scale$transform(limits)) view_scale_primary(scale) } @@ -631,8 +631,10 @@ test_that("logticks look as they should", { p <- ggplot(data.frame(x = c(-100, 100), y = c(10, 1000)), aes(x, y)) + geom_point() + - scale_y_continuous(trans = transform_compose(transform_log10(), transform_reverse()), - expand = expansion(add = 0.5)) + + scale_y_continuous( + transform = transform_compose(transform_log10(), transform_reverse()), + expand = expansion(add = 0.5) + ) + scale_x_continuous( breaks = c(-100, -10, -1, 0, 1, 10, 100) ) + diff --git a/tests/testthat/test-plot-summary-api.R b/tests/testthat/test-plot-summary-api.R index 2d45e990e7..9ce33632c6 100644 --- a/tests/testthat/test-plot-summary-api.R +++ b/tests/testthat/test-plot-summary-api.R @@ -85,7 +85,7 @@ test_that("layout summary - reversed scales", { }) test_that("layout summary - log scales", { - pl <- p + scale_x_log10() + scale_y_continuous(trans = "log2") + pl <- p + scale_x_log10() + scale_y_continuous(transform = "log2") ll <- summarise_layout(ggplot_build(pl)) expect_equal(ll$xscale[[1]]$trans$name, "log-10") expect_equal(ll$xscale[[1]]$trans$transform(100), 2) diff --git a/tests/testthat/test-scale-binned.R b/tests/testthat/test-scale-binned.R index af11caf879..44d19995d9 100644 --- a/tests/testthat/test-scale-binned.R +++ b/tests/testthat/test-scale-binned.R @@ -54,7 +54,7 @@ test_that("binned scales can use NAs in limits", { }) test_that("binned scales can calculate breaks with reverse transformation", { - scale <- scale_x_binned(trans = "reverse") + scale <- scale_x_binned(transform = "reverse") scale$train(c(1, 9)) expect_equal(scale$get_breaks(), 8:2) }) @@ -63,7 +63,7 @@ test_that('binned scales can calculate breaks on dates', { data <- seq(as.Date("2000-01-01"), as.Date("2020-01-01"), length.out = 100) - scale <- scale_x_binned(trans = "date") + scale <- scale_x_binned(transform = "date") scale$train(scale$transform(data)) breaks <- scale$trans$inverse(scale$get_breaks()) @@ -81,7 +81,7 @@ test_that('binned scales can calculate breaks on date-times', { length.out = 100 ) - scale <- scale_x_binned(trans = "time") + scale <- scale_x_binned(transform = "time") scale$train(scale$transform(data)) breaks <- scale$trans$inverse(scale$get_breaks()) diff --git a/tests/testthat/test-scales-breaks-labels.R b/tests/testthat/test-scales-breaks-labels.R index 344d823d58..bd44e6e72d 100644 --- a/tests/testthat/test-scales-breaks-labels.R +++ b/tests/testthat/test-scales-breaks-labels.R @@ -115,7 +115,7 @@ test_that("discrete labels match breaks", { }) test_that("scale breaks work with numeric log transformation", { - sc <- scale_x_continuous(limits = c(1, 1e5), trans = transform_log10()) + sc <- scale_x_continuous(limits = c(1, 1e5), transform = transform_log10()) expect_equal(sc$get_breaks(), c(0, 2, 4)) # 1, 100, 10000 expect_equal(sc$get_breaks_minor(), c(0, 1, 2, 3, 4, 5)) }) @@ -229,14 +229,14 @@ test_that("breaks can be specified by names of labels", { }) test_that("only finite or NA values for breaks for transformed scales (#871)", { - sc <- scale_y_continuous(limits = c(0.01, 0.99), trans = "probit", + sc <- scale_y_continuous(limits = c(0.01, 0.99), transform = "probit", breaks = seq(0, 1, 0.2)) breaks <- sc$get_breaks() expect_true(all(is.finite(breaks) | is.na(breaks))) }) test_that("minor breaks are transformed by scales", { - sc <- scale_y_continuous(limits = c(1, 100), trans = "log10", + sc <- scale_y_continuous(limits = c(1, 100), transform = "log10", minor_breaks = c(1, 10, 100)) expect_equal(sc$get_breaks_minor(), c(0, 1, 2)) @@ -297,15 +297,15 @@ test_that("minor breaks draw correctly", { expect_doppelganger("numeric-log", ggplot(df, aes(x_log, x_log)) + - scale_x_continuous(trans = transform_log2()) + + scale_x_continuous(transform = transform_log2()) + scale_y_log10() + labs(x = NULL, y = NULL) + theme ) expect_doppelganger("numeric-exp", ggplot(df, aes(x_num, x_num)) + - scale_x_continuous(trans = transform_exp(2)) + - scale_y_continuous(trans = transform_exp(2)) + + scale_x_continuous(transform = transform_exp(2)) + + scale_y_continuous(transform = transform_exp(2)) + labs(x = NULL, y = NULL) + theme ) diff --git a/tests/testthat/test-scales.R b/tests/testthat/test-scales.R index 70d5b7dd27..e325e654b4 100644 --- a/tests/testthat/test-scales.R +++ b/tests/testthat/test-scales.R @@ -460,8 +460,8 @@ test_that("staged aesthetics are backtransformed properly (#4155)", { test_that("numeric scale transforms can produce breaks", { - test_breaks <- function(trans, limits) { - scale <- scale_x_continuous(trans = trans) + test_breaks <- function(transform, limits) { + scale <- scale_x_continuous(transform = transform) scale$train(scale$transform(limits)) view <- view_scale_primary(scale) scale$trans$inverse(view$get_breaks()) @@ -650,27 +650,27 @@ test_that("scale functions accurately report their calls", { test_that("scale call is found accurately", { - call_template <- quote(scale_x_continuous(trans = "log10")) + call_template <- quote(scale_x_continuous(transform = "log10")) - sc <- do.call("scale_x_continuous", list(trans = "log10")) + sc <- do.call("scale_x_continuous", list(transform = "log10")) expect_equal(sc$call, call_template) - sc <- inject(scale_x_continuous(!!!list(trans = "log10"))) + sc <- inject(scale_x_continuous(!!!list(transform = "log10"))) expect_equal(sc$call, call_template) - sc <- exec("scale_x_continuous", trans = "log10") + sc <- exec("scale_x_continuous", transform = "log10") expect_equal(sc$call, call_template) - foo <- function() scale_x_continuous(trans = "log10") + foo <- function() scale_x_continuous(transform = "log10") expect_equal(foo()$call, call_template) env <- new_environment() - env$bar <- function() scale_x_continuous(trans = "log10") + env$bar <- function() scale_x_continuous(transform = "log10") expect_equal(env$bar()$call, call_template) # Now should recognise the outer function scale_x_new <- function() { - scale_x_continuous(trans = "log10") + scale_x_continuous(transform = "log10") } expect_equal( scale_x_new()$call, diff --git a/tests/testthat/test-sec-axis.R b/tests/testthat/test-sec-axis.R index 7dcba15139..bcef0ae7aa 100644 --- a/tests/testthat/test-sec-axis.R +++ b/tests/testthat/test-sec-axis.R @@ -127,7 +127,7 @@ test_that("custom breaks work", { scale_x_continuous( name = "Unit A", sec.axis = sec_axis( - trans = y ~ ., + transform = y ~ ., breaks = custom_breaks ) ) @@ -142,7 +142,7 @@ test_that("sec axis works with skewed transform", { ggplot(foo, aes(x, y)) + geom_point() + scale_x_continuous( - name = "Unit A", trans = "log", + name = "Unit A", transform = "log", breaks = c(0.001, 0.01, 0.1, 1, 10, 100, 1000), sec.axis = sec_axis(~ . * 100, name = "Unit B", @@ -192,7 +192,7 @@ test_that("sec_axis() handles secondary power transformations", { ) p <- ggplot(df, aes(x, y)) + geom_point() + - scale_y_continuous(sec.axis = sec_axis(trans = (~ 2^.))) + scale_y_continuous(sec.axis = sec_axis(transform = (~ 2^.))) scale <- layer_scales(p)$y breaks <- scale$break_info() @@ -244,11 +244,11 @@ test_that("sec_axis() respects custom transformations", { ggplot(dat, aes(x = x, y = y)) + geom_line(linewidth = 1, na.rm = T) + scale_y_continuous( - trans = + transform = magnify_trans_log(interval_low = 0.5, interval_high = 1, reducer = 0.5, reducer2 = 8), breaks = c(0.001, 0.01, 0.1, 0.5, 0.6, 0.7, 0.8, 0.9, 1), limits = c(0.001, 1), sec.axis = sec_axis( - trans = + transform = ~ . * (1 / 2), breaks = c(0.001, 0.01, 0.1, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5) ) ) + theme_linedraw() @@ -335,8 +335,8 @@ test_that("sec.axis allows independent trans btwn primary and secondary axes", { "sec_axis, independent transformations", ggplot(data = data, aes(Probability, Value)) + geom_point() + scale_x_continuous( - trans = scales::transform_probability(distribution = "norm", lower.tail = FALSE), - sec.axis = sec_axis(trans = ~ 1 / ., name = "Return Period") + transform = scales::transform_probability(distribution = "norm", lower.tail = FALSE), + sec.axis = sec_axis(transform = ~ 1 / ., name = "Return Period") ) + theme_linedraw() ) }) @@ -351,7 +351,8 @@ test_that("sec_axis() works for power transformations (monotonicity test doesn't "sec axis monotonicity test", ggplot(data, aes(x, y)) + geom_line() + - scale_y_continuous(trans = "sqrt", sec.axis = dup_axis()) + theme_linedraw() + scale_y_continuous(transform = "sqrt", sec.axis = dup_axis()) + + theme_linedraw() ) testdat <- data_frame( @@ -360,7 +361,7 @@ test_that("sec_axis() works for power transformations (monotonicity test doesn't ) p <- ggplot(data = testdat, aes(x = x, y = y)) + geom_point() + - scale_y_continuous(sec.axis = sec_axis(trans = ~ .^0.5)) + scale_y_continuous(sec.axis = sec_axis(transform = ~ .^0.5)) scale <- layer_scales(p)$y breaks <- scale$break_info() expect_equal(breaks$major, sqrt(breaks$sec.major), tolerance = .005) From 43606d3076a23a8ae70d6e2ba7531efe04d5fd22 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 4 Dec 2023 15:56:09 +0100 Subject: [PATCH 06/18] reoxygenate --- man/binned_scale.Rd | 10 +++++++--- man/continuous_scale.Rd | 10 +++++++--- man/datetime_scale.Rd | 16 +++++++++++++++- man/fortify-multcomp.Rd | 2 +- man/guide_axis_logticks.Rd | 2 +- man/scale_binned.Rd | 13 +++++++++---- man/scale_continuous.Rd | 17 +++++++++++------ man/scale_gradient.Rd | 6 ++++-- man/scale_linewidth.Rd | 13 +++++++++---- man/scale_size.Rd | 16 +++++++++++----- man/scale_steps.Rd | 6 ++++-- man/sec_axis.Rd | 12 +++++++----- 12 files changed, 86 insertions(+), 37 deletions(-) diff --git a/man/binned_scale.Rd b/man/binned_scale.Rd index 31d70663f0..591429cbfb 100644 --- a/man/binned_scale.Rd +++ b/man/binned_scale.Rd @@ -19,7 +19,8 @@ binned_scale( n.breaks = NULL, nice.breaks = TRUE, right = TRUE, - trans = "identity", + transform = "identity", + trans = lifecycle::deprecated(), show.limits = FALSE, guide = "bins", position = "left", @@ -120,7 +121,7 @@ means that values at break positions are part of the lower bin (open on the left), whereas they are part of the upper bin when intervals are closed on the left (open on the right).} -\item{trans}{For continuous scales, the name of a transformation object +\item{transform}{For continuous scales, the name of a transformation object or the object itself. Built-in transformations include "asn", "atanh", "boxcox", "date", "exp", "hms", "identity", "log", "log10", "log1p", "log2", "logit", "modulus", "probability", "probit", "pseudo_log", "reciprocal", @@ -128,11 +129,14 @@ or the object itself. Built-in transformations include "asn", "atanh", A transformation object bundles together a transform, its inverse, and methods for generating breaks and labels. Transformation objects -are defined in the scales package, and are called \verb{_trans}. If +are defined in the scales package, and are called \verb{transform_}. If transformations require arguments, you can call them from the scales package, e.g. \code{\link[scales:transform_boxcox]{scales::transform_boxcox(p = 2)}}. You can create your own transformation with \code{\link[scales:new_transform]{scales::new_transform()}}.} +\item{trans}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} Deprecated in favour of +\code{transform}.} + \item{show.limits}{should the limits of the scale appear as ticks} \item{guide}{A function used to create a guide or its name. See diff --git a/man/continuous_scale.Rd b/man/continuous_scale.Rd index 407916b251..8e9dde8f6d 100644 --- a/man/continuous_scale.Rd +++ b/man/continuous_scale.Rd @@ -18,7 +18,8 @@ continuous_scale( oob = censor, expand = waiver(), na.value = NA_real_, - trans = "identity", + transform = "identity", + trans = lifecycle::deprecated(), guide = "legend", position = "left", call = caller_call(), @@ -118,7 +119,7 @@ expand the scale by 5\% on each side for continuous variables, and by \item{na.value}{Missing values will be replaced with this value.} -\item{trans}{For continuous scales, the name of a transformation object +\item{transform}{For continuous scales, the name of a transformation object or the object itself. Built-in transformations include "asn", "atanh", "boxcox", "date", "exp", "hms", "identity", "log", "log10", "log1p", "log2", "logit", "modulus", "probability", "probit", "pseudo_log", "reciprocal", @@ -126,11 +127,14 @@ or the object itself. Built-in transformations include "asn", "atanh", A transformation object bundles together a transform, its inverse, and methods for generating breaks and labels. Transformation objects -are defined in the scales package, and are called \verb{_trans}. If +are defined in the scales package, and are called \verb{transform_}. If transformations require arguments, you can call them from the scales package, e.g. \code{\link[scales:transform_boxcox]{scales::transform_boxcox(p = 2)}}. You can create your own transformation with \code{\link[scales:new_transform]{scales::new_transform()}}.} +\item{trans}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} Deprecated in favour of +\code{transform}.} + \item{guide}{A function used to create a guide or its name. See \code{\link[=guides]{guides()}} for more information.} diff --git a/man/datetime_scale.Rd b/man/datetime_scale.Rd index 117d04b013..b2a6330489 100644 --- a/man/datetime_scale.Rd +++ b/man/datetime_scale.Rd @@ -6,7 +6,8 @@ \usage{ datetime_scale( aesthetics, - trans, + transform, + trans = lifecycle::deprecated(), palette, breaks = pretty_breaks(), minor_breaks = waiver(), @@ -23,6 +24,19 @@ datetime_scale( \arguments{ \item{aesthetics}{The names of the aesthetics that this scale works with.} +\item{transform}{For continuous scales, the name of a transformation object +or the object itself. Built-in transformations include "asn", "atanh", +"boxcox", "date", "exp", "hms", "identity", "log", "log10", "log1p", "log2", +"logit", "modulus", "probability", "probit", "pseudo_log", "reciprocal", +"reverse", "sqrt" and "time". + +A transformation object bundles together a transform, its inverse, +and methods for generating breaks and labels. Transformation objects +are defined in the scales package, and are called \verb{transform_}. If +transformations require arguments, you can call them from the scales +package, e.g. \code{\link[scales:transform_boxcox]{scales::transform_boxcox(p = 2)}}. +You can create your own transformation with \code{\link[scales:new_transform]{scales::new_transform()}}.} + \item{trans}{For date/time scales, the name of a date/time transformation or the object itself. Built-in transformations include "hms", "date" and "time".} diff --git a/man/fortify-multcomp.Rd b/man/fortify-multcomp.Rd index cb0eb1fb09..a52dec001c 100644 --- a/man/fortify-multcomp.Rd +++ b/man/fortify-multcomp.Rd @@ -42,7 +42,7 @@ fortify(summary(wht)) ggplot(mapping = aes(lhs, estimate)) + geom_linerange(aes(ymin = lwr, ymax = upr), data = CI) + geom_point(aes(size = p), data = summary(wht)) + - scale_size(trans = "reverse") + scale_size(transform = "reverse") cld <- cld(wht) fortify(cld) diff --git a/man/guide_axis_logticks.Rd b/man/guide_axis_logticks.Rd index 60ebaa8b12..94b3b77362 100644 --- a/man/guide_axis_logticks.Rd +++ b/man/guide_axis_logticks.Rd @@ -104,7 +104,7 @@ p2 <- ggplot(data.frame(x = rcauchy(1000)), aes(x = x)) + geom_density() + scale_x_continuous( breaks = c(-10^(4:0), 0, 10^(0:4)), - trans = "pseudo_log" + transform = "pseudo_log" ) # The log ticks are mirrored when 0 is included diff --git a/man/scale_binned.Rd b/man/scale_binned.Rd index 0046d8fbcd..ad71b75fc1 100644 --- a/man/scale_binned.Rd +++ b/man/scale_binned.Rd @@ -17,7 +17,8 @@ scale_x_binned( na.value = NA_real_, right = TRUE, show.limits = FALSE, - trans = "identity", + transform = "identity", + trans = lifecycle::deprecated(), guide = waiver(), position = "bottom" ) @@ -34,7 +35,8 @@ scale_y_binned( na.value = NA_real_, right = TRUE, show.limits = FALSE, - trans = "identity", + transform = "identity", + trans = lifecycle::deprecated(), guide = waiver(), position = "left" ) @@ -118,7 +120,7 @@ the left (open on the right).} \item{show.limits}{should the limits of the scale appear as ticks} -\item{trans}{For continuous scales, the name of a transformation object +\item{transform}{For continuous scales, the name of a transformation object or the object itself. Built-in transformations include "asn", "atanh", "boxcox", "date", "exp", "hms", "identity", "log", "log10", "log1p", "log2", "logit", "modulus", "probability", "probit", "pseudo_log", "reciprocal", @@ -126,11 +128,14 @@ or the object itself. Built-in transformations include "asn", "atanh", A transformation object bundles together a transform, its inverse, and methods for generating breaks and labels. Transformation objects -are defined in the scales package, and are called \verb{_trans}. If +are defined in the scales package, and are called \verb{transform_}. If transformations require arguments, you can call them from the scales package, e.g. \code{\link[scales:transform_boxcox]{scales::transform_boxcox(p = 2)}}. You can create your own transformation with \code{\link[scales:new_transform]{scales::new_transform()}}.} +\item{trans}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} Deprecated in favour of +\code{transform}.} + \item{guide}{A function used to create a guide or its name. See \code{\link[=guides]{guides()}} for more information.} diff --git a/man/scale_continuous.Rd b/man/scale_continuous.Rd index ec91afc919..11221addac 100644 --- a/man/scale_continuous.Rd +++ b/man/scale_continuous.Rd @@ -21,7 +21,8 @@ scale_x_continuous( expand = waiver(), oob = censor, na.value = NA_real_, - trans = "identity", + transform = "identity", + trans = lifecycle::deprecated(), guide = waiver(), position = "bottom", sec.axis = waiver() @@ -37,7 +38,8 @@ scale_y_continuous( expand = waiver(), oob = censor, na.value = NA_real_, - trans = "identity", + transform = "identity", + trans = lifecycle::deprecated(), guide = waiver(), position = "left", sec.axis = waiver() @@ -132,7 +134,7 @@ bounds values with \code{NA}. \item{na.value}{Missing values will be replaced with this value.} -\item{trans}{For continuous scales, the name of a transformation object +\item{transform}{For continuous scales, the name of a transformation object or the object itself. Built-in transformations include "asn", "atanh", "boxcox", "date", "exp", "hms", "identity", "log", "log10", "log1p", "log2", "logit", "modulus", "probability", "probit", "pseudo_log", "reciprocal", @@ -140,11 +142,14 @@ or the object itself. Built-in transformations include "asn", "atanh", A transformation object bundles together a transform, its inverse, and methods for generating breaks and labels. Transformation objects -are defined in the scales package, and are called \verb{_trans}. If +are defined in the scales package, and are called \verb{transform_}. If transformations require arguments, you can call them from the scales package, e.g. \code{\link[scales:transform_boxcox]{scales::transform_boxcox(p = 2)}}. You can create your own transformation with \code{\link[scales:new_transform]{scales::new_transform()}}.} +\item{trans}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} Deprecated in favour of +\code{transform}.} + \item{guide}{A function used to create a guide or its name. See \code{\link[=guides]{guides()}} for more information.} @@ -158,7 +163,7 @@ You can create your own transformation with \code{\link[scales:new_transform]{sc \description{ \code{scale_x_continuous()} and \code{scale_y_continuous()} are the default scales for continuous x and y aesthetics. There are three variants -that set the \code{trans} argument for commonly used transformations: +that set the \code{transform} argument for commonly used transformations: \verb{scale_*_log10()}, \verb{scale_*_sqrt()} and \verb{scale_*_reverse()}. } \details{ @@ -215,7 +220,7 @@ p1 + scale_y_sqrt() p1 + scale_y_reverse() # Or you can supply a transformation in the `trans` argument: -p1 + scale_y_continuous(trans = scales::transform_reciprocal()) +p1 + scale_y_continuous(transform = scales::transform_reciprocal()) # You can also create your own. See ?scales::new_transform diff --git a/man/scale_gradient.Rd b/man/scale_gradient.Rd index cea9e781f9..059ba12d87 100644 --- a/man/scale_gradient.Rd +++ b/man/scale_gradient.Rd @@ -159,7 +159,7 @@ bounds values with \code{NA}. \item \code{\link[scales:oob]{scales::squish()}} for squishing out of bounds values into range. \item \code{\link[scales:oob]{scales::squish_infinite()}} for squishing infinite values into range. }} - \item{\code{trans}}{For continuous scales, the name of a transformation object + \item{\code{transform}}{For continuous scales, the name of a transformation object or the object itself. Built-in transformations include "asn", "atanh", "boxcox", "date", "exp", "hms", "identity", "log", "log10", "log1p", "log2", "logit", "modulus", "probability", "probit", "pseudo_log", "reciprocal", @@ -167,10 +167,12 @@ or the object itself. Built-in transformations include "asn", "atanh", A transformation object bundles together a transform, its inverse, and methods for generating breaks and labels. Transformation objects -are defined in the scales package, and are called \verb{_trans}. If +are defined in the scales package, and are called \verb{transform_}. If transformations require arguments, you can call them from the scales package, e.g. \code{\link[scales:transform_boxcox]{scales::transform_boxcox(p = 2)}}. You can create your own transformation with \code{\link[scales:new_transform]{scales::new_transform()}}.} + \item{\code{trans}}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} Deprecated in favour of +\code{transform}.} \item{\code{expand}}{For position scales, a vector of range expansion constants used to add some padding around the data to ensure that they are placed some distance away from the axes. Use the convenience function \code{\link[=expansion]{expansion()}} diff --git a/man/scale_linewidth.Rd b/man/scale_linewidth.Rd index e699c24441..51c2d1d189 100644 --- a/man/scale_linewidth.Rd +++ b/man/scale_linewidth.Rd @@ -16,7 +16,8 @@ scale_linewidth( labels = waiver(), limits = NULL, range = c(1, 6), - trans = "identity", + transform = "identity", + trans = lifecycle::deprecated(), guide = "legend" ) @@ -28,7 +29,8 @@ scale_linewidth_binned( range = c(1, 6), n.breaks = NULL, nice.breaks = TRUE, - trans = "identity", + transform = "identity", + trans = lifecycle::deprecated(), guide = "bins" ) } @@ -77,7 +79,7 @@ If the purpose is to zoom, use the limit argument in the coordinate system \item{range}{a numeric vector of length 2 that specifies the minimum and maximum size of the plotting symbol after transformation.} -\item{trans}{For continuous scales, the name of a transformation object +\item{transform}{For continuous scales, the name of a transformation object or the object itself. Built-in transformations include "asn", "atanh", "boxcox", "date", "exp", "hms", "identity", "log", "log10", "log1p", "log2", "logit", "modulus", "probability", "probit", "pseudo_log", "reciprocal", @@ -85,11 +87,14 @@ or the object itself. Built-in transformations include "asn", "atanh", A transformation object bundles together a transform, its inverse, and methods for generating breaks and labels. Transformation objects -are defined in the scales package, and are called \verb{_trans}. If +are defined in the scales package, and are called \verb{transform_}. If transformations require arguments, you can call them from the scales package, e.g. \code{\link[scales:transform_boxcox]{scales::transform_boxcox(p = 2)}}. You can create your own transformation with \code{\link[scales:new_transform]{scales::new_transform()}}.} +\item{trans}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} Deprecated in favour of +\code{transform}.} + \item{guide}{A function used to create a guide or its name. See \code{\link[=guides]{guides()}} for more information.} diff --git a/man/scale_size.Rd b/man/scale_size.Rd index 408493113f..6be10e6f3e 100644 --- a/man/scale_size.Rd +++ b/man/scale_size.Rd @@ -19,7 +19,8 @@ scale_size( labels = waiver(), limits = NULL, range = c(1, 6), - trans = "identity", + transform = "identity", + trans = lifecycle::deprecated(), guide = "legend" ) @@ -29,7 +30,8 @@ scale_radius( labels = waiver(), limits = NULL, range = c(1, 6), - trans = "identity", + transform = "identity", + trans = lifecycle::deprecated(), guide = "legend" ) @@ -41,7 +43,8 @@ scale_size_binned( range = c(1, 6), n.breaks = NULL, nice.breaks = TRUE, - trans = "identity", + transform = "identity", + trans = lifecycle::deprecated(), guide = "bins" ) @@ -94,7 +97,7 @@ If the purpose is to zoom, use the limit argument in the coordinate system \item{range}{a numeric vector of length 2 that specifies the minimum and maximum size of the plotting symbol after transformation.} -\item{trans}{For continuous scales, the name of a transformation object +\item{transform}{For continuous scales, the name of a transformation object or the object itself. Built-in transformations include "asn", "atanh", "boxcox", "date", "exp", "hms", "identity", "log", "log10", "log1p", "log2", "logit", "modulus", "probability", "probit", "pseudo_log", "reciprocal", @@ -102,11 +105,14 @@ or the object itself. Built-in transformations include "asn", "atanh", A transformation object bundles together a transform, its inverse, and methods for generating breaks and labels. Transformation objects -are defined in the scales package, and are called \verb{_trans}. If +are defined in the scales package, and are called \verb{transform_}. If transformations require arguments, you can call them from the scales package, e.g. \code{\link[scales:transform_boxcox]{scales::transform_boxcox(p = 2)}}. You can create your own transformation with \code{\link[scales:new_transform]{scales::new_transform()}}.} +\item{trans}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} Deprecated in favour of +\code{transform}.} + \item{guide}{A function used to create a guide or its name. See \code{\link[=guides]{guides()}} for more information.} diff --git a/man/scale_steps.Rd b/man/scale_steps.Rd index a7906d8c4a..e8aab0a473 100644 --- a/man/scale_steps.Rd +++ b/man/scale_steps.Rd @@ -142,7 +142,7 @@ bounds values with \code{NA}. \item \code{\link[scales:oob]{scales::squish()}} for squishing out of bounds values into range. \item \code{\link[scales:oob]{scales::squish_infinite()}} for squishing infinite values into range. }} - \item{\code{trans}}{For continuous scales, the name of a transformation object + \item{\code{transform}}{For continuous scales, the name of a transformation object or the object itself. Built-in transformations include "asn", "atanh", "boxcox", "date", "exp", "hms", "identity", "log", "log10", "log1p", "log2", "logit", "modulus", "probability", "probit", "pseudo_log", "reciprocal", @@ -150,10 +150,12 @@ or the object itself. Built-in transformations include "asn", "atanh", A transformation object bundles together a transform, its inverse, and methods for generating breaks and labels. Transformation objects -are defined in the scales package, and are called \verb{_trans}. If +are defined in the scales package, and are called \verb{transform_}. If transformations require arguments, you can call them from the scales package, e.g. \code{\link[scales:transform_boxcox]{scales::transform_boxcox(p = 2)}}. You can create your own transformation with \code{\link[scales:new_transform]{scales::new_transform()}}.} + \item{\code{trans}}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} Deprecated in favour of +\code{transform}.} \item{\code{expand}}{For position scales, a vector of range expansion constants used to add some padding around the data to ensure that they are placed some distance away from the axes. Use the convenience function \code{\link[=expansion]{expansion()}} diff --git a/man/sec_axis.Rd b/man/sec_axis.Rd index df69ba8e65..a1c5678e0b 100644 --- a/man/sec_axis.Rd +++ b/man/sec_axis.Rd @@ -7,15 +7,17 @@ \title{Specify a secondary axis} \usage{ sec_axis( - trans = NULL, + transform = NULL, name = waiver(), breaks = waiver(), labels = waiver(), - guide = waiver() + guide = waiver(), + trans = lifecycle::deprecated() ) dup_axis( - trans = ~., + transform = ~., + trans = lifecycle::deprecated(), name = derive(), breaks = derive(), labels = derive(), @@ -25,8 +27,6 @@ dup_axis( derive() } \arguments{ -\item{trans}{A formula or function of transformation} - \item{name}{The name of the secondary axis} \item{breaks}{One of: @@ -47,6 +47,8 @@ derive() \item{guide}{A position guide that will be used to render the axis on the plot. Usually this is \code{\link[=guide_axis]{guide_axis()}}.} + +\item{trans}{A formula or function of transformation} } \description{ This function is used in conjunction with a position scale to create a From 986373bce7c1ae0c27ae6769bf2442b39ef5b6c1 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 4 Dec 2023 15:58:15 +0100 Subject: [PATCH 07/18] add news bullet --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS.md b/NEWS.md index 359d54ffb9..ec1f5d6ac9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # ggplot2 (development version) +* The `trans` argument in scales and secondary axes has been renamed to + `transform`. The `trans` argument itself is deprecated (#5558). + * Contour functions will not fail when `options("OutDec")` is not `.` (@eliocamp, #5555). * The `legend.key` theme element is set to inherit from the `panel.background` From b2f5ecc993c0f0f080e04fabac7828b8d9fb73a9 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 4 Dec 2023 16:17:31 +0100 Subject: [PATCH 08/18] document sec axis --- R/axis-secondary.R | 4 +++- man/sec_axis.Rd | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/R/axis-secondary.R b/R/axis-secondary.R index 9aabb1b84f..a384b2c69d 100644 --- a/R/axis-secondary.R +++ b/R/axis-secondary.R @@ -4,7 +4,9 @@ #' secondary axis, positioned opposite of the primary axis. All secondary #' axes must be based on a one-to-one transformation of the primary axes. #' -#' @param trans A formula or function of transformation +#' @param transform A formula or function of transformation +#' +#' @param trans `r lifecycle::badge("deprecated")` #' #' @param name The name of the secondary axis #' diff --git a/man/sec_axis.Rd b/man/sec_axis.Rd index a1c5678e0b..10d9d3d598 100644 --- a/man/sec_axis.Rd +++ b/man/sec_axis.Rd @@ -27,6 +27,8 @@ dup_axis( derive() } \arguments{ +\item{transform}{A formula or function of transformation} + \item{name}{The name of the secondary axis} \item{breaks}{One of: @@ -48,7 +50,7 @@ derive() \item{guide}{A position guide that will be used to render the axis on the plot. Usually this is \code{\link[=guide_axis]{guide_axis()}}.} -\item{trans}{A formula or function of transformation} +\item{trans}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}}} } \description{ This function is used in conjunction with a position scale to create a From a121505c9849120e089f84f071269a49db537efb Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 5 Dec 2023 08:59:09 +0100 Subject: [PATCH 09/18] don't namespace deprecated --- R/axis-secondary.R | 4 ++-- R/scale-.R | 4 ++-- R/scale-binned.R | 4 ++-- R/scale-continuous.R | 4 ++-- R/scale-date.R | 2 +- R/scale-linewidth.R | 4 ++-- R/scale-size.R | 6 +++--- man/binned_scale.Rd | 2 +- man/continuous_scale.Rd | 2 +- man/datetime_scale.Rd | 2 +- man/scale_binned.Rd | 4 ++-- man/scale_continuous.Rd | 4 ++-- man/scale_linewidth.Rd | 4 ++-- man/scale_size.Rd | 6 +++--- man/sec_axis.Rd | 4 ++-- 15 files changed, 28 insertions(+), 28 deletions(-) diff --git a/R/axis-secondary.R b/R/axis-secondary.R index a384b2c69d..8633b134a1 100644 --- a/R/axis-secondary.R +++ b/R/axis-secondary.R @@ -98,7 +98,7 @@ #' @export sec_axis <- function(transform = NULL, name = waiver(), breaks = waiver(), labels = waiver(), - guide = waiver(), trans = lifecycle::deprecated()) { + guide = waiver(), trans = deprecated()) { if (lifecycle::is_present(trans)) { deprecate_soft0("3.5.0", "sec_axis(trans)", "sec_axis(transform)") transform <- trans @@ -119,7 +119,7 @@ sec_axis <- function(transform = NULL, #' @rdname sec_axis #' #' @export -dup_axis <- function(transform = ~., trans = lifecycle::deprecated(), +dup_axis <- function(transform = ~., trans = deprecated(), name = derive(), breaks = derive(), labels = derive(), guide = derive()) { sec_axis(transform, trans = trans, name, breaks, labels, guide) } diff --git a/R/scale-.R b/R/scale-.R index 8aad9af97d..0215f563da 100644 --- a/R/scale-.R +++ b/R/scale-.R @@ -96,7 +96,7 @@ continuous_scale <- function(aesthetics, scale_name = deprecated(), palette, nam breaks = waiver(), minor_breaks = waiver(), n.breaks = NULL, labels = waiver(), limits = NULL, rescaler = rescale, oob = censor, expand = waiver(), na.value = NA_real_, - transform = "identity", trans = lifecycle::deprecated(), + transform = "identity", trans = deprecated(), guide = "legend", position = "left", call = caller_call(), super = ScaleContinuous) { @@ -267,7 +267,7 @@ binned_scale <- function(aesthetics, scale_name = deprecated(), palette, name = rescaler = rescale, oob = squish, expand = waiver(), na.value = NA_real_, n.breaks = NULL, nice.breaks = TRUE, right = TRUE, transform = "identity", - trans = lifecycle::deprecated(), show.limits = FALSE, + trans = deprecated(), show.limits = FALSE, guide = "bins", position = "left", call = caller_call(), super = ScaleBinned) { diff --git a/R/scale-binned.R b/R/scale-binned.R index 14647cce1f..f69efda8a0 100644 --- a/R/scale-binned.R +++ b/R/scale-binned.R @@ -27,7 +27,7 @@ scale_x_binned <- function(name = waiver(), n.breaks = 10, nice.breaks = TRUE, breaks = waiver(), labels = waiver(), limits = NULL, expand = waiver(), oob = squish, na.value = NA_real_, right = TRUE, show.limits = FALSE, transform = "identity", - trans = lifecycle::deprecated(), + trans = deprecated(), guide = waiver(), position = "bottom") { binned_scale( ggplot_global$x_aes, @@ -47,7 +47,7 @@ scale_y_binned <- function(name = waiver(), n.breaks = 10, nice.breaks = TRUE, breaks = waiver(), labels = waiver(), limits = NULL, expand = waiver(), oob = squish, na.value = NA_real_, right = TRUE, show.limits = FALSE, transform = "identity", - trans = lifecycle::deprecated(), + trans = deprecated(), guide = waiver(), position = "left") { binned_scale( ggplot_global$y_aes, diff --git a/R/scale-continuous.R b/R/scale-continuous.R index da01d8f323..19b3e9cb44 100644 --- a/R/scale-continuous.R +++ b/R/scale-continuous.R @@ -82,7 +82,7 @@ scale_x_continuous <- function(name = waiver(), breaks = waiver(), labels = waiver(), limits = NULL, expand = waiver(), oob = censor, na.value = NA_real_, transform = "identity", - trans = lifecycle::deprecated(), + trans = deprecated(), guide = waiver(), position = "bottom", sec.axis = waiver()) { call <- caller_call() @@ -109,7 +109,7 @@ scale_y_continuous <- function(name = waiver(), breaks = waiver(), labels = waiver(), limits = NULL, expand = waiver(), oob = censor, na.value = NA_real_, transform = "identity", - trans = lifecycle::deprecated(), + trans = deprecated(), guide = waiver(), position = "left", sec.axis = waiver()) { call <- caller_call() diff --git a/R/scale-date.R b/R/scale-date.R index 3b77e1ecfd..fd4a76ccf0 100644 --- a/R/scale-date.R +++ b/R/scale-date.R @@ -288,7 +288,7 @@ scale_y_time <- function(name = waiver(), #' #' @export #' @keywords internal -datetime_scale <- function(aesthetics, transform, trans = lifecycle::deprecated(), +datetime_scale <- function(aesthetics, transform, trans = deprecated(), palette, breaks = pretty_breaks(), minor_breaks = waiver(), labels = waiver(), date_breaks = waiver(), date_labels = waiver(), diff --git a/R/scale-linewidth.R b/R/scale-linewidth.R index 4fb086cd41..993e11f5e1 100644 --- a/R/scale-linewidth.R +++ b/R/scale-linewidth.R @@ -30,7 +30,7 @@ NULL scale_linewidth_continuous <- function(name = waiver(), breaks = waiver(), labels = waiver(), limits = NULL, range = c(1, 6), transform = "identity", - trans = lifecycle::deprecated(), + trans = deprecated(), guide = "legend") { continuous_scale("linewidth", palette = pal_rescale(range), name = name, breaks = breaks, labels = labels, limits = limits, @@ -46,7 +46,7 @@ scale_linewidth <- scale_linewidth_continuous scale_linewidth_binned <- function(name = waiver(), breaks = waiver(), labels = waiver(), limits = NULL, range = c(1, 6), n.breaks = NULL, nice.breaks = TRUE, transform = "identity", - trans = lifecycle::deprecated(), guide = "bins") { + trans = deprecated(), guide = "bins") { binned_scale("linewidth", palette = pal_rescale(range), name = name, breaks = breaks, labels = labels, limits = limits, transform = transform, trans = trans, n.breaks = n.breaks, diff --git a/R/scale-size.R b/R/scale-size.R index dad388ee9a..c1928fcf18 100644 --- a/R/scale-size.R +++ b/R/scale-size.R @@ -52,7 +52,7 @@ NULL scale_size_continuous <- function(name = waiver(), breaks = waiver(), labels = waiver(), limits = NULL, range = c(1, 6), transform = "identity", - trans = lifecycle::deprecated(), + trans = deprecated(), guide = "legend") { continuous_scale("size", palette = pal_area(range), name = name, breaks = breaks, labels = labels, limits = limits, @@ -67,7 +67,7 @@ scale_size <- scale_size_continuous #' @export scale_radius <- function(name = waiver(), breaks = waiver(), labels = waiver(), limits = NULL, range = c(1, 6), - transform = "identity", trans = lifecycle::deprecated(), + transform = "identity", trans = deprecated(), guide = "legend") { continuous_scale("size", palette = pal_rescale(range), name = name, breaks = breaks, labels = labels, limits = limits, transform = transform, @@ -79,7 +79,7 @@ scale_radius <- function(name = waiver(), breaks = waiver(), labels = waiver(), scale_size_binned <- function(name = waiver(), breaks = waiver(), labels = waiver(), limits = NULL, range = c(1, 6), n.breaks = NULL, nice.breaks = TRUE, transform = "identity", - trans = lifecycle::deprecated(), guide = "bins") { + trans = deprecated(), guide = "bins") { binned_scale("size", palette = pal_area(range), name = name, breaks = breaks, labels = labels, limits = limits, transform = transform, trans = trans, n.breaks = n.breaks, diff --git a/man/binned_scale.Rd b/man/binned_scale.Rd index 591429cbfb..b03ae8201b 100644 --- a/man/binned_scale.Rd +++ b/man/binned_scale.Rd @@ -20,7 +20,7 @@ binned_scale( nice.breaks = TRUE, right = TRUE, transform = "identity", - trans = lifecycle::deprecated(), + trans = deprecated(), show.limits = FALSE, guide = "bins", position = "left", diff --git a/man/continuous_scale.Rd b/man/continuous_scale.Rd index 8e9dde8f6d..a1ddd92780 100644 --- a/man/continuous_scale.Rd +++ b/man/continuous_scale.Rd @@ -19,7 +19,7 @@ continuous_scale( expand = waiver(), na.value = NA_real_, transform = "identity", - trans = lifecycle::deprecated(), + trans = deprecated(), guide = "legend", position = "left", call = caller_call(), diff --git a/man/datetime_scale.Rd b/man/datetime_scale.Rd index b2a6330489..d0a1afeec2 100644 --- a/man/datetime_scale.Rd +++ b/man/datetime_scale.Rd @@ -7,7 +7,7 @@ datetime_scale( aesthetics, transform, - trans = lifecycle::deprecated(), + trans = deprecated(), palette, breaks = pretty_breaks(), minor_breaks = waiver(), diff --git a/man/scale_binned.Rd b/man/scale_binned.Rd index ad71b75fc1..1d9bf4bc58 100644 --- a/man/scale_binned.Rd +++ b/man/scale_binned.Rd @@ -18,7 +18,7 @@ scale_x_binned( right = TRUE, show.limits = FALSE, transform = "identity", - trans = lifecycle::deprecated(), + trans = deprecated(), guide = waiver(), position = "bottom" ) @@ -36,7 +36,7 @@ scale_y_binned( right = TRUE, show.limits = FALSE, transform = "identity", - trans = lifecycle::deprecated(), + trans = deprecated(), guide = waiver(), position = "left" ) diff --git a/man/scale_continuous.Rd b/man/scale_continuous.Rd index 11221addac..ea630a7882 100644 --- a/man/scale_continuous.Rd +++ b/man/scale_continuous.Rd @@ -22,7 +22,7 @@ scale_x_continuous( oob = censor, na.value = NA_real_, transform = "identity", - trans = lifecycle::deprecated(), + trans = deprecated(), guide = waiver(), position = "bottom", sec.axis = waiver() @@ -39,7 +39,7 @@ scale_y_continuous( oob = censor, na.value = NA_real_, transform = "identity", - trans = lifecycle::deprecated(), + trans = deprecated(), guide = waiver(), position = "left", sec.axis = waiver() diff --git a/man/scale_linewidth.Rd b/man/scale_linewidth.Rd index 51c2d1d189..68cd760927 100644 --- a/man/scale_linewidth.Rd +++ b/man/scale_linewidth.Rd @@ -17,7 +17,7 @@ scale_linewidth( limits = NULL, range = c(1, 6), transform = "identity", - trans = lifecycle::deprecated(), + trans = deprecated(), guide = "legend" ) @@ -30,7 +30,7 @@ scale_linewidth_binned( n.breaks = NULL, nice.breaks = TRUE, transform = "identity", - trans = lifecycle::deprecated(), + trans = deprecated(), guide = "bins" ) } diff --git a/man/scale_size.Rd b/man/scale_size.Rd index 6be10e6f3e..72c5f3f2ab 100644 --- a/man/scale_size.Rd +++ b/man/scale_size.Rd @@ -20,7 +20,7 @@ scale_size( limits = NULL, range = c(1, 6), transform = "identity", - trans = lifecycle::deprecated(), + trans = deprecated(), guide = "legend" ) @@ -31,7 +31,7 @@ scale_radius( limits = NULL, range = c(1, 6), transform = "identity", - trans = lifecycle::deprecated(), + trans = deprecated(), guide = "legend" ) @@ -44,7 +44,7 @@ scale_size_binned( n.breaks = NULL, nice.breaks = TRUE, transform = "identity", - trans = lifecycle::deprecated(), + trans = deprecated(), guide = "bins" ) diff --git a/man/sec_axis.Rd b/man/sec_axis.Rd index 10d9d3d598..bca906a079 100644 --- a/man/sec_axis.Rd +++ b/man/sec_axis.Rd @@ -12,12 +12,12 @@ sec_axis( breaks = waiver(), labels = waiver(), guide = waiver(), - trans = lifecycle::deprecated() + trans = deprecated() ) dup_axis( transform = ~., - trans = lifecycle::deprecated(), + trans = deprecated(), name = derive(), breaks = derive(), labels = derive(), From d34cfdee773e33c7deb0d49c3a1d51c633d9aed1 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Wed, 6 Dec 2023 09:50:55 +0100 Subject: [PATCH 10/18] rename trans field --- R/axis-secondary.R | 16 +++--- R/coord-transform.R | 6 +- R/guide-axis-logticks.R | 10 ++-- R/scale-.R | 76 ++++++++++++++------------ R/scale-date.R | 2 +- R/scale-expansion.R | 5 +- R/scales-.R | 6 +- R/stat-function.R | 4 +- tests/testthat/test-plot-summary-api.R | 12 ++-- tests/testthat/test-scale-binned.R | 4 +- tests/testthat/test-scales.R | 2 +- vignettes/extending-ggplot2.Rmd | 2 +- 12 files changed, 77 insertions(+), 68 deletions(-) diff --git a/R/axis-secondary.R b/R/axis-secondary.R index 8433f6a5e8..8bbe2abfc8 100644 --- a/R/axis-secondary.R +++ b/R/axis-secondary.R @@ -178,7 +178,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, } if (is.derived(self$name) && !is.waive(scale$name)) self$name <- scale$name if (is.derived(self$breaks)) self$breaks <- scale$breaks - if (is.waive(self$breaks)) self$breaks <- scale$trans$breaks + if (is.waive(self$breaks)) self$breaks <- scale$transformer$breaks if (is.derived(self$labels)) self$labels <- scale$labels if (is.derived(self$guide)) self$guide <- scale$guide }, @@ -195,8 +195,9 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, return() } + transformer <- scale$transformer along_range <- seq(range[1], range[2], length.out = self$detail) - old_range <- scale$trans$inverse(along_range) + old_range <- transformer$inverse(along_range) # Create mapping between primary and secondary range full_range <- self$transform_range(old_range) @@ -213,8 +214,9 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, self$mono_test(scale) # Get scale's original range before transformation + transformer <- scale$transformer along_range <- seq(range[1], range[2], length.out = self$detail) - old_range <- scale$trans$inverse(along_range) + old_range <- transformer$inverse(along_range) # Create mapping between primary and secondary range full_range <- self$transform_range(old_range) @@ -234,8 +236,8 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, # patch for date and datetime scales just to maintain functionality # works only for linear secondary transforms that respect the time or date transform - if (scale$trans$name %in% c("date", "time")) { - temp_scale <- self$create_scale(new_range, trans = scale$trans) + if (transformer$name %in% c("date", "time")) { + temp_scale <- self$create_scale(new_range, trans = transformer) range_info <- temp_scale$break_info() old_val_trans <- rescale(range_info$major, from = c(0, 1), to = range) old_val_minor_trans <- rescale(range_info$minor, from = c(0, 1), to = range) @@ -246,7 +248,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, # Map the break values back to their correct position on the primary scale if (!is.null(range_info$major_source)) { old_val <- stats::approx(full_range, old_range, range_info$major_source)$y - old_val_trans <- scale$trans$transform(old_val) + old_val_trans <- transformer$transform(old_val) # rescale values from 0 to 1 range_info$major[] <- round( @@ -262,7 +264,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, if (!is.null(range_info$minor_source)) { old_val_minor <- stats::approx(full_range, old_range, range_info$minor_source)$y - old_val_minor_trans <- scale$trans$transform(old_val_minor) + old_val_minor_trans <- transformer$transform(old_val_minor) range_info$minor[] <- round( rescale( diff --git a/R/coord-transform.R b/R/coord-transform.R index 9fde8bb98e..50c7375f33 100644 --- a/R/coord-transform.R +++ b/R/coord-transform.R @@ -190,8 +190,8 @@ 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 %||% transform_identity() - coord_limits <- coord_limits %||% scale_trans$inverse(c(NA, NA)) + transformer <- scale$transformer %||% transform_identity() + coord_limits <- coord_limits %||% transformer$inverse(c(NA, NA)) scale_limits <- scale$get_limits() if (scale$is_discrete()) { @@ -204,7 +204,7 @@ view_scales_from_scale_with_coord_trans <- function(scale, coord_limits, trans, ) } else { # transform user-specified limits to scale transformed space - coord_limits <- scale$trans$transform(coord_limits) + coord_limits <- transformer$transform(coord_limits) continuous_ranges <- expand_limits_continuous_trans( scale_limits, expansion, diff --git a/R/guide-axis-logticks.R b/R/guide-axis-logticks.R index 15f35c30bf..7ad8123a60 100644 --- a/R/guide-axis-logticks.R +++ b/R/guide-axis-logticks.R @@ -147,20 +147,20 @@ GuideAxisLogticks <- ggproto( # Reconstruct a transformation if user has prescaled data if (!is.null(params$prescale_base)) { - trans_name <- scale$scale$trans$name + trans_name <- scale$scale$transformer$name if (trans_name != "identity") { cli::cli_warn(paste0( "The {.arg prescale_base} argument will override the scale's ", "{.field {trans_name}} transformation in log-tick positioning." )) } - trans <- transform_log(base = params$prescale_base) + transformer <- transform_log(base = params$prescale_base) } else { - trans <- scale$scale$trans + transformer <- scale$scale$transformer } # Reconstruct original range - limits <- trans$inverse(scale$get_limits()) + limits <- transformer$inverse(scale$get_limits()) has_negatives <- any(limits <= 0) if (!has_negatives) { @@ -188,7 +188,7 @@ GuideAxisLogticks <- ggproto( } # Set ticks back into transformed space - ticks <- trans$transform(c(tens, fives, ones)) + ticks <- transformer$transform(c(tens, fives, ones)) nticks <- c(length(tens), length(fives), length(ones)) logkey <- data_frame0( diff --git a/R/scale-.R b/R/scale-.R index 9fadc8a033..7f3997f8ea 100644 --- a/R/scale-.R +++ b/R/scale-.R @@ -141,7 +141,7 @@ continuous_scale <- function(aesthetics, scale_name = deprecated(), palette, nam range = ContinuousRange$new(), limits = limits, - trans = transform, + transformer = transform, na.value = na.value, expand = expand, rescaler = rescaler, @@ -311,7 +311,7 @@ binned_scale <- function(aesthetics, scale_name = deprecated(), palette, name = range = ContinuousRange$new(), limits = limits, - trans = transform, + transformer = transform, na.value = na.value, expand = expand, rescaler = rescaler, @@ -354,7 +354,7 @@ binned_scale <- function(aesthetics, scale_name = deprecated(), palette, name = #' - `clone()` Returns a copy of the scale that can be trained #' independently without affecting the original scale. #' -#' - `transform()` Transforms a vector of values using `self$trans`. +#' - `transform()` Transforms a vector of values using `self$transformer`. #' This occurs before the `Stat` is calculated. #' #' - `train()` Update the `self$range` of observed (transformed) data values with @@ -384,7 +384,7 @@ binned_scale <- function(aesthetics, scale_name = deprecated(), palette, name = #' (`self$range`). #' #' - `get_breaks()` Calculates the final scale breaks in transformed data space -#' based on on the combination of `self$breaks`, `self$trans$breaks()` (for +#' based on on the combination of `self$breaks`, `self$transformer$breaks()` (for #' continuous scales), and `limits`. Breaks outside of `limits` are assigned #' a value of `NA` (continuous scales) or dropped (discrete scales). #' @@ -393,7 +393,7 @@ binned_scale <- function(aesthetics, scale_name = deprecated(), palette, name = #' #' - `get_breaks_minor()` For continuous scales, calculates the final scale minor breaks #' in transformed data space based on the rescaled `breaks`, the value of `self$minor_breaks`, -#' and the value of `self$trans$minor_breaks()`. Discrete scales always return `NULL`. +#' and the value of `self$transformer$minor_breaks()`. Discrete scales always return `NULL`. #' #' - `make_title()` Hook to modify the title that is calculated during guide construction #' (for non-position scales) or when the `Layout` calculates the x and y labels @@ -595,8 +595,8 @@ check_breaks_labels <- function(breaks, labels, call = NULL) { } default_transform <- function(self, x) { - new_x <- self$trans$transform(x) - check_transformation(x, new_x, self$trans$name, self$call) + new_x <- self$transformer$transform(x) + check_transformation(x, new_x, self$transformer$name, self$call) new_x } @@ -616,7 +616,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, oob = censor, minor_breaks = waiver(), n.breaks = NULL, - trans = transform_identity(), + transformer = transform_identity(), is_discrete = function() FALSE, @@ -665,8 +665,9 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, if (is.null(self$limits)) { self$range$range } else if (is.function(self$limits)) { + transformer <- self$transformer # if limits is a function, it expects to work in data space - self$trans$transform(self$limits(self$trans$inverse(self$range$range))) + transformer$transform(self$limits(transformer$inverse(self$range$range))) } else { # NA limits for a continuous scale mean replace with the min/max of data ifelse(is.na(self$limits), self$range$range, self$limits) @@ -681,9 +682,9 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, if (self$is_empty()) { return(numeric()) } - + transformer <- self$transformer # Ensure limits don't exceed domain (#980) - domain <- suppressWarnings(self$trans$transform(self$trans$domain)) + domain <- suppressWarnings(transformer$transform(transformer$domain)) domain <- sort(domain) # To avoid NaN causing issues. NaN are dropped by the sort() if (length(domain) == 2 && !zero_range(domain)) { @@ -691,7 +692,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, } # Limits in transformed space need to be converted back to data space - limits <- self$trans$inverse(limits) + limits <- transformer$inverse(limits) if (is.null(self$breaks)) { return(NULL) @@ -706,19 +707,19 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, # Compute `zero_range()` in transformed space in case `limits` in data space # don't support conversion to numeric (#5304) - if (zero_range(as.numeric(self$trans$transform(limits)))) { + if (zero_range(as.numeric(transformer$transform(limits)))) { breaks <- limits[1] } else if (is.waive(self$breaks)) { - if (!is.null(self$n.breaks) && trans_support_nbreaks(self$trans)) { - breaks <- self$trans$breaks(limits, self$n.breaks) + if (!is.null(self$n.breaks) && trans_support_nbreaks(transformer)) { + breaks <- transformer$breaks(limits, self$n.breaks) } else { if (!is.null(self$n.breaks)) { cli::cli_warn( - "Ignoring {.arg n.breaks}. Use a {.cls trans} object that supports setting number of breaks.", + "Ignoring {.arg n.breaks}. Use a {.cls transform} object that supports setting number of breaks.", call = self$call ) } - breaks <- self$trans$breaks(limits) + breaks <- transformer$breaks(limits) } } else if (is.function(self$breaks)) { breaks <- self$breaks(limits) @@ -727,9 +728,9 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, } # Breaks in data space need to be converted back to transformed space - breaks <- self$trans$transform(breaks) + breaks <- transformer$transform(breaks) # Any breaks outside the dimensions are flagged as missing - breaks <- censor(breaks, self$trans$transform(limits), only.finite = FALSE) + breaks <- censor(breaks, transformer$transform(limits), only.finite = FALSE) breaks }, @@ -750,18 +751,19 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, ) } + transformer <- self$transformer if (is.waive(self$minor_breaks)) { if (is.null(b)) { breaks <- NULL } else { - breaks <- self$trans$minor_breaks(b, limits, n) + breaks <- transformer$minor_breaks(b, limits, n) } } else if (is.function(self$minor_breaks)) { # Find breaks in data space, and convert to numeric - breaks <- self$minor_breaks(self$trans$inverse(limits)) - breaks <- self$trans$transform(breaks) + breaks <- self$minor_breaks(transformer$inverse(limits)) + breaks <- transformer$transform(breaks) } else { - breaks <- self$trans$transform(self$minor_breaks) + breaks <- transformer$transform(self$minor_breaks) } # Any minor breaks outside the dimensions need to be thrown away @@ -773,7 +775,8 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, return(NULL) } - breaks <- self$trans$inverse(breaks) + transformer <- self$transformer + breaks <- transformer$inverse(breaks) if (is.null(self$labels)) { return(NULL) @@ -787,7 +790,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, } if (is.waive(self$labels)) { - labels <- self$trans$format(breaks) + labels <- transformer$format(breaks) } else if (is.function(self$labels)) { labels <- self$labels(breaks) } else { @@ -1138,7 +1141,9 @@ ScaleBinned <- ggproto("ScaleBinned", Scale, get_breaks = function(self, limits = self$get_limits()) { if (self$is_empty()) return(numeric()) - limits <- self$trans$inverse(limits) + transformer <- self$transformer + + limits <- transformer$inverse(limits) is_rev <- limits[2] < limits[1] limits <- sort(limits) @@ -1151,8 +1156,8 @@ ScaleBinned <- ggproto("ScaleBinned", Scale, ) } else if (is.waive(self$breaks)) { if (self$nice.breaks) { - if (!is.null(self$n.breaks) && trans_support_nbreaks(self$trans)) { - breaks <- self$trans$breaks(limits, n = self$n.breaks) + if (!is.null(self$n.breaks) && trans_support_nbreaks(transformer)) { + breaks <- transformer$breaks(limits, n = self$n.breaks) } else { if (!is.null(self$n.breaks)) { cli::cli_warn( @@ -1160,7 +1165,7 @@ ScaleBinned <- ggproto("ScaleBinned", Scale, call = self$call ) } - breaks <- self$trans$breaks(limits) + breaks <- transformer$breaks(limits) } } else { n.breaks <- self$n.breaks %||% 5 # same default as trans objects @@ -1191,12 +1196,12 @@ ScaleBinned <- ggproto("ScaleBinned", Scale, bin_size <- max(breaks[1] - limits[1], limits[2] - breaks[1]) new_limits <- c(breaks[1] - bin_size, breaks[1] + bin_size) } - new_limits_trans <- suppressWarnings(self$trans$transform(new_limits)) + new_limits_trans <- suppressWarnings(transformer$transform(new_limits)) limits[is.finite(new_limits_trans)] <- new_limits[is.finite(new_limits_trans)] if (is_rev) { - self$limits <- rev(self$trans$transform(limits)) + self$limits <- rev(transformer$transform(limits)) } else { - self$limits <- self$trans$transform(limits) + self$limits <- transformer$transform(limits) } } } else if (is.function(self$breaks)) { @@ -1221,7 +1226,7 @@ ScaleBinned <- ggproto("ScaleBinned", Scale, self$breaks <- breaks - self$trans$transform(breaks) + transformer$transform(breaks) }, get_breaks_minor = function(...) NULL, @@ -1229,7 +1234,8 @@ ScaleBinned <- ggproto("ScaleBinned", Scale, get_labels = function(self, breaks = self$get_breaks()) { if (is.null(breaks)) return(NULL) - breaks <- self$trans$inverse(breaks) + transformer <- self$transformer + breaks <- transformer$inverse(breaks) if (is.null(self$labels)) { return(NULL) @@ -1239,7 +1245,7 @@ ScaleBinned <- ggproto("ScaleBinned", Scale, call = self$call ) } else if (is.waive(self$labels)) { - labels <- self$trans$format(breaks) + labels <- transformer$format(breaks) } else if (is.function(self$labels)) { labels <- self$labels(breaks) } else { diff --git a/R/scale-date.R b/R/scale-date.R index fd4a76ccf0..3e559b1088 100644 --- a/R/scale-date.R +++ b/R/scale-date.R @@ -358,7 +358,7 @@ ScaleContinuousDatetime <- ggproto("ScaleContinuousDatetime", ScaleContinuous, tz <- attr(x, "tzone") if (is.null(self$timezone) && !is.null(tz)) { self$timezone <- tz - self$trans <- transform_time(self$timezone) + self$transformer <- transform_time(self$timezone) } ggproto_parent(ScaleContinuous, self)$transform(x) }, diff --git a/R/scale-expansion.R b/R/scale-expansion.R index 5518e9f012..f220bcfb40 100644 --- a/R/scale-expansion.R +++ b/R/scale-expansion.R @@ -145,8 +145,9 @@ expand_limits_scale <- function(scale, expand = expansion(0, 0), limits = waiver } else { # using the inverse transform to resolve the NA value is needed for date/datetime/time # scales, which refuse to transform objects of the incorrect type - coord_limits <- coord_limits %||% scale$trans$inverse(c(NA_real_, NA_real_)) - coord_limits_scale <- scale$trans$transform(coord_limits) + transformer <- scale$transformer + coord_limits <- coord_limits %||% transformer$inverse(c(NA_real_, NA_real_)) + coord_limits_scale <- transformer$transform(coord_limits) expand_limits_continuous(limits, expand, coord_limits_scale) } } diff --git a/R/scales-.R b/R/scales-.R index 2d8ffbfe06..095c1341b3 100644 --- a/R/scales-.R +++ b/R/scales-.R @@ -90,7 +90,7 @@ ScalesList <- ggproto("ScalesList", NULL, # to transform anything idx_skip <- vapply(self$scales, function(x) { has_default_transform(x) && - (is.null(x$trans) || identical(x$trans$transform, identity)) + (is.null(x$transformer) || identical(x$transformer$transform, identity)) }, logical(1L)) scales <- self$scales[!idx_skip] @@ -114,7 +114,7 @@ ScalesList <- ggproto("ScalesList", NULL, # to transform anything idx_skip <- vapply(self$scales, function(x) { has_default_transform(x) && - (is.null(x$trans) || identical(x$trans$transform, identity)) + (is.null(x$transformer) || identical(x$transformer$transform, identity)) }, logical(1)) scales <- self$scales[!idx_skip] @@ -129,7 +129,7 @@ ScalesList <- ggproto("ScalesList", NULL, if (length(aesthetics) == 0) { return() } - lapply(df[aesthetics], scale$trans$inverse) + lapply(df[aesthetics], scale$transformer$inverse) } ), recursive = FALSE) diff --git a/R/stat-function.R b/R/stat-function.R index 8585d3b843..7b11b71516 100644 --- a/R/stat-function.R +++ b/R/stat-function.R @@ -66,7 +66,7 @@ StatFunction <- ggproto("StatFunction", Stat, } else { # For continuous scales, need to back transform from transformed range # to original values - x_trans <- scales$x$trans$inverse(xseq) + x_trans <- scales$x$transformer$inverse(xseq) } } @@ -75,7 +75,7 @@ StatFunction <- ggproto("StatFunction", Stat, y_out <- inject(fun(x_trans, !!!args)) if (!is.null(scales$y) && !scales$y$is_discrete()) { # For continuous scales, need to apply transform - y_out <- scales$y$trans$transform(y_out) + y_out <- scales$y$transformer$transform(y_out) } data_frame0(x = xseq, y = y_out) diff --git a/tests/testthat/test-plot-summary-api.R b/tests/testthat/test-plot-summary-api.R index 9ce33632c6..e59f49c341 100644 --- a/tests/testthat/test-plot-summary-api.R +++ b/tests/testthat/test-plot-summary-api.R @@ -80,17 +80,17 @@ test_that("layout summary - reversed scales", { lr <- summarise_layout(ggplot_build(pr)) expect_equal(lr$xmin, -7.27) expect_equal(lr$xmax, -1.33) - expect_equal(lr$xscale[[1]]$trans$name, "reverse") - expect_equal(lr$xscale[[1]]$trans$transform(5), -5) + expect_equal(lr$xscale[[1]]$transformer$name, "reverse") + expect_equal(lr$xscale[[1]]$transformer$transform(5), -5) }) test_that("layout summary - log scales", { pl <- p + scale_x_log10() + scale_y_continuous(transform = "log2") ll <- summarise_layout(ggplot_build(pl)) - expect_equal(ll$xscale[[1]]$trans$name, "log-10") - expect_equal(ll$xscale[[1]]$trans$transform(100), 2) - expect_equal(ll$yscale[[1]]$trans$name, "log-2") - expect_equal(ll$yscale[[1]]$trans$transform(16), 4) + expect_equal(ll$xscale[[1]]$transformer$name, "log-10") + expect_equal(ll$xscale[[1]]$transformer$transform(100), 2) + expect_equal(ll$yscale[[1]]$transformer$name, "log-2") + expect_equal(ll$yscale[[1]]$transformer$transform(16), 4) }) test_that("coord summary - basic", { diff --git a/tests/testthat/test-scale-binned.R b/tests/testthat/test-scale-binned.R index 44d19995d9..700e7c045c 100644 --- a/tests/testthat/test-scale-binned.R +++ b/tests/testthat/test-scale-binned.R @@ -65,7 +65,7 @@ test_that('binned scales can calculate breaks on dates', { scale <- scale_x_binned(transform = "date") scale$train(scale$transform(data)) - breaks <- scale$trans$inverse(scale$get_breaks()) + breaks <- scale$transformer$inverse(scale$get_breaks()) expect_s3_class(breaks, "Date") expect_equal( @@ -83,7 +83,7 @@ test_that('binned scales can calculate breaks on date-times', { scale <- scale_x_binned(transform = "time") scale$train(scale$transform(data)) - breaks <- scale$trans$inverse(scale$get_breaks()) + breaks <- scale$transformer$inverse(scale$get_breaks()) expect_s3_class(breaks, "POSIXct") expect_equal( diff --git a/tests/testthat/test-scales.R b/tests/testthat/test-scales.R index e325e654b4..50111cc9ae 100644 --- a/tests/testthat/test-scales.R +++ b/tests/testthat/test-scales.R @@ -464,7 +464,7 @@ test_that("numeric scale transforms can produce breaks", { scale <- scale_x_continuous(transform = transform) scale$train(scale$transform(limits)) view <- view_scale_primary(scale) - scale$trans$inverse(view$get_breaks()) + scale$transformer$inverse(view$get_breaks()) } expect_equal(test_breaks("asn", limits = c(0, 1)), diff --git a/vignettes/extending-ggplot2.Rmd b/vignettes/extending-ggplot2.Rmd index 0b856abe72..13c80502ac 100644 --- a/vignettes/extending-ggplot2.Rmd +++ b/vignettes/extending-ggplot2.Rmd @@ -864,7 +864,7 @@ FacetTrans <- ggproto("FacetTrans", Facet, if (!is.null(y_scale)) { y_scale_orig <- y_scale$clone() y_scale_new <- y_scale$clone() - y_scale_new$trans <- params$trans + y_scale_new$transformer <- params$trans # Make sure that oob values are kept y_scale_new$oob <- function(x, ...) x scales$y <- list(y_scale_orig, y_scale_new) From 398158a526a5676867149f4326c4287decc805b6 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Wed, 6 Dec 2023 13:54:07 +0100 Subject: [PATCH 11/18] fix sec axis bug --- R/axis-secondary.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/axis-secondary.R b/R/axis-secondary.R index 8bbe2abfc8..b0dc30bd8a 100644 --- a/R/axis-secondary.R +++ b/R/axis-secondary.R @@ -298,7 +298,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, labels = self$labels, limits = range, expand = c(0, 0), - trans = trans + transformer = trans ) scale$train(range) scale From 2e31c0e8a3cf1028b218bf8cced36595a9d115bc Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 11 Dec 2023 15:40:05 +0100 Subject: [PATCH 12/18] fallback mechanism --- R/axis-secondary.R | 4 ++-- R/coord-transform.R | 2 +- R/guide-axis-logticks.R | 2 +- R/scale-.R | 12 ++++++------ R/scale-expansion.R | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/R/axis-secondary.R b/R/axis-secondary.R index b0dc30bd8a..4275557b8c 100644 --- a/R/axis-secondary.R +++ b/R/axis-secondary.R @@ -195,7 +195,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, return() } - transformer <- scale$transformer + transformer <- scale$transformer %||% scale$trans along_range <- seq(range[1], range[2], length.out = self$detail) old_range <- transformer$inverse(along_range) @@ -214,7 +214,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, self$mono_test(scale) # Get scale's original range before transformation - transformer <- scale$transformer + transformer <- scale$transformer %||% scale$trans along_range <- seq(range[1], range[2], length.out = self$detail) old_range <- transformer$inverse(along_range) diff --git a/R/coord-transform.R b/R/coord-transform.R index 50c7375f33..7fcb307b2d 100644 --- a/R/coord-transform.R +++ b/R/coord-transform.R @@ -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) - transformer <- scale$transformer %||% transform_identity() + transformer <- scale$transformer %||% scale$trans %||% transform_identity() coord_limits <- coord_limits %||% transformer$inverse(c(NA, NA)) scale_limits <- scale$get_limits() diff --git a/R/guide-axis-logticks.R b/R/guide-axis-logticks.R index 7ad8123a60..17566ece97 100644 --- a/R/guide-axis-logticks.R +++ b/R/guide-axis-logticks.R @@ -156,7 +156,7 @@ GuideAxisLogticks <- ggproto( } transformer <- transform_log(base = params$prescale_base) } else { - transformer <- scale$scale$transformer + transformer <- scale$scale$transformer %||% scale$scale$trans } # Reconstruct original range diff --git a/R/scale-.R b/R/scale-.R index b709537c1a..5c69bfe41b 100644 --- a/R/scale-.R +++ b/R/scale-.R @@ -667,7 +667,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, if (is.null(self$limits)) { self$range$range } else if (is.function(self$limits)) { - transformer <- self$transformer + transformer <- self$transformer %||% self$trans # if limits is a function, it expects to work in data space transformer$transform(self$limits(transformer$inverse(self$range$range))) } else { @@ -684,7 +684,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, if (self$is_empty()) { return(numeric()) } - transformer <- self$transformer + transformer <- self$transformer %||% self$trans # Ensure limits don't exceed domain (#980) domain <- suppressWarnings(transformer$transform(transformer$domain)) domain <- sort(domain) @@ -752,7 +752,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, # some transforms assume finite major breaks b <- b[is.finite(b)] - transformer <- self$transformer + transformer <- self$transformer %||% self$trans if (is.waive(self$minor_breaks)) { if (is.null(b)) { breaks <- NULL @@ -786,7 +786,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, return(NULL) } - transformer <- self$transformer + transformer <- self$transformer %||% self$trans breaks <- transformer$inverse(breaks) if (is.null(self$labels)) { @@ -1154,7 +1154,7 @@ ScaleBinned <- ggproto("ScaleBinned", Scale, get_breaks = function(self, limits = self$get_limits()) { if (self$is_empty()) return(numeric()) - transformer <- self$transformer + transformer <- self$transformer %||% self$trans limits <- transformer$inverse(limits) is_rev <- limits[2] < limits[1] @@ -1247,7 +1247,7 @@ ScaleBinned <- ggproto("ScaleBinned", Scale, get_labels = function(self, breaks = self$get_breaks()) { if (is.null(breaks)) return(NULL) - transformer <- self$transformer + transformer <- self$transformer %||% self$trans breaks <- transformer$inverse(breaks) if (is.null(self$labels)) { diff --git a/R/scale-expansion.R b/R/scale-expansion.R index f220bcfb40..ef5ff61a8d 100644 --- a/R/scale-expansion.R +++ b/R/scale-expansion.R @@ -145,7 +145,7 @@ expand_limits_scale <- function(scale, expand = expansion(0, 0), limits = waiver } else { # using the inverse transform to resolve the NA value is needed for date/datetime/time # scales, which refuse to transform objects of the incorrect type - transformer <- scale$transformer + transformer <- scale$transformer %||% scale$trans coord_limits <- coord_limits %||% transformer$inverse(c(NA_real_, NA_real_)) coord_limits_scale <- transformer$transform(coord_limits) expand_limits_continuous(limits, expand, coord_limits_scale) From 7940355fc94e0e20287d6e866da72c50c20e3872 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 11 Dec 2023 15:40:30 +0100 Subject: [PATCH 13/18] deprecation message --- R/scale-.R | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/R/scale-.R b/R/scale-.R index 5c69bfe41b..20c4e6ccd4 100644 --- a/R/scale-.R +++ b/R/scale-.R @@ -597,7 +597,11 @@ check_breaks_labels <- function(breaks, labels, call = NULL) { } default_transform <- function(self, x) { - new_x <- self$transformer$transform(x) + if (!is.null(self$trans)) { + deprecate_soft0("3.5.0", I("Scale$trans"), I("Scale$transformer")) + } + transformer <- self$transformer %||% self$trans + new_x <- transformer$transform(x) check_transformation(x, new_x, self$transformer$name, self$call) new_x } From 3d55f85236e1e2bf6ccd9980a3d4d11da7df2112 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 11 Dec 2023 15:48:53 +0100 Subject: [PATCH 14/18] transformer -> transformation --- R/axis-secondary.R | 20 +++---- R/coord-transform.R | 6 +- R/guide-axis-logticks.R | 10 ++-- R/scale-.R | 82 +++++++++++++------------- R/scale-date.R | 2 +- R/scale-expansion.R | 6 +- R/scales-.R | 6 +- R/stat-function.R | 4 +- tests/testthat/test-plot-summary-api.R | 12 ++-- tests/testthat/test-scale-binned.R | 4 +- tests/testthat/test-scales.R | 2 +- vignettes/extending-ggplot2.Rmd | 2 +- 12 files changed, 78 insertions(+), 78 deletions(-) diff --git a/R/axis-secondary.R b/R/axis-secondary.R index 4275557b8c..6084f4b7de 100644 --- a/R/axis-secondary.R +++ b/R/axis-secondary.R @@ -178,7 +178,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, } if (is.derived(self$name) && !is.waive(scale$name)) self$name <- scale$name if (is.derived(self$breaks)) self$breaks <- scale$breaks - if (is.waive(self$breaks)) self$breaks <- scale$transformer$breaks + if (is.waive(self$breaks)) self$breaks <- scale$transformation$breaks if (is.derived(self$labels)) self$labels <- scale$labels if (is.derived(self$guide)) self$guide <- scale$guide }, @@ -195,9 +195,9 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, return() } - transformer <- scale$transformer %||% scale$trans + transformation <- scale$transformation %||% scale$trans along_range <- seq(range[1], range[2], length.out = self$detail) - old_range <- transformer$inverse(along_range) + old_range <- transformation$inverse(along_range) # Create mapping between primary and secondary range full_range <- self$transform_range(old_range) @@ -214,9 +214,9 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, self$mono_test(scale) # Get scale's original range before transformation - transformer <- scale$transformer %||% scale$trans + transformation <- scale$transformation %||% scale$trans along_range <- seq(range[1], range[2], length.out = self$detail) - old_range <- transformer$inverse(along_range) + old_range <- transformation$inverse(along_range) # Create mapping between primary and secondary range full_range <- self$transform_range(old_range) @@ -236,8 +236,8 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, # patch for date and datetime scales just to maintain functionality # works only for linear secondary transforms that respect the time or date transform - if (transformer$name %in% c("date", "time")) { - temp_scale <- self$create_scale(new_range, trans = transformer) + if (transformation$name %in% c("date", "time")) { + temp_scale <- self$create_scale(new_range, trans = transformation) range_info <- temp_scale$break_info() old_val_trans <- rescale(range_info$major, from = c(0, 1), to = range) old_val_minor_trans <- rescale(range_info$minor, from = c(0, 1), to = range) @@ -248,7 +248,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, # Map the break values back to their correct position on the primary scale if (!is.null(range_info$major_source)) { old_val <- stats::approx(full_range, old_range, range_info$major_source)$y - old_val_trans <- transformer$transform(old_val) + old_val_trans <- transformation$transform(old_val) # rescale values from 0 to 1 range_info$major[] <- round( @@ -264,7 +264,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, if (!is.null(range_info$minor_source)) { old_val_minor <- stats::approx(full_range, old_range, range_info$minor_source)$y - old_val_minor_trans <- transformer$transform(old_val_minor) + old_val_minor_trans <- transformation$transform(old_val_minor) range_info$minor[] <- round( rescale( @@ -298,7 +298,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, labels = self$labels, limits = range, expand = c(0, 0), - transformer = trans + transformation = trans ) scale$train(range) scale diff --git a/R/coord-transform.R b/R/coord-transform.R index 7fcb307b2d..9df4e91761 100644 --- a/R/coord-transform.R +++ b/R/coord-transform.R @@ -190,8 +190,8 @@ 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) - transformer <- scale$transformer %||% scale$trans %||% transform_identity() - coord_limits <- coord_limits %||% transformer$inverse(c(NA, NA)) + transformation <- scale$transformation %||% scale$trans %||% transform_identity() + coord_limits <- coord_limits %||% transformation$inverse(c(NA, NA)) scale_limits <- scale$get_limits() if (scale$is_discrete()) { @@ -204,7 +204,7 @@ view_scales_from_scale_with_coord_trans <- function(scale, coord_limits, trans, ) } else { # transform user-specified limits to scale transformed space - coord_limits <- transformer$transform(coord_limits) + coord_limits <- transformation$transform(coord_limits) continuous_ranges <- expand_limits_continuous_trans( scale_limits, expansion, diff --git a/R/guide-axis-logticks.R b/R/guide-axis-logticks.R index 17566ece97..5809fc9cd7 100644 --- a/R/guide-axis-logticks.R +++ b/R/guide-axis-logticks.R @@ -147,20 +147,20 @@ GuideAxisLogticks <- ggproto( # Reconstruct a transformation if user has prescaled data if (!is.null(params$prescale_base)) { - trans_name <- scale$scale$transformer$name + trans_name <- scale$scale$transformation$name if (trans_name != "identity") { cli::cli_warn(paste0( "The {.arg prescale_base} argument will override the scale's ", "{.field {trans_name}} transformation in log-tick positioning." )) } - transformer <- transform_log(base = params$prescale_base) + transformation <- transform_log(base = params$prescale_base) } else { - transformer <- scale$scale$transformer %||% scale$scale$trans + transformation <- scale$scale$transformation %||% scale$scale$trans } # Reconstruct original range - limits <- transformer$inverse(scale$get_limits()) + limits <- transformation$inverse(scale$get_limits()) has_negatives <- any(limits <= 0) if (!has_negatives) { @@ -188,7 +188,7 @@ GuideAxisLogticks <- ggproto( } # Set ticks back into transformed space - ticks <- transformer$transform(c(tens, fives, ones)) + ticks <- transformation$transform(c(tens, fives, ones)) nticks <- c(length(tens), length(fives), length(ones)) logkey <- data_frame0( diff --git a/R/scale-.R b/R/scale-.R index 20c4e6ccd4..cd3a074040 100644 --- a/R/scale-.R +++ b/R/scale-.R @@ -143,7 +143,7 @@ continuous_scale <- function(aesthetics, scale_name = deprecated(), palette, nam range = ContinuousRange$new(), limits = limits, - transformer = transform, + transformation = transform, na.value = na.value, expand = expand, rescaler = rescaler, @@ -313,7 +313,7 @@ binned_scale <- function(aesthetics, scale_name = deprecated(), palette, name = range = ContinuousRange$new(), limits = limits, - transformer = transform, + transformation = transform, na.value = na.value, expand = expand, rescaler = rescaler, @@ -356,7 +356,7 @@ binned_scale <- function(aesthetics, scale_name = deprecated(), palette, name = #' - `clone()` Returns a copy of the scale that can be trained #' independently without affecting the original scale. #' -#' - `transform()` Transforms a vector of values using `self$transformer`. +#' - `transform()` Transforms a vector of values using `self$transformation`. #' This occurs before the `Stat` is calculated. #' #' - `train()` Update the `self$range` of observed (transformed) data values with @@ -386,7 +386,7 @@ binned_scale <- function(aesthetics, scale_name = deprecated(), palette, name = #' (`self$range`). #' #' - `get_breaks()` Calculates the final scale breaks in transformed data space -#' based on on the combination of `self$breaks`, `self$transformer$breaks()` (for +#' based on on the combination of `self$breaks`, `self$transformation$breaks()` (for #' continuous scales), and `limits`. Breaks outside of `limits` are assigned #' a value of `NA` (continuous scales) or dropped (discrete scales). #' @@ -395,7 +395,7 @@ binned_scale <- function(aesthetics, scale_name = deprecated(), palette, name = #' #' - `get_breaks_minor()` For continuous scales, calculates the final scale minor breaks #' in transformed data space based on the rescaled `breaks`, the value of `self$minor_breaks`, -#' and the value of `self$transformer$minor_breaks()`. Discrete scales always return `NULL`. +#' and the value of `self$transformation$minor_breaks()`. Discrete scales always return `NULL`. #' #' - `make_title()` Hook to modify the title that is calculated during guide construction #' (for non-position scales) or when the `Layout` calculates the x and y labels @@ -598,11 +598,11 @@ check_breaks_labels <- function(breaks, labels, call = NULL) { default_transform <- function(self, x) { if (!is.null(self$trans)) { - deprecate_soft0("3.5.0", I("Scale$trans"), I("Scale$transformer")) + deprecate_soft0("3.5.0", I("Scale$trans"), I("Scale$transformation")) } - transformer <- self$transformer %||% self$trans - new_x <- transformer$transform(x) - check_transformation(x, new_x, self$transformer$name, self$call) + transformation <- self$transformation %||% self$trans + new_x <- transformation$transform(x) + check_transformation(x, new_x, self$transformation$name, self$call) new_x } @@ -622,7 +622,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, oob = censor, minor_breaks = waiver(), n.breaks = NULL, - transformer = transform_identity(), + transformation = transform_identity(), is_discrete = function() FALSE, @@ -671,9 +671,9 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, if (is.null(self$limits)) { self$range$range } else if (is.function(self$limits)) { - transformer <- self$transformer %||% self$trans + transformation <- self$transformation %||% self$trans # if limits is a function, it expects to work in data space - transformer$transform(self$limits(transformer$inverse(self$range$range))) + transformation$transform(self$limits(transformation$inverse(self$range$range))) } else { # NA limits for a continuous scale mean replace with the min/max of data ifelse(is.na(self$limits), self$range$range, self$limits) @@ -688,9 +688,9 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, if (self$is_empty()) { return(numeric()) } - transformer <- self$transformer %||% self$trans + transformation <- self$transformation %||% self$trans # Ensure limits don't exceed domain (#980) - domain <- suppressWarnings(transformer$transform(transformer$domain)) + domain <- suppressWarnings(transformation$transform(transformation$domain)) domain <- sort(domain) # To avoid NaN causing issues. NaN are dropped by the sort() if (length(domain) == 2 && !zero_range(domain)) { @@ -698,7 +698,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, } # Limits in transformed space need to be converted back to data space - limits <- transformer$inverse(limits) + limits <- transformation$inverse(limits) if (is.null(self$breaks)) { return(NULL) @@ -713,11 +713,11 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, # Compute `zero_range()` in transformed space in case `limits` in data space # don't support conversion to numeric (#5304) - if (zero_range(as.numeric(transformer$transform(limits)))) { + if (zero_range(as.numeric(transformation$transform(limits)))) { breaks <- limits[1] } else if (is.waive(self$breaks)) { - if (!is.null(self$n.breaks) && trans_support_nbreaks(transformer)) { - breaks <- transformer$breaks(limits, self$n.breaks) + if (!is.null(self$n.breaks) && trans_support_nbreaks(transformation)) { + breaks <- transformation$breaks(limits, self$n.breaks) } else { if (!is.null(self$n.breaks)) { cli::cli_warn( @@ -725,7 +725,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, call = self$call ) } - breaks <- transformer$breaks(limits) + breaks <- transformation$breaks(limits) } } else if (is.function(self$breaks)) { breaks <- self$breaks(limits) @@ -734,7 +734,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, } # Breaks in data space need to be converted back to transformed space - transformer$transform(breaks) + transformation$transform(breaks) }, get_breaks_minor = function(self, n = 2, b = self$break_positions(), limits = self$get_limits()) { @@ -756,12 +756,12 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, # some transforms assume finite major breaks b <- b[is.finite(b)] - transformer <- self$transformer %||% self$trans + transformation <- self$transformation %||% self$trans if (is.waive(self$minor_breaks)) { if (is.null(b)) { breaks <- NULL } else { - breaks <- transformer$minor_breaks(b, limits, n) + breaks <- transformation$minor_breaks(b, limits, n) } } else if (is.function(self$minor_breaks)) { # Using `fetch_ggproto` here to avoid auto-wrapping the user-supplied @@ -771,14 +771,14 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, # Find breaks in data space if (length(arg_names) == 1L) { - breaks <- break_fun(transformer$inverse(limits)) + breaks <- break_fun(transformation$inverse(limits)) } else { - breaks <- break_fun(transformer$inverse(limits), transformer$inverse(b)) + breaks <- break_fun(transformation$inverse(limits), transformation$inverse(b)) } # Convert breaks to numeric - breaks <- transformer$transform(breaks) + breaks <- transformation$transform(breaks) } else { - breaks <- transformer$transform(self$minor_breaks) + breaks <- transformation$transform(self$minor_breaks) } # Any minor breaks outside the dimensions need to be thrown away @@ -790,8 +790,8 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, return(NULL) } - transformer <- self$transformer %||% self$trans - breaks <- transformer$inverse(breaks) + transformation <- self$transformation %||% self$trans + breaks <- transformation$inverse(breaks) if (is.null(self$labels)) { return(NULL) @@ -805,7 +805,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, } if (is.waive(self$labels)) { - labels <- transformer$format(breaks) + labels <- transformation$format(breaks) } else if (is.function(self$labels)) { labels <- self$labels(breaks) } else { @@ -1158,9 +1158,9 @@ ScaleBinned <- ggproto("ScaleBinned", Scale, get_breaks = function(self, limits = self$get_limits()) { if (self$is_empty()) return(numeric()) - transformer <- self$transformer %||% self$trans + transformation <- self$transformation %||% self$trans - limits <- transformer$inverse(limits) + limits <- transformation$inverse(limits) is_rev <- limits[2] < limits[1] limits <- sort(limits) @@ -1173,8 +1173,8 @@ ScaleBinned <- ggproto("ScaleBinned", Scale, ) } else if (is.waive(self$breaks)) { if (self$nice.breaks) { - if (!is.null(self$n.breaks) && trans_support_nbreaks(transformer)) { - breaks <- transformer$breaks(limits, n = self$n.breaks) + if (!is.null(self$n.breaks) && trans_support_nbreaks(transformation)) { + breaks <- transformation$breaks(limits, n = self$n.breaks) } else { if (!is.null(self$n.breaks)) { cli::cli_warn( @@ -1182,7 +1182,7 @@ ScaleBinned <- ggproto("ScaleBinned", Scale, call = self$call ) } - breaks <- transformer$breaks(limits) + breaks <- transformation$breaks(limits) } } else { n.breaks <- self$n.breaks %||% 5 # same default as trans objects @@ -1213,12 +1213,12 @@ ScaleBinned <- ggproto("ScaleBinned", Scale, bin_size <- max(breaks[1] - limits[1], limits[2] - breaks[1]) new_limits <- c(breaks[1] - bin_size, breaks[1] + bin_size) } - new_limits_trans <- suppressWarnings(transformer$transform(new_limits)) + new_limits_trans <- suppressWarnings(transformation$transform(new_limits)) limits[is.finite(new_limits_trans)] <- new_limits[is.finite(new_limits_trans)] if (is_rev) { - self$limits <- rev(transformer$transform(limits)) + self$limits <- rev(transformation$transform(limits)) } else { - self$limits <- transformer$transform(limits) + self$limits <- transformation$transform(limits) } } } else if (is.function(self$breaks)) { @@ -1243,7 +1243,7 @@ ScaleBinned <- ggproto("ScaleBinned", Scale, self$breaks <- breaks - transformer$transform(breaks) + transformation$transform(breaks) }, get_breaks_minor = function(...) NULL, @@ -1251,8 +1251,8 @@ ScaleBinned <- ggproto("ScaleBinned", Scale, get_labels = function(self, breaks = self$get_breaks()) { if (is.null(breaks)) return(NULL) - transformer <- self$transformer %||% self$trans - breaks <- transformer$inverse(breaks) + transformation <- self$transformation %||% self$trans + breaks <- transformation$inverse(breaks) if (is.null(self$labels)) { return(NULL) @@ -1262,7 +1262,7 @@ ScaleBinned <- ggproto("ScaleBinned", Scale, call = self$call ) } else if (is.waive(self$labels)) { - labels <- transformer$format(breaks) + labels <- transformation$format(breaks) } else if (is.function(self$labels)) { labels <- self$labels(breaks) } else { diff --git a/R/scale-date.R b/R/scale-date.R index 3e559b1088..f607b78847 100644 --- a/R/scale-date.R +++ b/R/scale-date.R @@ -358,7 +358,7 @@ ScaleContinuousDatetime <- ggproto("ScaleContinuousDatetime", ScaleContinuous, tz <- attr(x, "tzone") if (is.null(self$timezone) && !is.null(tz)) { self$timezone <- tz - self$transformer <- transform_time(self$timezone) + self$transformation <- transform_time(self$timezone) } ggproto_parent(ScaleContinuous, self)$transform(x) }, diff --git a/R/scale-expansion.R b/R/scale-expansion.R index ef5ff61a8d..58c5f48e5f 100644 --- a/R/scale-expansion.R +++ b/R/scale-expansion.R @@ -145,9 +145,9 @@ expand_limits_scale <- function(scale, expand = expansion(0, 0), limits = waiver } else { # using the inverse transform to resolve the NA value is needed for date/datetime/time # scales, which refuse to transform objects of the incorrect type - transformer <- scale$transformer %||% scale$trans - coord_limits <- coord_limits %||% transformer$inverse(c(NA_real_, NA_real_)) - coord_limits_scale <- transformer$transform(coord_limits) + transformation <- scale$transformation %||% scale$trans + coord_limits <- coord_limits %||% transformation$inverse(c(NA_real_, NA_real_)) + coord_limits_scale <- transformation$transform(coord_limits) expand_limits_continuous(limits, expand, coord_limits_scale) } } diff --git a/R/scales-.R b/R/scales-.R index 095c1341b3..5e1fd3208a 100644 --- a/R/scales-.R +++ b/R/scales-.R @@ -90,7 +90,7 @@ ScalesList <- ggproto("ScalesList", NULL, # to transform anything idx_skip <- vapply(self$scales, function(x) { has_default_transform(x) && - (is.null(x$transformer) || identical(x$transformer$transform, identity)) + (is.null(x$transformation) || identical(x$transformation$transform, identity)) }, logical(1L)) scales <- self$scales[!idx_skip] @@ -114,7 +114,7 @@ ScalesList <- ggproto("ScalesList", NULL, # to transform anything idx_skip <- vapply(self$scales, function(x) { has_default_transform(x) && - (is.null(x$transformer) || identical(x$transformer$transform, identity)) + (is.null(x$transformation) || identical(x$transformation$transform, identity)) }, logical(1)) scales <- self$scales[!idx_skip] @@ -129,7 +129,7 @@ ScalesList <- ggproto("ScalesList", NULL, if (length(aesthetics) == 0) { return() } - lapply(df[aesthetics], scale$transformer$inverse) + lapply(df[aesthetics], scale$transformation$inverse) } ), recursive = FALSE) diff --git a/R/stat-function.R b/R/stat-function.R index 7b11b71516..b43af4fb5a 100644 --- a/R/stat-function.R +++ b/R/stat-function.R @@ -66,7 +66,7 @@ StatFunction <- ggproto("StatFunction", Stat, } else { # For continuous scales, need to back transform from transformed range # to original values - x_trans <- scales$x$transformer$inverse(xseq) + x_trans <- scales$x$transformation$inverse(xseq) } } @@ -75,7 +75,7 @@ StatFunction <- ggproto("StatFunction", Stat, y_out <- inject(fun(x_trans, !!!args)) if (!is.null(scales$y) && !scales$y$is_discrete()) { # For continuous scales, need to apply transform - y_out <- scales$y$transformer$transform(y_out) + y_out <- scales$y$transformation$transform(y_out) } data_frame0(x = xseq, y = y_out) diff --git a/tests/testthat/test-plot-summary-api.R b/tests/testthat/test-plot-summary-api.R index e59f49c341..9c2b483eb7 100644 --- a/tests/testthat/test-plot-summary-api.R +++ b/tests/testthat/test-plot-summary-api.R @@ -80,17 +80,17 @@ test_that("layout summary - reversed scales", { lr <- summarise_layout(ggplot_build(pr)) expect_equal(lr$xmin, -7.27) expect_equal(lr$xmax, -1.33) - expect_equal(lr$xscale[[1]]$transformer$name, "reverse") - expect_equal(lr$xscale[[1]]$transformer$transform(5), -5) + expect_equal(lr$xscale[[1]]$transformation$name, "reverse") + expect_equal(lr$xscale[[1]]$transformation$transform(5), -5) }) test_that("layout summary - log scales", { pl <- p + scale_x_log10() + scale_y_continuous(transform = "log2") ll <- summarise_layout(ggplot_build(pl)) - expect_equal(ll$xscale[[1]]$transformer$name, "log-10") - expect_equal(ll$xscale[[1]]$transformer$transform(100), 2) - expect_equal(ll$yscale[[1]]$transformer$name, "log-2") - expect_equal(ll$yscale[[1]]$transformer$transform(16), 4) + expect_equal(ll$xscale[[1]]$transformation$name, "log-10") + expect_equal(ll$xscale[[1]]$transformation$transform(100), 2) + expect_equal(ll$yscale[[1]]$transformation$name, "log-2") + expect_equal(ll$yscale[[1]]$transformation$transform(16), 4) }) test_that("coord summary - basic", { diff --git a/tests/testthat/test-scale-binned.R b/tests/testthat/test-scale-binned.R index 700e7c045c..e252ef9a72 100644 --- a/tests/testthat/test-scale-binned.R +++ b/tests/testthat/test-scale-binned.R @@ -65,7 +65,7 @@ test_that('binned scales can calculate breaks on dates', { scale <- scale_x_binned(transform = "date") scale$train(scale$transform(data)) - breaks <- scale$transformer$inverse(scale$get_breaks()) + breaks <- scale$transformation$inverse(scale$get_breaks()) expect_s3_class(breaks, "Date") expect_equal( @@ -83,7 +83,7 @@ test_that('binned scales can calculate breaks on date-times', { scale <- scale_x_binned(transform = "time") scale$train(scale$transform(data)) - breaks <- scale$transformer$inverse(scale$get_breaks()) + breaks <- scale$transformation$inverse(scale$get_breaks()) expect_s3_class(breaks, "POSIXct") expect_equal( diff --git a/tests/testthat/test-scales.R b/tests/testthat/test-scales.R index 50111cc9ae..4893caceb4 100644 --- a/tests/testthat/test-scales.R +++ b/tests/testthat/test-scales.R @@ -464,7 +464,7 @@ test_that("numeric scale transforms can produce breaks", { scale <- scale_x_continuous(transform = transform) scale$train(scale$transform(limits)) view <- view_scale_primary(scale) - scale$transformer$inverse(view$get_breaks()) + scale$transformation$inverse(view$get_breaks()) } expect_equal(test_breaks("asn", limits = c(0, 1)), diff --git a/vignettes/extending-ggplot2.Rmd b/vignettes/extending-ggplot2.Rmd index 13c80502ac..2f32ae4b68 100644 --- a/vignettes/extending-ggplot2.Rmd +++ b/vignettes/extending-ggplot2.Rmd @@ -864,7 +864,7 @@ FacetTrans <- ggproto("FacetTrans", Facet, if (!is.null(y_scale)) { y_scale_orig <- y_scale$clone() y_scale_new <- y_scale$clone() - y_scale_new$transformer <- params$trans + y_scale_new$transformation <- params$trans # Make sure that oob values are kept y_scale_new$oob <- function(x, ...) x scales$y <- list(y_scale_orig, y_scale_new) From a98974ed4dc361e7e8f35230dc5b377cb464b878 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 11 Dec 2023 16:55:57 +0100 Subject: [PATCH 15/18] rename `AxisSecondary$trans` slot --- R/axis-secondary.R | 17 +++++++++-------- man/ggplot2-ggproto.Rd | 6 +++--- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/R/axis-secondary.R b/R/axis-secondary.R index 6084f4b7de..cd69d43c1e 100644 --- a/R/axis-secondary.R +++ b/R/axis-secondary.R @@ -109,7 +109,7 @@ sec_axis <- function(transform = NULL, transform <- as_function(transform) ggproto(NULL, AxisSecondary, - trans = transform, + transform = transform, name = name, breaks = breaks, labels = labels, @@ -153,7 +153,7 @@ is.derived <- function(x) { #' @usage NULL #' @export AxisSecondary <- ggproto("AxisSecondary", NULL, - trans = NULL, + transform = NULL, axis = NULL, name = waiver(), breaks = waiver(), @@ -165,7 +165,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, detail = 1000, empty = function(self) { - is.null(self$trans) + is.null(self$transform %||% self$trans) }, # Inherit settings from the primary axis/scale @@ -173,7 +173,8 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, if (self$empty()) { return() } - if (!is.function(self$trans)) { + transform <- self$transform %||% self$trans + if (!is.function(transform)) { cli::cli_abort("Transformation for secondary axes must be a function.") } if (is.derived(self$name) && !is.waive(scale$name)) self$name <- scale$name @@ -184,7 +185,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, }, transform_range = function(self, range) { - self$trans(range) + self$transform(range) }, mono_test = function(self, scale){ @@ -237,7 +238,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, # patch for date and datetime scales just to maintain functionality # works only for linear secondary transforms that respect the time or date transform if (transformation$name %in% c("date", "time")) { - temp_scale <- self$create_scale(new_range, trans = transformation) + temp_scale <- self$create_scale(new_range, transformation = transformation) range_info <- temp_scale$break_info() old_val_trans <- rescale(range_info$major, from = c(0, 1), to = range) old_val_minor_trans <- rescale(range_info$minor, from = c(0, 1), to = range) @@ -291,14 +292,14 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, }, # Temporary scale for the purpose of calling break_info() - create_scale = function(self, range, trans = transform_identity()) { + create_scale = function(self, range, transformation = transform_identity()) { scale <- ggproto(NULL, ScaleContinuousPosition, name = self$name, breaks = self$breaks, labels = self$labels, limits = range, expand = c(0, 0), - transformation = trans + transformation = transformation ) scale$train(range) scale diff --git a/man/ggplot2-ggproto.Rd b/man/ggplot2-ggproto.Rd index 789a28db3c..0bd034ad50 100644 --- a/man/ggplot2-ggproto.Rd +++ b/man/ggplot2-ggproto.Rd @@ -515,7 +515,7 @@ Methods: it has no information with which to calculate its \code{limits}). \item \code{clone()} Returns a copy of the scale that can be trained independently without affecting the original scale. -\item \code{transform()} Transforms a vector of values using \code{self$trans}. +\item \code{transform()} Transforms a vector of values using \code{self$transformation}. This occurs before the \code{Stat} is calculated. \item \code{train()} Update the \code{self$range} of observed (transformed) data values with a vector of (possibly) new values. @@ -538,14 +538,14 @@ accept a data frame, and apply the \code{transform}, \code{train}, and \code{map based on the combination of \code{self$limits} and/or the range of observed values (\code{self$range}). \item \code{get_breaks()} Calculates the final scale breaks in transformed data space -based on on the combination of \code{self$breaks}, \code{self$trans$breaks()} (for +based on on the combination of \code{self$breaks}, \code{self$transformation$breaks()} (for continuous scales), and \code{limits}. Breaks outside of \code{limits} are assigned a value of \code{NA} (continuous scales) or dropped (discrete scales). \item \code{get_labels()} Calculates labels for a given set of (transformed) \code{breaks} based on the combination of \code{self$labels} and \code{breaks}. \item \code{get_breaks_minor()} For continuous scales, calculates the final scale minor breaks in transformed data space based on the rescaled \code{breaks}, the value of \code{self$minor_breaks}, -and the value of \code{self$trans$minor_breaks()}. Discrete scales always return \code{NULL}. +and the value of \code{self$transformation$minor_breaks()}. Discrete scales always return \code{NULL}. \item \code{make_title()} Hook to modify the title that is calculated during guide construction (for non-position scales) or when the \code{Layout} calculates the x and y labels (position scales). From 9842121ad08bf012827d5774c7ced75406f9f16d Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Thu, 14 Dec 2023 09:28:06 +0100 Subject: [PATCH 16/18] Implement `get_transformation()` method --- R/scale-.R | 8 ++++++++ R/scale-view.R | 1 + 2 files changed, 9 insertions(+) diff --git a/R/scale-.R b/R/scale-.R index cd3a074040..3b921ec7b1 100644 --- a/R/scale-.R +++ b/R/scale-.R @@ -551,6 +551,14 @@ Scale <- ggproto("Scale", NULL, cli::cli_abort("Not implemented.", call = self$call) }, + get_transformation = function(self) { + if (!is.null(self$trans)) { + deprecate_soft0("3.5.0", I("Scale$trans"), I("Scale$transformation")) + return(self$trans) + } + self$transformation + }, + clone = function(self) { cli::cli_abort("Not implemented.", call = self$call) }, diff --git a/R/scale-view.R b/R/scale-view.R index 1402a3ffee..3a068ea81c 100644 --- a/R/scale-view.R +++ b/R/scale-view.R @@ -131,6 +131,7 @@ ViewScale <- ggproto("ViewScale", NULL, get_breaks = function(self) self$breaks, get_breaks_minor = function(self) self$minor_breaks, get_labels = function(self, breaks = self$get_breaks()) self$scale$get_labels(breaks), + get_transformation = function(self) self$scale$get_transformation(), rescale = function(self, x) { self$scale$rescale(x, self$limits, self$continuous_range) }, From adbdb40a88a5f934fad812bdb832b0f5ab3f3d5b Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Thu, 14 Dec 2023 09:28:29 +0100 Subject: [PATCH 17/18] Use `get_transformation()` --- R/axis-secondary.R | 4 ++-- R/coord-transform.R | 2 +- R/guide-axis-logticks.R | 2 +- R/scale-.R | 17 +++++++---------- R/scale-expansion.R | 2 +- 5 files changed, 12 insertions(+), 15 deletions(-) diff --git a/R/axis-secondary.R b/R/axis-secondary.R index cd69d43c1e..673cc0ef5b 100644 --- a/R/axis-secondary.R +++ b/R/axis-secondary.R @@ -196,7 +196,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, return() } - transformation <- scale$transformation %||% scale$trans + transformation <- scale$get_transformation() along_range <- seq(range[1], range[2], length.out = self$detail) old_range <- transformation$inverse(along_range) @@ -215,7 +215,7 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, self$mono_test(scale) # Get scale's original range before transformation - transformation <- scale$transformation %||% scale$trans + transformation <- scale$get_transformation() along_range <- seq(range[1], range[2], length.out = self$detail) old_range <- transformation$inverse(along_range) diff --git a/R/coord-transform.R b/R/coord-transform.R index 9df4e91761..79d651e8af 100644 --- a/R/coord-transform.R +++ b/R/coord-transform.R @@ -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) - transformation <- scale$transformation %||% scale$trans %||% transform_identity() + transformation <- scale$get_transformation() %||% transform_identity() coord_limits <- coord_limits %||% transformation$inverse(c(NA, NA)) scale_limits <- scale$get_limits() diff --git a/R/guide-axis-logticks.R b/R/guide-axis-logticks.R index 5809fc9cd7..c197799418 100644 --- a/R/guide-axis-logticks.R +++ b/R/guide-axis-logticks.R @@ -156,7 +156,7 @@ GuideAxisLogticks <- ggproto( } transformation <- transform_log(base = params$prescale_base) } else { - transformation <- scale$scale$transformation %||% scale$scale$trans + transformation <- scale$get_transformation() } # Reconstruct original range diff --git a/R/scale-.R b/R/scale-.R index 3b921ec7b1..ceabf714db 100644 --- a/R/scale-.R +++ b/R/scale-.R @@ -605,10 +605,7 @@ check_breaks_labels <- function(breaks, labels, call = NULL) { } default_transform <- function(self, x) { - if (!is.null(self$trans)) { - deprecate_soft0("3.5.0", I("Scale$trans"), I("Scale$transformation")) - } - transformation <- self$transformation %||% self$trans + transformation <- self$get_transformation() new_x <- transformation$transform(x) check_transformation(x, new_x, self$transformation$name, self$call) new_x @@ -679,7 +676,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, if (is.null(self$limits)) { self$range$range } else if (is.function(self$limits)) { - transformation <- self$transformation %||% self$trans + transformation <- self$get_transformation() # if limits is a function, it expects to work in data space transformation$transform(self$limits(transformation$inverse(self$range$range))) } else { @@ -696,7 +693,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, if (self$is_empty()) { return(numeric()) } - transformation <- self$transformation %||% self$trans + transformation <- self$get_transformation() # Ensure limits don't exceed domain (#980) domain <- suppressWarnings(transformation$transform(transformation$domain)) domain <- sort(domain) @@ -764,7 +761,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, # some transforms assume finite major breaks b <- b[is.finite(b)] - transformation <- self$transformation %||% self$trans + transformation <- self$get_transformation() if (is.waive(self$minor_breaks)) { if (is.null(b)) { breaks <- NULL @@ -798,7 +795,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, return(NULL) } - transformation <- self$transformation %||% self$trans + transformation <- self$get_transformation() breaks <- transformation$inverse(breaks) if (is.null(self$labels)) { @@ -1166,7 +1163,7 @@ ScaleBinned <- ggproto("ScaleBinned", Scale, get_breaks = function(self, limits = self$get_limits()) { if (self$is_empty()) return(numeric()) - transformation <- self$transformation %||% self$trans + transformation <- self$get_transformation() limits <- transformation$inverse(limits) is_rev <- limits[2] < limits[1] @@ -1259,7 +1256,7 @@ ScaleBinned <- ggproto("ScaleBinned", Scale, get_labels = function(self, breaks = self$get_breaks()) { if (is.null(breaks)) return(NULL) - transformation <- self$transformation %||% self$trans + transformation <- self$get_transformation() breaks <- transformation$inverse(breaks) if (is.null(self$labels)) { diff --git a/R/scale-expansion.R b/R/scale-expansion.R index 58c5f48e5f..9ede4c1400 100644 --- a/R/scale-expansion.R +++ b/R/scale-expansion.R @@ -145,7 +145,7 @@ expand_limits_scale <- function(scale, expand = expansion(0, 0), limits = waiver } else { # using the inverse transform to resolve the NA value is needed for date/datetime/time # scales, which refuse to transform objects of the incorrect type - transformation <- scale$transformation %||% scale$trans + transformation <- scale$get_transformation() coord_limits <- coord_limits %||% transformation$inverse(c(NA_real_, NA_real_)) coord_limits_scale <- transformation$transform(coord_limits) expand_limits_continuous(limits, expand, coord_limits_scale) From 8cf9cd1255b23fb47e1a4b42b6d8b1fdd257162b Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Thu, 14 Dec 2023 09:47:53 +0100 Subject: [PATCH 18/18] Document `get_transformation()` method --- R/scale-.R | 2 ++ man/ggplot2-ggproto.Rd | 1 + 2 files changed, 3 insertions(+) diff --git a/R/scale-.R b/R/scale-.R index ceabf714db..2d22133fae 100644 --- a/R/scale-.R +++ b/R/scale-.R @@ -397,6 +397,8 @@ binned_scale <- function(aesthetics, scale_name = deprecated(), palette, name = #' in transformed data space based on the rescaled `breaks`, the value of `self$minor_breaks`, #' and the value of `self$transformation$minor_breaks()`. Discrete scales always return `NULL`. #' +#' - `get_transformation()` Returns the scale's transformation object. +#' #' - `make_title()` Hook to modify the title that is calculated during guide construction #' (for non-position scales) or when the `Layout` calculates the x and y labels #' (position scales). diff --git a/man/ggplot2-ggproto.Rd b/man/ggplot2-ggproto.Rd index 0bd034ad50..d7ca283a92 100644 --- a/man/ggplot2-ggproto.Rd +++ b/man/ggplot2-ggproto.Rd @@ -546,6 +546,7 @@ based on the combination of \code{self$labels} and \code{breaks}. \item \code{get_breaks_minor()} For continuous scales, calculates the final scale minor breaks in transformed data space based on the rescaled \code{breaks}, the value of \code{self$minor_breaks}, and the value of \code{self$transformation$minor_breaks()}. Discrete scales always return \code{NULL}. +\item \code{get_transformation()} Returns the scale's transformation object. \item \code{make_title()} Hook to modify the title that is calculated during guide construction (for non-position scales) or when the \code{Layout} calculates the x and y labels (position scales).