From a590298278e32c9a31419c3c2953168b14896650 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 6 Nov 2023 16:02:49 +0100 Subject: [PATCH 1/8] `_trans` suffix to `transform_` prefix --- NAMESPACE | 32 ++- R/documentation.R | 2 +- R/scale-continuous.R | 6 +- R/{trans-compose.R => transform-compose.R} | 10 +- R/{trans-date.R => transform-date.R} | 53 +++-- R/{trans-numeric.R => transform-numeric.R} | 219 ++++++++++++------ R/{trans.R => transform.R} | 34 +-- README.Rmd | 4 +- README.md | 4 +- man/cscale.Rd | 6 +- man/label_bytes.Rd | 4 + man/label_currency.Rd | 4 + man/label_date.Rd | 4 + man/label_number_si.Rd | 4 + man/label_ordinal.Rd | 4 + man/label_percent.Rd | 4 + man/log_trans.Rd | 48 ---- man/trans_new.Rd | 10 +- man/trans_range.Rd | 8 +- man/{asinh_trans.Rd => transform_asinh.Rd} | 9 +- man/{asn_trans.Rd => transform_asn.Rd} | 9 +- man/{atanh_trans.Rd => transform_atanh.Rd} | 9 +- man/{boxcox_trans.Rd => transform_boxcox.Rd} | 30 ++- ...{compose_trans.Rd => transform_compose.Rd} | 7 +- man/{date_trans.Rd => transform_date.Rd} | 9 +- man/{exp_trans.Rd => transform_exp.Rd} | 15 +- ...dentity_trans.Rd => transform_identity.Rd} | 9 +- man/transform_log.Rd | 63 +++++ ...lity_trans.Rd => transform_probability.Rd} | 17 +- ...rocal_trans.Rd => transform_reciprocal.Rd} | 9 +- ...{reverse_trans.Rd => transform_reverse.Rd} | 9 +- man/{sqrt_trans.Rd => transform_sqrt.Rd} | 9 +- man/{time_trans.Rd => transform_time.Rd} | 9 +- ...imespan_trans.Rd => transform_timespan.Rd} | 28 ++- man/{yj_trans.Rd => transform_yj.Rd} | 21 +- tests/testthat/_snaps/trans-compose.md | 10 +- tests/testthat/_snaps/trans-date.md | 4 +- tests/testthat/_snaps/trans.md | 8 +- tests/testthat/test-trans-compose.R | 24 +- tests/testthat/test-trans-date.R | 14 +- tests/testthat/test-trans-numeric.R | 110 ++++----- tests/testthat/test-trans.R | 20 +- 42 files changed, 572 insertions(+), 340 deletions(-) rename R/{trans-compose.R => transform-compose.R} (91%) rename R/{trans-date.R => transform-date.R} (68%) rename R/{trans-numeric.R => transform-numeric.R} (68%) rename R/{trans.R => transform.R} (82%) delete mode 100644 man/log_trans.Rd rename man/{asinh_trans.Rd => transform_asinh.Rd} (55%) rename man/{asn_trans.Rd => transform_asn.Rd} (60%) rename man/{atanh_trans.Rd => transform_atanh.Rd} (52%) rename man/{boxcox_trans.Rd => transform_boxcox.Rd} (70%) rename man/{compose_trans.Rd => transform_compose.Rd} (82%) rename man/{date_trans.Rd => transform_date.Rd} (71%) rename man/{exp_trans.Rd => transform_exp.Rd} (50%) rename man/{identity_trans.Rd => transform_identity.Rd} (54%) create mode 100644 man/transform_log.Rd rename man/{probability_trans.Rd => transform_probability.Rd} (66%) rename man/{reciprocal_trans.Rd => transform_reciprocal.Rd} (51%) rename man/{reverse_trans.Rd => transform_reverse.Rd} (69%) rename man/{sqrt_trans.Rd => transform_sqrt.Rd} (59%) rename man/{time_trans.Rd => transform_time.Rd} (78%) rename man/{timespan_trans.Rd => transform_timespan.Rd} (53%) rename man/{yj_trans.Rd => transform_yj.Rd} (73%) diff --git a/NAMESPACE b/NAMESPACE index 221150ac..ccd670b1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,13 +4,13 @@ S3method(fullseq,Date) S3method(fullseq,POSIXt) S3method(fullseq,difftime) S3method(fullseq,numeric) -S3method(lines,trans) +S3method(lines,transform) S3method(offset_by,Date) S3method(offset_by,POSIXt) S3method(offset_by,difftime) S3method(offset_by,numeric) -S3method(plot,trans) -S3method(print,trans) +S3method(plot,transform) +S3method(print,transform) S3method(rescale,"NULL") S3method(rescale,AsIs) S3method(rescale,Date) @@ -37,6 +37,7 @@ export(abs_area) export(alpha) export(area_pal) export(as.trans) +export(as.transform) export(asinh_trans) export(asn_trans) export(atanh_trans) @@ -89,6 +90,7 @@ export(hue_pal) export(identity_pal) export(identity_trans) export(is.trans) +export(is.transform) export(label_bytes) export(label_comma) export(label_currency) @@ -171,6 +173,30 @@ export(trans_breaks) export(trans_format) export(trans_new) export(trans_range) +export(transform_asinh) +export(transform_asn) +export(transform_atanh) +export(transform_boxcox) +export(transform_compose) +export(transform_date) +export(transform_exp) +export(transform_hms) +export(transform_identity) +export(transform_log) +export(transform_log10) +export(transform_log1p) +export(transform_log2) +export(transform_logit) +export(transform_modulus) +export(transform_probability) +export(transform_probit) +export(transform_pseudo_log) +export(transform_reciprocal) +export(transform_reverse) +export(transform_sqrt) +export(transform_time) +export(transform_timespan) +export(transform_yj) export(unit_format) export(viridis_pal) export(wrap_format) diff --git a/R/documentation.R b/R/documentation.R index 6b8fcc68..8669f7b3 100644 --- a/R/documentation.R +++ b/R/documentation.R @@ -7,6 +7,6 @@ seealso <- function(pattern) { paste0("\\code{\\link{", names, "}}", collapse = ", ") } -seealso_trans <- function() seealso("_trans$") +seealso_transform <- function() seealso("^transform_") seealso_pal <- function() seealso("_pal$") diff --git a/R/scale-continuous.R b/R/scale-continuous.R index a26334ae..0957e802 100644 --- a/R/scale-continuous.R +++ b/R/scale-continuous.R @@ -11,19 +11,19 @@ #' leaves the data unchanged. #' #' Built in transformations: -#' \Sexpr[results=rd,stage=build]{scales:::seealso_trans()}. +#' \Sexpr[results=rd,stage=build]{scales:::seealso_transform()}. #' @export #' @examples #' with(mtcars, plot(disp, mpg, cex = cscale(hp, rescale_pal()))) #' with(mtcars, plot(disp, mpg, cex = cscale(hp, rescale_pal(), -#' trans = sqrt_trans() +#' trans = transform_sqrt() #' ))) #' with(mtcars, plot(disp, mpg, cex = cscale(hp, area_pal()))) #' with(mtcars, plot(disp, mpg, #' pch = 20, cex = 5, #' col = cscale(hp, seq_gradient_pal("grey80", "black")) #' )) -cscale <- function(x, palette, na.value = NA_real_, trans = identity_trans()) { +cscale <- function(x, palette, na.value = NA_real_, trans = transform_identity()) { if (!is.trans(trans)) cli::cli_abort("{.arg trans} must be a {.cls trans} object") x <- trans$transform(x) diff --git a/R/trans-compose.R b/R/transform-compose.R similarity index 91% rename from R/trans-compose.R rename to R/transform-compose.R index 4cd1330a..ab3a739d 100644 --- a/R/trans-compose.R +++ b/R/transform-compose.R @@ -10,10 +10,10 @@ #' @examples #' demo_continuous(10^c(-2:4), trans = "log10", labels = label_log()) #' demo_continuous(10^c(-2:4), trans = c("log10", "reverse"), labels = label_log()) -compose_trans <- function(...) { - trans_list <- lapply(list2(...), as.trans) +transform_compose <- function(...) { + trans_list <- lapply(list2(...), as.transform) if (length(trans_list) == 0) { - cli::cli_abort("{.fun compose_trans} must include at least 1 transformer to compose") + cli::cli_abort("{.fun transform_compose} must include at least 1 transformer to compose") } # Resolve domains. First push the domain of the first transformation all the @@ -53,6 +53,10 @@ compose_trans <- function(...) { ) } +#' @export +#' @rdname transform_compose +compose_trans <- transform_compose + compose_fwd <- function(x, trans_list) { for (trans in trans_list) { x <- trans$transform(x) diff --git a/R/trans-date.R b/R/transform-date.R similarity index 68% rename from R/trans-date.R rename to R/transform-date.R index 9a17ce0c..743bb091 100644 --- a/R/trans-date.R +++ b/R/transform-date.R @@ -3,11 +3,11 @@ #' @export #' @examples #' years <- seq(as.Date("1910/1/1"), as.Date("1999/1/1"), "years") -#' t <- date_trans() +#' t <- transform_date() #' t$transform(years) #' t$inverse(t$transform(years)) #' t$format(t$breaks(range(years))) -date_trans <- function() { +transform_date <- function() { trans_new("date", transform = "from_date", inverse = "to_date", @@ -16,10 +16,14 @@ date_trans <- function() { ) } +#' @export +#' @rdname transform_date +date_trans <- transform_date + to_date <- function(x) structure(x, class = "Date") from_date <- function(x) { if (!inherits(x, "Date")) { - cli::cli_abort("{.fun date_trans} works with objects of class {.cls Date} only") + cli::cli_abort("{.fun transform_date} works with objects of class {.cls Date} only") } structure(as.numeric(x), names = names(x)) } @@ -31,11 +35,11 @@ from_date <- function(x) { #' @export #' @examples #' hours <- seq(ISOdate(2000, 3, 20, tz = ""), by = "hour", length.out = 10) -#' t <- time_trans() +#' t <- transform_time() #' t$transform(hours) #' t$inverse(t$transform(hours)) #' t$format(t$breaks(range(hours))) -time_trans <- function(tz = NULL) { +transform_time <- function(tz = NULL) { force(tz) to_time <- function(x) { structure(x, class = c("POSIXt", "POSIXct"), tzone = tz) @@ -43,7 +47,7 @@ time_trans <- function(tz = NULL) { from_time <- function(x) { if (!inherits(x, "POSIXct")) { - cli::cli_abort("{.fun time_trans} works with objects of class {.cls POSIXct} only") + cli::cli_abort("{.fun transform_time} works with objects of class {.cls POSIXct} only") } if (is.null(tz)) { tz <<- attr(as.POSIXlt(x), "tzone")[[1]] @@ -59,27 +63,31 @@ time_trans <- function(tz = NULL) { ) } +#' @export +#' @rdname transform_time +time_trans <- transform_time + #' Transformation for times (class hms) #' -#' `timespan_trans()` provides transformations for data encoding time passed +#' `transform_timespan()` provides transformations for data encoding time passed #' along with breaks and label formatting showing standard unit of time fitting -#' the range of the data. `hms_trans()` provides the same but using standard hms -#' idioms and formatting. +#' the range of the data. `transform_hms()` provides the same but using standard +#' hms idioms and formatting. #' #' @inheritParams label_timespan #' @export #' @examples -#' # timespan_trans allows you to specify the time unit numeric data is +#' # transform_timespan allows you to specify the time unit numeric data is #' # interpreted in -#' min_trans <- timespan_trans("mins") -#' demo_timespan(seq(0, 100), trans = min_trans) +#' trans_min <- transform_timespan("mins") +#' demo_timespan(seq(0, 100), trans = trans_min) #' # Input already in difftime format is interpreted correctly -#' demo_timespan(as.difftime(seq(0, 100), units = "secs"), trans = min_trans) +#' demo_timespan(as.difftime(seq(0, 100), units = "secs"), trans = trans_min) #' #' if (require("hms")) { -#' # hms_trans always assumes seconds +#' # transform_hms always assumes seconds #' hms <- round(runif(10) * 86400) -#' t <- hms_trans() +#' t <- transform_hms() #' t$transform(hms) #' t$inverse(t$transform(hms)) #' t$breaks(hms) @@ -87,7 +95,7 @@ time_trans <- function(tz = NULL) { #' demo_timespan(hms, trans = t) #' } #' -timespan_trans <- function(unit = c("secs", "mins", "hours", "days", "weeks")) { +transform_timespan <- function(unit = c("secs", "mins", "hours", "days", "weeks")) { unit <- arg_match(unit) trans_new( "timespan", @@ -103,9 +111,14 @@ timespan_trans <- function(unit = c("secs", "mins", "hours", "days", "weeks")) { format = label_timespan(unit) ) } -#' @rdname timespan_trans + #' @export -hms_trans <- function() { +#' @rdname transform_timespan +timespan_trans <- transform_timespan + +#' @rdname transform_timespan +#' @export +transform_hms <- function() { trans_new( "hms", transform = function(x) { @@ -116,6 +129,10 @@ hms_trans <- function() { ) } +#' @rdname transform_timespan +#' @export +hms_trans <- transform_hms + breaks_hms <- function(n = 5) { base_breaks <- breaks_timespan("secs", n) function(x) { diff --git a/R/trans-numeric.R b/R/transform-numeric.R similarity index 68% rename from R/trans-numeric.R rename to R/transform-numeric.R index 301e15b0..ade69f63 100644 --- a/R/trans-numeric.R +++ b/R/transform-numeric.R @@ -5,8 +5,8 @@ #' #' @export #' @examples -#' plot(asn_trans(), xlim = c(0, 1)) -asn_trans <- function() { +#' plot(transform_asn(), xlim = c(0, 1)) +transform_asn <- function() { trans_new( "asn", function(x) 2 * asin(sqrt(x)), @@ -17,12 +17,16 @@ asn_trans <- function() { ) } +#' @rdname transform_asn +#' @export +asn_trans <- transform_asn + #' Arc-tangent transformation #' #' @export #' @examples -#' plot(atanh_trans(), xlim = c(-1, 1)) -atanh_trans <- function() { +#' plot(transform_atanh(), xlim = c(-1, 1)) +transform_atanh <- function() { trans_new( "atanh", "atanh", @@ -33,12 +37,16 @@ atanh_trans <- function() { ) } +#' @export +#' @rdname transform_atanh +atanh_trans <- transform_atanh + #' Inverse Hyperbolic Sine transformation #' #' @export #' @examples -#' plot(asinh_trans(), xlim = c(-1e2, 1e2)) -asinh_trans <- function() { +#' plot(transform_asinh(), xlim = c(-1e2, 1e2)) +transform_asinh <- function() { trans_new( "asinh", transform = asinh, @@ -48,6 +56,10 @@ asinh_trans <- function() { ) } +#' @export +#' @rdname transform_asinh +asinh_trans <- transform_asinh + #' Box-Cox & modulus transformations #' #' The Box-Cox transformation is a flexible transformation, often used to @@ -69,9 +81,9 @@ asinh_trans <- function() { #' #' @param p Transformation exponent, \eqn{\lambda}. #' @param offset Constant offset. 0 for Box-Cox type 1, -#' otherwise any non-negative constant (Box-Cox type 2). `modulus_trans()` +#' otherwise any non-negative constant (Box-Cox type 2). `transform_modulus()` #' sets the default to 1. -#' @seealso [yj_trans()] +#' @seealso [transform_yj()] #' @references Box, G. E., & Cox, D. R. (1964). An analysis of transformations. #' Journal of the Royal Statistical Society. Series B (Methodological), 211-252. #' \url{https://www.jstor.org/stable/2984418} @@ -81,16 +93,16 @@ asinh_trans <- function() { #' \url{https://www.jstor.org/stable/2986305} #' @export #' @examples -#' plot(boxcox_trans(-1), xlim = c(0, 10)) -#' plot(boxcox_trans(0), xlim = c(0, 10)) -#' plot(boxcox_trans(1), xlim = c(0, 10)) -#' plot(boxcox_trans(2), xlim = c(0, 10)) +#' plot(transform_boxcox(-1), xlim = c(0, 10)) +#' plot(transform_boxcox(0), xlim = c(0, 10)) +#' plot(transform_boxcox(1), xlim = c(0, 10)) +#' plot(transform_boxcox(2), xlim = c(0, 10)) #' -#' plot(modulus_trans(-1), xlim = c(-10, 10)) -#' plot(modulus_trans(0), xlim = c(-10, 10)) -#' plot(modulus_trans(1), xlim = c(-10, 10)) -#' plot(modulus_trans(2), xlim = c(-10, 10)) -boxcox_trans <- function(p, offset = 0) { +#' plot(transform_modulus(-1), xlim = c(-10, 10)) +#' plot(transform_modulus(0), xlim = c(-10, 10)) +#' plot(transform_modulus(1), xlim = c(-10, 10)) +#' plot(transform_modulus(2), xlim = c(-10, 10)) +transform_boxcox <- function(p, offset = 0) { if (abs(p) < 1e-07) { trans <- function(x) log(x + offset) inv <- function(x) exp(x) - offset @@ -106,8 +118,8 @@ boxcox_trans <- function(p, offset = 0) { trans_with_check <- function(x) { if (any((x + offset) < 0, na.rm = TRUE)) { cli::cli_abort(c( - "{.fun boxcox_trans} must be given only positive values", - i = "Consider using {.fun modulus_trans} instead?" + "{.fun transform_boxcox} must be given only positive values", + i = "Consider using {.fun transform_modulus} instead?" )) } trans(x) @@ -123,9 +135,13 @@ boxcox_trans <- function(p, offset = 0) { ) } -#' @rdname boxcox_trans #' @export -modulus_trans <- function(p, offset = 1) { +#' @rdname transform_boxcox +boxcox_trans <- transform_boxcox + +#' @rdname transform_boxcox +#' @export +transform_modulus <- function(p, offset = 1) { if (abs(p) < 1e-07) { trans <- function(x) sign(x) * log(abs(x) + offset) inv <- function(x) sign(x) * (exp(abs(x)) - offset) @@ -143,11 +159,15 @@ modulus_trans <- function(p, offset = 1) { ) } +#' @rdname transform_boxcox +#' @export +modulus_trans <- transform_modulus + #' Yeo-Johnson transformation #' -#' The Yeo-Johnson transformation is a flexible transformation that is similiar -#' to Box-Cox, [boxcox_trans()], but does not require input values to be greater -#' than zero. +#' The Yeo-Johnson transformation is a flexible transformation that is similar +#' to Box-Cox, [transform_boxcox()], but does not require input values to be +#' greater than zero. #' #' The transformation takes one of four forms depending on the values of `y` and \eqn{\lambda}. #' @@ -166,11 +186,11 @@ modulus_trans <- function(p, offset = 1) { #' \url{https://www.jstor.org/stable/2673623} #' @export #' @examples -#' plot(yj_trans(-1), xlim = c(-10, 10)) -#' plot(yj_trans(0), xlim = c(-10, 10)) -#' plot(yj_trans(1), xlim = c(-10, 10)) -#' plot(yj_trans(2), xlim = c(-10, 10)) -yj_trans <- function(p) { +#' plot(transform_yj(-1), xlim = c(-10, 10)) +#' plot(transform_yj(0), xlim = c(-10, 10)) +#' plot(transform_yj(1), xlim = c(-10, 10)) +#' plot(transform_yj(2), xlim = c(-10, 10)) +transform_yj <- function(p) { eps <- 1e-7 if (abs(p) < eps) { @@ -206,6 +226,10 @@ yj_trans <- function(p) { ) } +#' @export +#' @rdname transform_yj +yj_trans <- transform_yj + trans_two_sided <- function(x, pos, neg, f_at_0 = 0) { out <- rep(NA_real_, length(x)) present <- !is.na(x) @@ -220,11 +244,11 @@ trans_two_sided <- function(x, pos, neg, f_at_0 = 0) { #' @param base Base of logarithm #' @export #' @examples -#' plot(exp_trans(0.5), xlim = c(-2, 2)) -#' plot(exp_trans(1), xlim = c(-2, 2)) -#' plot(exp_trans(2), xlim = c(-2, 2)) -#' plot(exp_trans(), xlim = c(-2, 2)) -exp_trans <- function(base = exp(1)) { +#' plot(transform_exp(0.5), xlim = c(-2, 2)) +#' plot(transform_exp(1), xlim = c(-2, 2)) +#' plot(transform_exp(2), xlim = c(-2, 2)) +#' plot(transform_exp(), xlim = c(-2, 2)) +transform_exp <- function(base = exp(1)) { force(base) trans_new( paste0("power-", format(base)), @@ -235,12 +259,16 @@ exp_trans <- function(base = exp(1)) { ) } +#' @export +#' @rdname transform_exp +exp_trans <- transform_exp + #' Identity transformation (do nothing) #' #' @export #' @examples -#' plot(identity_trans(), xlim = c(-1, 1)) -identity_trans <- function() { +#' plot(transform_identity(), xlim = c(-1, 1)) +transform_identity <- function() { trans_new( "identity", "force", @@ -250,31 +278,35 @@ identity_trans <- function() { ) } +#' @export +#' @rdname transform_identity +identity_trans <- transform_identity + #' Log transformations #' -#' * `log_trans()`: `log(x)` +#' * `transform_log()`: `log(x)` #' * `log1p()`: `log(x + 1)` -#' * `pseudo_log_trans()`: smoothly transition to linear scale around 0. +#' * `transform_pseudo_log()`: smoothly transition to linear scale around 0. #' #' @param base base of logarithm #' @export #' @examples -#' plot(log2_trans(), xlim = c(0, 5)) -#' plot(log_trans(), xlim = c(0, 5)) -#' plot(log10_trans(), xlim = c(0, 5)) +#' plot(transform_log2(), xlim = c(0, 5)) +#' plot(transform_log(), xlim = c(0, 5)) +#' plot(transform_log10(), xlim = c(0, 5)) #' -#' plot(log_trans(), xlim = c(0, 2)) -#' plot(log1p_trans(), xlim = c(-1, 1)) +#' plot(transform_log(), xlim = c(0, 2)) +#' plot(transform_log1p(), xlim = c(-1, 1)) #' #' # The pseudo-log is defined for all real numbers -#' plot(pseudo_log_trans(), xlim = c(-5, 5)) -#' lines(log_trans(), xlim = c(0, 5), col = "red") +#' plot(transform_pseudo_log(), xlim = c(-5, 5)) +#' lines(transform_log(), xlim = c(0, 5), col = "red") #' -#' # For large positives nubmers it's very close to log -#' plot(pseudo_log_trans(), xlim = c(1, 20)) -#' lines(log_trans(), xlim = c(1, 20), col = "red") -log_trans <- function(base = exp(1)) { +#' # For large positives numbers it's very close to log +#' plot(transform_pseudo_log(), xlim = c(1, 20)) +#' lines(transform_log(), xlim = c(1, 20), col = "red") +transform_log <- function(base = exp(1)) { force(base) trans_new( paste0("log-", format(base)), @@ -287,20 +319,20 @@ log_trans <- function(base = exp(1)) { ) } #' @export -#' @rdname log_trans -log10_trans <- function() { - log_trans(10) +#' @rdname transform_log +transform_log10 <- function() { + transform_log(10) } #' @export -#' @rdname log_trans -log2_trans <- function() { - log_trans(2) +#' @rdname transform_log +transform_log2 <- function() { + transform_log(2) } -#' @rdname log_trans +#' @rdname transform_log #' @export -log1p_trans <- function() { +transform_log1p <- function() { trans_new( "log1p", "log1p", @@ -311,10 +343,24 @@ log1p_trans <- function() { ) } -#' @rdname log_trans +#' @export +#' @rdname transform_log +log_trans <- transform_log +#' @export +#' @rdname transform_log +log10_trans <- transform_log10 +#' @export +#' @rdname transform_log +log2_trans <- transform_log2 +#' @export +#' @rdname transform_log +log1p_trans <- transform_log1p + + +#' @rdname transform_log #' @param sigma Scaling factor for the linear part of pseudo-log transformation. #' @export -pseudo_log_trans <- function(sigma = 1, base = exp(1)) { +transform_pseudo_log <- function(sigma = 1, base = exp(1)) { trans_new( "pseudo_log", function(x) asinh(x / (2 * sigma)) / log(base), @@ -324,6 +370,10 @@ pseudo_log_trans <- function(sigma = 1, base = exp(1)) { ) } +#' @export +#' @rdname transform_log +pseudo_log_trans <- transform_pseudo_log + #' Probability transformation #' #' @param distribution probability distribution. Should be standard R @@ -333,9 +383,9 @@ pseudo_log_trans <- function(sigma = 1, base = exp(1)) { #' @param ... other arguments passed on to distribution and quantile functions #' @export #' @examples -#' plot(logit_trans(), xlim = c(0, 1)) -#' plot(probit_trans(), xlim = c(0, 1)) -probability_trans <- function(distribution, ...) { +#' plot(transform_logit(), xlim = c(0, 1)) +#' plot(transform_probit(), xlim = c(0, 1)) +transform_probability <- function(distribution, ...) { qfun <- match.fun(paste0("q", distribution)) pfun <- match.fun(paste0("p", distribution)) dfun <- match.fun(paste0("d", distribution)) @@ -349,19 +399,32 @@ probability_trans <- function(distribution, ...) { domain = c(0, 1) ) } + +#' @export +#' @rdname transform_probability +transform_logit <- function() transform_probability("logis") +#' @export +#' @rdname transform_probability +transform_probit <- function() transform_probability("norm") + + +#' @export +#' @rdname transform_probability +probability_trans <- transform_probability #' @export -#' @rdname probability_trans -logit_trans <- function() probability_trans("logis") +#' @rdname transform_probability +logit_trans <- transform_logit #' @export -#' @rdname probability_trans -probit_trans <- function() probability_trans("norm") +#' @rdname transform_probability +probit_trans <- transform_probit + #' Reciprocal transformation #' #' @export #' @examples -#' plot(reciprocal_trans(), xlim = c(0, 1)) -reciprocal_trans <- function() { +#' plot(transform_reciprocal(), xlim = c(0, 1)) +transform_reciprocal <- function() { trans_new( "reciprocal", function(x) 1 / x, @@ -371,6 +434,10 @@ reciprocal_trans <- function() { ) } +#' @export +#' @rdname transform_reciprocal +reciprocal_trans <- transform_reciprocal + #' Reverse transformation #' #' reversing transformation works by multiplying the input with -1. This means @@ -379,8 +446,8 @@ reciprocal_trans <- function() { #' #' @export #' @examples -#' plot(reverse_trans(), xlim = c(-1, 1)) -reverse_trans <- function() { +#' plot(transform_reverse(), xlim = c(-1, 1)) +transform_reverse <- function() { trans_new( "reverse", function(x) -x, @@ -391,6 +458,10 @@ reverse_trans <- function() { ) } +#' @export +#' @rdname transform_reverse +reverse_trans <- transform_reverse + #' Square-root transformation #' #' This is the variance stabilising transformation for the Poisson @@ -398,8 +469,8 @@ reverse_trans <- function() { #' #' @export #' @examples -#' plot(sqrt_trans(), xlim = c(0, 5)) -sqrt_trans <- function() { +#' plot(transform_sqrt(), xlim = c(0, 5)) +transform_sqrt <- function() { trans_new( "sqrt", "sqrt", @@ -409,3 +480,7 @@ sqrt_trans <- function() { domain = c(0, Inf) ) } + +#' @export +#' @rdname transform_sqrt +sqrt_trans <- transform_sqrt diff --git a/R/trans.R b/R/transform.R similarity index 82% rename from R/trans.R rename to R/transform.R index 38e43b69..0c978840 100644 --- a/R/trans.R +++ b/R/transform.R @@ -24,7 +24,7 @@ #' @param domain the allowed range of the data to be transformed. The function #' in the `transform` argument is expected to be able to transform the `domain` #' argument. -#' @seealso \Sexpr[results=rd,stage=build]{scales:::seealso_trans()} +#' @seealso \Sexpr[results=rd,stage=build]{scales:::seealso_transform()} #' @export #' @keywords internal #' @aliases trans @@ -50,22 +50,26 @@ trans_new <- function(name, transform, inverse, format = format, domain = domain ), - class = "trans" + class = "transform" ) } #' @rdname trans_new #' @export -is.trans <- function(x) inherits(x, "trans") +is.transform <- function(x) inherits(x, "transform") #' @export -print.trans <- function(x, ...) { +#' @rdname trans_new +is.trans <- is.transform + +#' @export +print.transform <- function(x, ...) { cat("Transformer: ", x$name, " [", x$domain[[1]], ", ", x$domain[[2]], "]\n", sep = "") invisible(x) } #' @export -plot.trans <- function(x, y, ..., xlim, ylim = NULL) { +plot.transform <- function(x, y, ..., xlim, ylim = NULL) { if (is.null(ylim)) { ylim <- range(x$transform(seq(xlim[1], xlim[2], length = 100)), finite = TRUE) } @@ -84,7 +88,7 @@ plot.trans <- function(x, y, ..., xlim, ylim = NULL) { } #' @export -lines.trans <- function(x, ..., xlim) { +lines.transform <- function(x, ..., xlim) { xgrid <- seq(xlim[1], xlim[2], length = 100) y <- suppressWarnings(x$transform(xgrid)) @@ -93,7 +97,7 @@ lines.trans <- function(x, ..., xlim) { #' @rdname trans_new #' @export -as.trans <- function(x, arg = deparse(substitute(x))) { +as.transform <- function(x, arg = deparse(substitute(x))) { if (is.trans(x)) { x } else if (is.character(x) && length(x) >= 1) { @@ -101,23 +105,27 @@ as.trans <- function(x, arg = deparse(substitute(x))) { f <- paste0(x, "_trans") match.fun(f)() } else { - compose_trans(!!!x) + transform_compose(!!!x) } } else { cli::cli_abort(sprintf("{.arg %s} must be a character vector or a transformer object", arg)) } } +#' @export +#' @rdname trans_new +as.trans <- as.transform + #' Compute range of transformed values #' -#' Silently drops any ranges outside of the domain of `trans`. +#' Silently drops any ranges outside of the domain of `transform`. #' -#' @param trans a transformation object, or the name of a transformation object +#' @param transform a transformation object, or the name of a transformation object #' given as a string. #' @param x a numeric vector to compute the range of #' @export #' @keywords internal -trans_range <- function(trans, x) { - trans <- as.trans(trans) - range(trans$transform(range(squish(x, trans$domain), na.rm = TRUE))) +trans_range <- function(transform, x) { + transform <- as.transform(transform) + range(transform$transform(range(squish(x, transform$domain), na.rm = TRUE))) } diff --git a/README.Rmd b/README.Rmd index 86845388..d63d324e 100644 --- a/README.Rmd +++ b/README.Rmd @@ -99,7 +99,7 @@ transformation functions for repeated use. ```{r transforms} # use trans_new to build a new transformation -logp3_trans <- trans_new( +transform_logp3 <- trans_new( name = "logp", transform = function(x) log(x + 3), inverse = function(x) exp(x) - 3, @@ -109,5 +109,5 @@ logp3_trans <- trans_new( dsamp <- sample_n(diamonds, 100) ggplot(dsamp, aes(carat, price, colour = color)) + geom_point() + - scale_y_continuous(trans = logp3_trans) + scale_y_continuous(trans = transform_logp3) ``` diff --git a/README.md b/README.md index 5a2d861c..18b81eee 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ transformation functions for repeated use. ``` r # use trans_new to build a new transformation -logp3_trans <- trans_new( +transform_logp3 <- trans_new( name = "logp", transform = function(x) log(x + 3), inverse = function(x) exp(x) - 3, @@ -122,7 +122,7 @@ logp3_trans <- trans_new( dsamp <- sample_n(diamonds, 100) ggplot(dsamp, aes(carat, price, colour = color)) + geom_point() + - scale_y_continuous(trans = logp3_trans) + scale_y_continuous(trans = transform_logp3) ``` ![](man/figures/README-transforms-1.png) diff --git a/man/cscale.Rd b/man/cscale.Rd index 1c65f54d..f535dce1 100644 --- a/man/cscale.Rd +++ b/man/cscale.Rd @@ -4,7 +4,7 @@ \alias{cscale} \title{Continuous scale} \usage{ -cscale(x, palette, na.value = NA_real_, trans = identity_trans()) +cscale(x, palette, na.value = NA_real_, trans = transform_identity()) } \arguments{ \item{x}{vector of continuous values to scale} @@ -21,7 +21,7 @@ raw data prior to scaling. Defaults to the identity transformation which leaves the data unchanged. Built in transformations: -\Sexpr[results=rd,stage=build]{scales:::seealso_trans()}.} +\Sexpr[results=rd,stage=build]{scales:::seealso_transform()}.} } \description{ Continuous scale @@ -29,7 +29,7 @@ Continuous scale \examples{ with(mtcars, plot(disp, mpg, cex = cscale(hp, rescale_pal()))) with(mtcars, plot(disp, mpg, cex = cscale(hp, rescale_pal(), - trans = sqrt_trans() + trans = transform_sqrt() ))) with(mtcars, plot(disp, mpg, cex = cscale(hp, area_pal()))) with(mtcars, plot(disp, mpg, diff --git a/man/label_bytes.Rd b/man/label_bytes.Rd index d25cae27..fd842fdd 100644 --- a/man/label_bytes.Rd +++ b/man/label_bytes.Rd @@ -42,6 +42,10 @@ decimal point.} \itemize{ \item \code{"none"} (the default): no change, e.g. \code{1}. \item \code{"plus"}: preceded by \code{+}, e.g. \code{+1}. +\item \code{"space"}: preceded by a Unicode "figure space", i.e., a space equally +as wide as a number or \code{+}. Compared to \code{"none"}, adding a figure space +can ensure numbers remain properly aligned when they are left- or +right-justified. }} \item{\code{style_negative}}{A string that determines the style of negative numbers: \itemize{ diff --git a/man/label_currency.Rd b/man/label_currency.Rd index 40bead59..0c9f5c2d 100644 --- a/man/label_currency.Rd +++ b/man/label_currency.Rd @@ -44,6 +44,10 @@ width (see \code{\link[base:format]{base::format()}}).} \itemize{ \item \code{"none"} (the default): no change, e.g. \code{1}. \item \code{"plus"}: preceded by \code{+}, e.g. \code{+1}. +\item \code{"space"}: preceded by a Unicode "figure space", i.e., a space equally +as wide as a number or \code{+}. Compared to \code{"none"}, adding a figure space +can ensure numbers remain properly aligned when they are left- or +right-justified. }} \item{\code{style_negative}}{A string that determines the style of negative numbers: \itemize{ diff --git a/man/label_date.Rd b/man/label_date.Rd index 9c510d4a..df3cebda 100644 --- a/man/label_date.Rd +++ b/man/label_date.Rd @@ -57,6 +57,10 @@ decimal point.} \itemize{ \item \code{"none"} (the default): no change, e.g. \code{1}. \item \code{"plus"}: preceded by \code{+}, e.g. \code{+1}. +\item \code{"space"}: preceded by a Unicode "figure space", i.e., a space equally +as wide as a number or \code{+}. Compared to \code{"none"}, adding a figure space +can ensure numbers remain properly aligned when they are left- or +right-justified. }} \item{\code{style_negative}}{A string that determines the style of negative numbers: \itemize{ diff --git a/man/label_number_si.Rd b/man/label_number_si.Rd index 85019af4..fa6a7adb 100644 --- a/man/label_number_si.Rd +++ b/man/label_number_si.Rd @@ -35,6 +35,10 @@ decimal point.} \itemize{ \item \code{"none"} (the default): no change, e.g. \code{1}. \item \code{"plus"}: preceded by \code{+}, e.g. \code{+1}. +\item \code{"space"}: preceded by a Unicode "figure space", i.e., a space equally +as wide as a number or \code{+}. Compared to \code{"none"}, adding a figure space +can ensure numbers remain properly aligned when they are left- or +right-justified. }} \item{\code{style_negative}}{A string that determines the style of negative numbers: \itemize{ diff --git a/man/label_ordinal.Rd b/man/label_ordinal.Rd index 62945380..e6de359c 100644 --- a/man/label_ordinal.Rd +++ b/man/label_ordinal.Rd @@ -47,6 +47,10 @@ decimal point.} \itemize{ \item \code{"none"} (the default): no change, e.g. \code{1}. \item \code{"plus"}: preceded by \code{+}, e.g. \code{+1}. +\item \code{"space"}: preceded by a Unicode "figure space", i.e., a space equally +as wide as a number or \code{+}. Compared to \code{"none"}, adding a figure space +can ensure numbers remain properly aligned when they are left- or +right-justified. }} \item{\code{style_negative}}{A string that determines the style of negative numbers: \itemize{ diff --git a/man/label_percent.Rd b/man/label_percent.Rd index ffef1e54..993e121d 100644 --- a/man/label_percent.Rd +++ b/man/label_percent.Rd @@ -48,6 +48,10 @@ width (see \code{\link[base:format]{base::format()}}).} \itemize{ \item \code{"none"} (the default): no change, e.g. \code{1}. \item \code{"plus"}: preceded by \code{+}, e.g. \code{+1}. +\item \code{"space"}: preceded by a Unicode "figure space", i.e., a space equally +as wide as a number or \code{+}. Compared to \code{"none"}, adding a figure space +can ensure numbers remain properly aligned when they are left- or +right-justified. }} \item{\code{style_negative}}{A string that determines the style of negative numbers: \itemize{ diff --git a/man/log_trans.Rd b/man/log_trans.Rd deleted file mode 100644 index 2e6619c6..00000000 --- a/man/log_trans.Rd +++ /dev/null @@ -1,48 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/trans-numeric.R -\name{log_trans} -\alias{log_trans} -\alias{log10_trans} -\alias{log2_trans} -\alias{log1p_trans} -\alias{pseudo_log_trans} -\title{Log transformations} -\usage{ -log_trans(base = exp(1)) - -log10_trans() - -log2_trans() - -log1p_trans() - -pseudo_log_trans(sigma = 1, base = exp(1)) -} -\arguments{ -\item{base}{base of logarithm} - -\item{sigma}{Scaling factor for the linear part of pseudo-log transformation.} -} -\description{ -\itemize{ -\item \code{log_trans()}: \code{log(x)} -\item \code{log1p()}: \code{log(x + 1)} -\item \code{pseudo_log_trans()}: smoothly transition to linear scale around 0. -} -} -\examples{ -plot(log2_trans(), xlim = c(0, 5)) -plot(log_trans(), xlim = c(0, 5)) -plot(log10_trans(), xlim = c(0, 5)) - -plot(log_trans(), xlim = c(0, 2)) -plot(log1p_trans(), xlim = c(-1, 1)) - -# The pseudo-log is defined for all real numbers -plot(pseudo_log_trans(), xlim = c(-5, 5)) -lines(log_trans(), xlim = c(0, 5), col = "red") - -# For large positives nubmers it's very close to log -plot(pseudo_log_trans(), xlim = c(1, 20)) -lines(log_trans(), xlim = c(1, 20), col = "red") -} diff --git a/man/trans_new.Rd b/man/trans_new.Rd index 64ae23ad..7c244e41 100644 --- a/man/trans_new.Rd +++ b/man/trans_new.Rd @@ -1,9 +1,11 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/trans.R +% Please edit documentation in R/transform.R \name{trans_new} \alias{trans_new} \alias{trans} +\alias{is.transform} \alias{is.trans} +\alias{as.transform} \alias{as.trans} \title{Create a new transformation object} \usage{ @@ -19,8 +21,12 @@ trans_new( domain = c(-Inf, Inf) ) +is.transform(x) + is.trans(x) +as.transform(x, arg = deparse(substitute(x))) + as.trans(x, arg = deparse(substitute(x))) } \arguments{ @@ -59,6 +65,6 @@ well-formatted labels. Transformations may also include the derivatives of the transformation and its inverse, but are not required to. } \seealso{ -\Sexpr[results=rd,stage=build]{scales:::seealso_trans()} +\Sexpr[results=rd,stage=build]{scales:::seealso_transform()} } \keyword{internal} diff --git a/man/trans_range.Rd b/man/trans_range.Rd index 14363080..2c988c99 100644 --- a/man/trans_range.Rd +++ b/man/trans_range.Rd @@ -1,18 +1,18 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/trans.R +% Please edit documentation in R/transform.R \name{trans_range} \alias{trans_range} \title{Compute range of transformed values} \usage{ -trans_range(trans, x) +trans_range(transform, x) } \arguments{ -\item{trans}{a transformation object, or the name of a transformation object +\item{transform}{a transformation object, or the name of a transformation object given as a string.} \item{x}{a numeric vector to compute the range of} } \description{ -Silently drops any ranges outside of the domain of \code{trans}. +Silently drops any ranges outside of the domain of \code{transform}. } \keyword{internal} diff --git a/man/asinh_trans.Rd b/man/transform_asinh.Rd similarity index 55% rename from man/asinh_trans.Rd rename to man/transform_asinh.Rd index 10b4e8be..84fd3cae 100644 --- a/man/asinh_trans.Rd +++ b/man/transform_asinh.Rd @@ -1,14 +1,17 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/trans-numeric.R -\name{asinh_trans} +% Please edit documentation in R/transform-numeric.R +\name{transform_asinh} +\alias{transform_asinh} \alias{asinh_trans} \title{Inverse Hyperbolic Sine transformation} \usage{ +transform_asinh() + asinh_trans() } \description{ Inverse Hyperbolic Sine transformation } \examples{ -plot(asinh_trans(), xlim = c(-1e2, 1e2)) +plot(transform_asinh(), xlim = c(-1e2, 1e2)) } diff --git a/man/asn_trans.Rd b/man/transform_asn.Rd similarity index 60% rename from man/asn_trans.Rd rename to man/transform_asn.Rd index 8785c6b0..a08d97d4 100644 --- a/man/asn_trans.Rd +++ b/man/transform_asn.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/trans-numeric.R -\name{asn_trans} +% Please edit documentation in R/transform-numeric.R +\name{transform_asn} +\alias{transform_asn} \alias{asn_trans} \title{Arc-sin square root transformation} \usage{ +transform_asn() + asn_trans() } \description{ @@ -11,5 +14,5 @@ This is the variance stabilising transformation for the binomial distribution. } \examples{ -plot(asn_trans(), xlim = c(0, 1)) +plot(transform_asn(), xlim = c(0, 1)) } diff --git a/man/atanh_trans.Rd b/man/transform_atanh.Rd similarity index 52% rename from man/atanh_trans.Rd rename to man/transform_atanh.Rd index a0b9b78c..6aa99466 100644 --- a/man/atanh_trans.Rd +++ b/man/transform_atanh.Rd @@ -1,14 +1,17 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/trans-numeric.R -\name{atanh_trans} +% Please edit documentation in R/transform-numeric.R +\name{transform_atanh} +\alias{transform_atanh} \alias{atanh_trans} \title{Arc-tangent transformation} \usage{ +transform_atanh() + atanh_trans() } \description{ Arc-tangent transformation } \examples{ -plot(atanh_trans(), xlim = c(-1, 1)) +plot(transform_atanh(), xlim = c(-1, 1)) } diff --git a/man/boxcox_trans.Rd b/man/transform_boxcox.Rd similarity index 70% rename from man/boxcox_trans.Rd rename to man/transform_boxcox.Rd index eed1d071..cc6d8ea5 100644 --- a/man/boxcox_trans.Rd +++ b/man/transform_boxcox.Rd @@ -1,19 +1,25 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/trans-numeric.R -\name{boxcox_trans} +% Please edit documentation in R/transform-numeric.R +\name{transform_boxcox} +\alias{transform_boxcox} \alias{boxcox_trans} +\alias{transform_modulus} \alias{modulus_trans} \title{Box-Cox & modulus transformations} \usage{ +transform_boxcox(p, offset = 0) + boxcox_trans(p, offset = 0) +transform_modulus(p, offset = 1) + modulus_trans(p, offset = 1) } \arguments{ \item{p}{Transformation exponent, \eqn{\lambda}.} \item{offset}{Constant offset. 0 for Box-Cox type 1, -otherwise any non-negative constant (Box-Cox type 2). \code{modulus_trans()} +otherwise any non-negative constant (Box-Cox type 2). \code{transform_modulus()} sets the default to 1.} } \description{ @@ -36,15 +42,15 @@ and when \code{y = 0}: \deqn{y^{(\lambda)} = sign(y) * \ln(|y| + 1)}{ y^(\lambda) = sign(y) * ln(|y| + 1)} } \examples{ -plot(boxcox_trans(-1), xlim = c(0, 10)) -plot(boxcox_trans(0), xlim = c(0, 10)) -plot(boxcox_trans(1), xlim = c(0, 10)) -plot(boxcox_trans(2), xlim = c(0, 10)) +plot(transform_boxcox(-1), xlim = c(0, 10)) +plot(transform_boxcox(0), xlim = c(0, 10)) +plot(transform_boxcox(1), xlim = c(0, 10)) +plot(transform_boxcox(2), xlim = c(0, 10)) -plot(modulus_trans(-1), xlim = c(-10, 10)) -plot(modulus_trans(0), xlim = c(-10, 10)) -plot(modulus_trans(1), xlim = c(-10, 10)) -plot(modulus_trans(2), xlim = c(-10, 10)) +plot(transform_modulus(-1), xlim = c(-10, 10)) +plot(transform_modulus(0), xlim = c(-10, 10)) +plot(transform_modulus(1), xlim = c(-10, 10)) +plot(transform_modulus(2), xlim = c(-10, 10)) } \references{ Box, G. E., & Cox, D. R. (1964). An analysis of transformations. @@ -56,5 +62,5 @@ An alternative family of transformations. Applied Statistics, 190-197. \url{https://www.jstor.org/stable/2986305} } \seealso{ -\code{\link[=yj_trans]{yj_trans()}} +\code{\link[=transform_yj]{transform_yj()}} } diff --git a/man/compose_trans.Rd b/man/transform_compose.Rd similarity index 82% rename from man/compose_trans.Rd rename to man/transform_compose.Rd index 4af20e58..84dc4ad6 100644 --- a/man/compose_trans.Rd +++ b/man/transform_compose.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/trans-compose.R -\name{compose_trans} +% Please edit documentation in R/transform-compose.R +\name{transform_compose} +\alias{transform_compose} \alias{compose_trans} \title{Compose two or more transformations together} \usage{ +transform_compose(...) + compose_trans(...) } \arguments{ diff --git a/man/date_trans.Rd b/man/transform_date.Rd similarity index 71% rename from man/date_trans.Rd rename to man/transform_date.Rd index 6c110199..85b700f9 100644 --- a/man/date_trans.Rd +++ b/man/transform_date.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/trans-date.R -\name{date_trans} +% Please edit documentation in R/transform-date.R +\name{transform_date} +\alias{transform_date} \alias{date_trans} \title{Transformation for dates (class Date)} \usage{ +transform_date() + date_trans() } \description{ @@ -11,7 +14,7 @@ Transformation for dates (class Date) } \examples{ years <- seq(as.Date("1910/1/1"), as.Date("1999/1/1"), "years") -t <- date_trans() +t <- transform_date() t$transform(years) t$inverse(t$transform(years)) t$format(t$breaks(range(years))) diff --git a/man/exp_trans.Rd b/man/transform_exp.Rd similarity index 50% rename from man/exp_trans.Rd rename to man/transform_exp.Rd index 92f5b9e5..b1addd90 100644 --- a/man/exp_trans.Rd +++ b/man/transform_exp.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/trans-numeric.R -\name{exp_trans} +% Please edit documentation in R/transform-numeric.R +\name{transform_exp} +\alias{transform_exp} \alias{exp_trans} \title{Exponential transformation (inverse of log transformation)} \usage{ +transform_exp(base = exp(1)) + exp_trans(base = exp(1)) } \arguments{ @@ -13,8 +16,8 @@ exp_trans(base = exp(1)) Exponential transformation (inverse of log transformation) } \examples{ -plot(exp_trans(0.5), xlim = c(-2, 2)) -plot(exp_trans(1), xlim = c(-2, 2)) -plot(exp_trans(2), xlim = c(-2, 2)) -plot(exp_trans(), xlim = c(-2, 2)) +plot(transform_exp(0.5), xlim = c(-2, 2)) +plot(transform_exp(1), xlim = c(-2, 2)) +plot(transform_exp(2), xlim = c(-2, 2)) +plot(transform_exp(), xlim = c(-2, 2)) } diff --git a/man/identity_trans.Rd b/man/transform_identity.Rd similarity index 54% rename from man/identity_trans.Rd rename to man/transform_identity.Rd index efb88769..f5a22f5b 100644 --- a/man/identity_trans.Rd +++ b/man/transform_identity.Rd @@ -1,14 +1,17 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/trans-numeric.R -\name{identity_trans} +% Please edit documentation in R/transform-numeric.R +\name{transform_identity} +\alias{transform_identity} \alias{identity_trans} \title{Identity transformation (do nothing)} \usage{ +transform_identity() + identity_trans() } \description{ Identity transformation (do nothing) } \examples{ -plot(identity_trans(), xlim = c(-1, 1)) +plot(transform_identity(), xlim = c(-1, 1)) } diff --git a/man/transform_log.Rd b/man/transform_log.Rd new file mode 100644 index 00000000..ad0a18e1 --- /dev/null +++ b/man/transform_log.Rd @@ -0,0 +1,63 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/transform-numeric.R +\name{transform_log} +\alias{transform_log} +\alias{transform_log10} +\alias{transform_log2} +\alias{transform_log1p} +\alias{log_trans} +\alias{log10_trans} +\alias{log2_trans} +\alias{log1p_trans} +\alias{transform_pseudo_log} +\alias{pseudo_log_trans} +\title{Log transformations} +\usage{ +transform_log(base = exp(1)) + +transform_log10() + +transform_log2() + +transform_log1p() + +log_trans(base = exp(1)) + +log10_trans() + +log2_trans() + +log1p_trans() + +transform_pseudo_log(sigma = 1, base = exp(1)) + +pseudo_log_trans(sigma = 1, base = exp(1)) +} +\arguments{ +\item{base}{base of logarithm} + +\item{sigma}{Scaling factor for the linear part of pseudo-log transformation.} +} +\description{ +\itemize{ +\item \code{transform_log()}: \code{log(x)} +\item \code{log1p()}: \code{log(x + 1)} +\item \code{transform_pseudo_log()}: smoothly transition to linear scale around 0. +} +} +\examples{ +plot(transform_log2(), xlim = c(0, 5)) +plot(transform_log(), xlim = c(0, 5)) +plot(transform_log10(), xlim = c(0, 5)) + +plot(transform_log(), xlim = c(0, 2)) +plot(transform_log1p(), xlim = c(-1, 1)) + +# The pseudo-log is defined for all real numbers +plot(transform_pseudo_log(), xlim = c(-5, 5)) +lines(transform_log(), xlim = c(0, 5), col = "red") + +# For large positives numbers it's very close to log +plot(transform_pseudo_log(), xlim = c(1, 20)) +lines(transform_log(), xlim = c(1, 20), col = "red") +} diff --git a/man/probability_trans.Rd b/man/transform_probability.Rd similarity index 66% rename from man/probability_trans.Rd rename to man/transform_probability.Rd index e0a4e82d..a1711017 100644 --- a/man/probability_trans.Rd +++ b/man/transform_probability.Rd @@ -1,11 +1,20 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/trans-numeric.R -\name{probability_trans} +% Please edit documentation in R/transform-numeric.R +\name{transform_probability} +\alias{transform_probability} +\alias{transform_logit} +\alias{transform_probit} \alias{probability_trans} \alias{logit_trans} \alias{probit_trans} \title{Probability transformation} \usage{ +transform_probability(distribution, ...) + +transform_logit() + +transform_probit() + probability_trans(distribution, ...) logit_trans() @@ -24,6 +33,6 @@ function, "q" + distribution is a valid quantile function, and Probability transformation } \examples{ -plot(logit_trans(), xlim = c(0, 1)) -plot(probit_trans(), xlim = c(0, 1)) +plot(transform_logit(), xlim = c(0, 1)) +plot(transform_probit(), xlim = c(0, 1)) } diff --git a/man/reciprocal_trans.Rd b/man/transform_reciprocal.Rd similarity index 51% rename from man/reciprocal_trans.Rd rename to man/transform_reciprocal.Rd index aec21d60..9de217b7 100644 --- a/man/reciprocal_trans.Rd +++ b/man/transform_reciprocal.Rd @@ -1,14 +1,17 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/trans-numeric.R -\name{reciprocal_trans} +% Please edit documentation in R/transform-numeric.R +\name{transform_reciprocal} +\alias{transform_reciprocal} \alias{reciprocal_trans} \title{Reciprocal transformation} \usage{ +transform_reciprocal() + reciprocal_trans() } \description{ Reciprocal transformation } \examples{ -plot(reciprocal_trans(), xlim = c(0, 1)) +plot(transform_reciprocal(), xlim = c(0, 1)) } diff --git a/man/reverse_trans.Rd b/man/transform_reverse.Rd similarity index 69% rename from man/reverse_trans.Rd rename to man/transform_reverse.Rd index 049f5499..8971ec5b 100644 --- a/man/reverse_trans.Rd +++ b/man/transform_reverse.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/trans-numeric.R -\name{reverse_trans} +% Please edit documentation in R/transform-numeric.R +\name{transform_reverse} +\alias{transform_reverse} \alias{reverse_trans} \title{Reverse transformation} \usage{ +transform_reverse() + reverse_trans() } \description{ @@ -12,5 +15,5 @@ that reverse transformation cannot easily be composed with transformations that require positive input unless the reversing is done as a final step. } \examples{ -plot(reverse_trans(), xlim = c(-1, 1)) +plot(transform_reverse(), xlim = c(-1, 1)) } diff --git a/man/sqrt_trans.Rd b/man/transform_sqrt.Rd similarity index 59% rename from man/sqrt_trans.Rd rename to man/transform_sqrt.Rd index f2695747..2478faf3 100644 --- a/man/sqrt_trans.Rd +++ b/man/transform_sqrt.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/trans-numeric.R -\name{sqrt_trans} +% Please edit documentation in R/transform-numeric.R +\name{transform_sqrt} +\alias{transform_sqrt} \alias{sqrt_trans} \title{Square-root transformation} \usage{ +transform_sqrt() + sqrt_trans() } \description{ @@ -11,5 +14,5 @@ This is the variance stabilising transformation for the Poisson distribution. } \examples{ -plot(sqrt_trans(), xlim = c(0, 5)) +plot(transform_sqrt(), xlim = c(0, 5)) } diff --git a/man/time_trans.Rd b/man/transform_time.Rd similarity index 78% rename from man/time_trans.Rd rename to man/transform_time.Rd index 8b473327..b82c12d0 100644 --- a/man/time_trans.Rd +++ b/man/transform_time.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/trans-date.R -\name{time_trans} +% Please edit documentation in R/transform-date.R +\name{transform_time} +\alias{transform_time} \alias{time_trans} \title{Transformation for date-times (class POSIXt)} \usage{ +transform_time(tz = NULL) + time_trans(tz = NULL) } \arguments{ @@ -15,7 +18,7 @@ Transformation for date-times (class POSIXt) } \examples{ hours <- seq(ISOdate(2000, 3, 20, tz = ""), by = "hour", length.out = 10) -t <- time_trans() +t <- transform_time() t$transform(hours) t$inverse(t$transform(hours)) t$format(t$breaks(range(hours))) diff --git a/man/timespan_trans.Rd b/man/transform_timespan.Rd similarity index 53% rename from man/timespan_trans.Rd rename to man/transform_timespan.Rd index 6ead6f0d..18eb8bae 100644 --- a/man/timespan_trans.Rd +++ b/man/transform_timespan.Rd @@ -1,35 +1,41 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/trans-date.R -\name{timespan_trans} +% Please edit documentation in R/transform-date.R +\name{transform_timespan} +\alias{transform_timespan} \alias{timespan_trans} +\alias{transform_hms} \alias{hms_trans} \title{Transformation for times (class hms)} \usage{ +transform_timespan(unit = c("secs", "mins", "hours", "days", "weeks")) + timespan_trans(unit = c("secs", "mins", "hours", "days", "weeks")) +transform_hms() + hms_trans() } \arguments{ \item{unit}{The unit used to interpret numeric input} } \description{ -\code{timespan_trans()} provides transformations for data encoding time passed +\code{transform_timespan()} provides transformations for data encoding time passed along with breaks and label formatting showing standard unit of time fitting -the range of the data. \code{hms_trans()} provides the same but using standard hms -idioms and formatting. +the range of the data. \code{transform_hms()} provides the same but using standard +hms idioms and formatting. } \examples{ -# timespan_trans allows you to specify the time unit numeric data is +# transform_timespan allows you to specify the time unit numeric data is # interpreted in -min_trans <- timespan_trans("mins") -demo_timespan(seq(0, 100), trans = min_trans) +trans_min <- transform_timespan("mins") +demo_timespan(seq(0, 100), trans = trans_min) # Input already in difftime format is interpreted correctly -demo_timespan(as.difftime(seq(0, 100), units = "secs"), trans = min_trans) +demo_timespan(as.difftime(seq(0, 100), units = "secs"), trans = trans_min) if (require("hms")) { - # hms_trans always assumes seconds + # transform_hms always assumes seconds hms <- round(runif(10) * 86400) - t <- hms_trans() + t <- transform_hms() t$transform(hms) t$inverse(t$transform(hms)) t$breaks(hms) diff --git a/man/yj_trans.Rd b/man/transform_yj.Rd similarity index 73% rename from man/yj_trans.Rd rename to man/transform_yj.Rd index f129e416..0eae8a30 100644 --- a/man/yj_trans.Rd +++ b/man/transform_yj.Rd @@ -1,18 +1,21 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/trans-numeric.R -\name{yj_trans} +% Please edit documentation in R/transform-numeric.R +\name{transform_yj} +\alias{transform_yj} \alias{yj_trans} \title{Yeo-Johnson transformation} \usage{ +transform_yj(p) + yj_trans(p) } \arguments{ \item{p}{Transformation exponent, \eqn{\lambda}.} } \description{ -The Yeo-Johnson transformation is a flexible transformation that is similiar -to Box-Cox, \code{\link[=boxcox_trans]{boxcox_trans()}}, but does not require input values to be greater -than zero. +The Yeo-Johnson transformation is a flexible transformation that is similar +to Box-Cox, \code{\link[=transform_boxcox]{transform_boxcox()}}, but does not require input values to be +greater than zero. } \details{ The transformation takes one of four forms depending on the values of \code{y} and \eqn{\lambda}. @@ -28,10 +31,10 @@ The transformation takes one of four forms depending on the values of \code{y} a } } \examples{ -plot(yj_trans(-1), xlim = c(-10, 10)) -plot(yj_trans(0), xlim = c(-10, 10)) -plot(yj_trans(1), xlim = c(-10, 10)) -plot(yj_trans(2), xlim = c(-10, 10)) +plot(transform_yj(-1), xlim = c(-10, 10)) +plot(transform_yj(0), xlim = c(-10, 10)) +plot(transform_yj(1), xlim = c(-10, 10)) +plot(transform_yj(2), xlim = c(-10, 10)) } \references{ Yeo, I., & Johnson, R. (2000). diff --git a/tests/testthat/_snaps/trans-compose.md b/tests/testthat/_snaps/trans-compose.md index 6eafb963..c2f772a1 100755 --- a/tests/testthat/_snaps/trans-compose.md +++ b/tests/testthat/_snaps/trans-compose.md @@ -1,13 +1,13 @@ # produces informative errors Code - compose_trans() + transform_compose() Condition - Error in `compose_trans()`: - ! `compose_trans()` must include at least 1 transformer to compose + Error in `transform_compose()`: + ! `transform_compose()` must include at least 1 transformer to compose Code - compose_trans("sqrt", "reverse", "log10") + transform_compose("sqrt", "reverse", "log10") Condition - Error in `compose_trans()`: + Error in `transform_compose()`: ! Sequence of transformations yields invalid domain diff --git a/tests/testthat/_snaps/trans-date.md b/tests/testthat/_snaps/trans-date.md index ff41bec3..bb644fbf 100644 --- a/tests/testthat/_snaps/trans-date.md +++ b/tests/testthat/_snaps/trans-date.md @@ -1,8 +1,8 @@ # date/time scales raise error on incorrect inputs - `time_trans()` works with objects of class only + `transform_time()` works with objects of class only --- - `date_trans()` works with objects of class only + `transform_date()` works with objects of class only diff --git a/tests/testthat/_snaps/trans.md b/tests/testthat/_snaps/trans.md index e5b33823..0fd5a98a 100644 --- a/tests/testthat/_snaps/trans.md +++ b/tests/testthat/_snaps/trans.md @@ -1,12 +1,12 @@ -# as.trans generates informative error +# as.transform generates informative error Code - as.trans(1) + as.transform(1) Condition - Error in `as.trans()`: + Error in `as.transform()`: ! `1` must be a character vector or a transformer object Code - as.trans("x") + as.transform("x") Condition Error in `get()`: ! object 'x_trans' of mode 'function' was not found diff --git a/tests/testthat/test-trans-compose.R b/tests/testthat/test-trans-compose.R index 314b735e..c11f07f5 100644 --- a/tests/testthat/test-trans-compose.R +++ b/tests/testthat/test-trans-compose.R @@ -1,38 +1,38 @@ test_that("composes transforms correctly", { - t <- compose_trans("log10", "reverse") + t <- transform_compose("log10", "reverse") expect_equal(t$transform(100), -2) expect_equal(t$inverse(-2), 100) }) test_that("composes derivatives correctly", { - t <- compose_trans("sqrt", "reciprocal", "reverse") + t <- transform_compose("sqrt", "reciprocal", "reverse") expect_equal(t$d_transform(0.25), 4) expect_equal(t$d_inverse(-2), 0.25) }) test_that("produces NULL derivatives if not all transforms have derivatives", { - t <- compose_trans("sqrt", trans_new("no_deriv", identity, identity)) + t <- transform_compose("sqrt", trans_new("no_deriv", identity, identity)) expect_null(t$d_transform) expect_null(t$d_inverse) }) test_that("uses breaks from first transformer", { - t <- compose_trans("log10", "reverse") + t <- transform_compose("log10", "reverse") expect_equal(t$breaks(c(1, 1000)), log_breaks()(c(1, 1000))) }) test_that("produces informative errors", { expect_snapshot(error = TRUE, { - compose_trans() - compose_trans("sqrt", "reverse", "log10") + transform_compose() + transform_compose("sqrt", "reverse", "log10") }) }) test_that("produces correct domains", { - expect_equal(compose_trans("sqrt", "reverse")$domain, c(0, Inf)) - expect_equal(compose_trans("sqrt", "log")$domain, c(0, Inf)) - expect_equal(compose_trans("log", "log")$domain, c(1, Inf)) - expect_equal(compose_trans("reverse", "log")$domain, c(-Inf, 0)) - expect_equal(compose_trans("reverse", "logit", "log")$domain, c(-1, -0.5)) - expect_error(compose_trans("sqrt", "reverse", "log")$domain, "invalid domain") + expect_equal(transform_compose("sqrt", "reverse")$domain, c(0, Inf)) + expect_equal(transform_compose("sqrt", "log")$domain, c(0, Inf)) + expect_equal(transform_compose("log", "log")$domain, c(1, Inf)) + expect_equal(transform_compose("reverse", "log")$domain, c(-Inf, 0)) + expect_equal(transform_compose("reverse", "logit", "log")$domain, c(-1, -0.5)) + expect_error(transform_compose("sqrt", "reverse", "log")$domain, "invalid domain") }) diff --git a/tests/testthat/test-trans-date.R b/tests/testthat/test-trans-date.R index 38092367..5516ed57 100644 --- a/tests/testthat/test-trans-date.R +++ b/tests/testthat/test-trans-date.R @@ -9,22 +9,22 @@ with_tz <- function(x, value) { } test_that("date/time scales raise error on incorrect inputs", { - time <- time_trans() + time <- transform_time() expect_snapshot_error(time$transform(a_date)) - date <- date_trans() + date <- transform_date() expect_snapshot_error(date$transform(a_time)) }) test_that("time scales learn timezones", { skip_if_not(getRversion() > "3.3.3") - time <- time_trans() + time <- transform_time() x <- time$inverse(time$transform(a_time)) expect_equal(tz(x), "UTC") expect_equal(tz2(x), "UTC") - time <- time_trans() + time <- transform_time() x <- time$inverse(time$transform(with_tz(a_time, "GMT"))) expect_equal(tz(x), "GMT") @@ -32,7 +32,7 @@ test_that("time scales learn timezones", { }) test_that("tz arugment overrules default time zone", { - time <- time_trans("GMT") + time <- transform_time("GMT") x <- time$inverse(time$transform(a_time)) expect_equal(tz(x), "GMT") @@ -59,9 +59,9 @@ test_that("date_breaks() works", { }) test_that("can invert domain", { - t <- date_trans() + t <- transform_date() expect_equal(t$transform(t$domain), c(-Inf, Inf)) - t <- time_trans() + t <- transform_time() expect_equal(t$transform(t$domain), c(-Inf, Inf)) }) diff --git a/tests/testthat/test-trans-numeric.R b/tests/testthat/test-trans-numeric.R index a7f5cbf8..ff3523bc 100644 --- a/tests/testthat/test-trans-numeric.R +++ b/tests/testthat/test-trans-numeric.R @@ -1,5 +1,5 @@ test_that("Pseudo-log is invertible", { - trans <- pseudo_log_trans() + trans <- transform_pseudo_log() expect_equal( trans$inverse(trans$transform(-10:10)), -10:10 @@ -7,63 +7,63 @@ test_that("Pseudo-log is invertible", { }) test_that("Modulus is invertible for negative and positive numbers", { - trans <- modulus_trans(p = .1) + trans <- transform_modulus(p = .1) expect_equal(trans$inv(trans$trans(-10:10)), -10:10) - trans <- modulus_trans(p = -2) + trans <- transform_modulus(p = -2) expect_equal(trans$inv(trans$trans(-10:10)), -10:10) - trans <- modulus_trans(p = 1) + trans <- transform_modulus(p = 1) expect_equal(trans$inv(trans$trans(-10:10)), -10:10) }) test_that("Boxcox gives error for negative values", { - trans <- boxcox_trans(p = .1) + trans <- transform_boxcox(p = .1) expect_error(trans$trans(-10:10)) - trans <- boxcox_trans(p = -2) + trans <- transform_boxcox(p = -2) expect_error(trans$trans(-10:10)) }) test_that("Boxcox can handle NA values", { - trans <- boxcox_trans(p = 0) + trans <- transform_boxcox(p = 0) expect_equal(trans$trans(c(1, NA_real_)), c(0, NA_real_)) }) test_that("Boxcox is invertible", { - trans <- boxcox_trans(p = .1) + trans <- transform_boxcox(p = .1) expect_equal(trans$inv(trans$trans(0:10)), 0:10) - trans <- boxcox_trans(p = -2) + trans <- transform_boxcox(p = -2) expect_equal(trans$inv(trans$trans(0:10)), 0:10) - trans <- boxcox_trans(p = 1) + trans <- transform_boxcox(p = 1) expect_equal(trans$inv(trans$trans(0:10)), 0:10) }) test_that("Yeo-Johnson is invertible", { x <- c(-12345, 12345, -0.12345, 0.12345, -10:10) - trans <- yj_trans(p = -1.5) + trans <- transform_yj(p = -1.5) expect_equal(trans$inverse(trans$transform(x)), x) - trans <- yj_trans(p = 0) + trans <- transform_yj(p = 0) expect_equal(trans$inverse(trans$transform(x)), x) - trans <- yj_trans(p = 0.7) + trans <- transform_yj(p = 0.7) expect_equal(trans$inverse(trans$transform(x)), x) - trans <- yj_trans(p = 1.5) + trans <- transform_yj(p = 1.5) expect_equal(trans$inverse(trans$transform(x)), x) - trans <- yj_trans(p = 2) + trans <- transform_yj(p = 2) expect_equal(trans$inverse(trans$transform(x)), x) }) test_that("Yeo-Johnson is identity function for p = 1", { x <- c(-12345, 12345, -0.12345, 0.12345, -10:10) - trans <- yj_trans(p = 1) + trans <- transform_yj(p = 1) expect_equal(trans$transform(x), x) }) test_that("Yeo-Johnson transforms NAs to NAs without error", { x <- c(1, 2, NA, 4) - trans <- yj_trans(p = 1) + trans <- transform_yj(p = 1) expect_equal(trans$transform(x), x) }) @@ -112,20 +112,20 @@ test_that("Yeo-Johnson transform works", { ) ) - expect_equal(yj_trans(lambdas[1])$transform(x[[1]]), expected_data[[1]]) - expect_equal(yj_trans(lambdas[2])$transform(x[[2]]), expected_data[[2]]) - expect_equal(yj_trans(lambdas[3])$transform(x[[3]]), expected_data[[3]]) + expect_equal(transform_yj(lambdas[1])$transform(x[[1]]), expected_data[[1]]) + expect_equal(transform_yj(lambdas[2])$transform(x[[2]]), expected_data[[2]]) + expect_equal(transform_yj(lambdas[3])$transform(x[[3]]), expected_data[[3]]) }) test_that("probability transforms have domain (0,1)", { - expect_equal(logit_trans()$domain, c(0, 1)) - expect_equal(probit_trans()$domain, c(0, 1)) + expect_equal(transform_logit()$domain, c(0, 1)) + expect_equal(transform_probit()$domain, c(0, 1)) }) # Derivatives ------------------------------------------------------------- -test_that("asn_trans derivatives work", { - trans <- asn_trans() +test_that("transform_asn derivatives work", { + trans <- transform_asn() expect_equal(trans$d_transform(c(0, 0.5, 1)), c(Inf, 2, Inf)) expect_equal(trans$d_inverse(c(0, pi/2, pi)), c(0, 0.5, 0)) x <- seq(0.1, 0.9, length.out = 10) @@ -133,8 +133,8 @@ test_that("asn_trans derivatives work", { expect_equal(trans$d_inverse(x), 1 / trans$d_transform(trans$inverse(x))) }) -test_that("atanh_trans derivatives work", { - trans <- atanh_trans() +test_that("transform_atanh derivatives work", { + trans <- transform_atanh() expect_equal(trans$d_transform(c(-1, 0, 1)), c(Inf, 1, Inf)) expect_equal(trans$d_inverse(c(-log(2), 0, log(2))), c(0.64, 1, 0.64)) x <- seq(-0.9, 0.9, length.out = 10) @@ -142,8 +142,8 @@ test_that("atanh_trans derivatives work", { expect_equal(trans$d_inverse(x), 1 / trans$d_transform(trans$inverse(x))) }) -test_that("asinh_trans derivatives work", { - trans <- asinh_trans() +test_that("transform_asinh derivatives work", { + trans <- transform_asinh() expect_equal(trans$d_transform(c(-1, 0, 1)), c(sqrt(2) / 2, 1, sqrt(2) / 2)) expect_equal(trans$d_inverse(c(-log(2), 0, log(2))), c(1.25, 1, 1.25)) x <- seq(-0.9, 0.9, length.out = 10) @@ -151,53 +151,53 @@ test_that("asinh_trans derivatives work", { expect_equal(trans$d_inverse(x), 1 / trans$d_transform(trans$inverse(x))) }) -test_that("boxcox_trans derivatives work", { - trans <- boxcox_trans(p = 0, offset = 1) +test_that("transform_boxcox derivatives work", { + trans <- transform_boxcox(p = 0, offset = 1) expect_equal(trans$d_transform(c(0, 1, 2)), c(1, 1/2, 1/3)) expect_equal(trans$d_inverse(c(0, 1, 2)), exp(c(0, 1, 2))) x <- 0:10 expect_equal(trans$d_transform(x), 1 / trans$d_inverse(trans$transform(x))) expect_equal(trans$d_inverse(x), 1 / trans$d_transform(trans$inverse(x))) - trans <- boxcox_trans(p = 2, offset = 2) + trans <- transform_boxcox(p = 2, offset = 2) expect_equal(trans$d_transform(c(0, 1, 2)), c(2, 3, 4)) expect_equal(trans$d_inverse(c(0, 0.5, 4)), c(1, sqrt(2) / 2, 1/3)) expect_equal(trans$d_transform(x), 1 / trans$d_inverse(trans$transform(x))) expect_equal(trans$d_inverse(x), 1 / trans$d_transform(trans$inverse(x))) }) -test_that("modulus_trans derivatives work", { - trans <- modulus_trans(p = 0, offset = 1) +test_that("transform_modulus derivatives work", { + trans <- transform_modulus(p = 0, offset = 1) expect_equal(trans$d_transform(c(-2, -1, 1, 2)), c(1/3, 1/2, 1/2, 1/3)) expect_equal(trans$d_inverse(c(-2, -1, 1, 2)), exp(c(2, 1, 1, 2))) x <- c(-10:-2, 2:10) expect_equal(trans$d_transform(x), 1 / trans$d_inverse(trans$transform(x))) expect_equal(trans$d_inverse(x), 1 / trans$d_transform(trans$inverse(x))) - trans <- modulus_trans(p = 2, offset = 2) + trans <- transform_modulus(p = 2, offset = 2) expect_equal(trans$d_transform(c(-2, -1, 1, 2)), c(4, 3, 3, 4)) expect_equal(trans$d_inverse(c(-4, -0.5, 0.5, 4)), c(1/3, sqrt(2) / 2, sqrt(2) / 2, 1/3)) expect_equal(trans$d_transform(x), 1 / trans$d_inverse(trans$transform(x))) expect_equal(trans$d_inverse(x), 1 / trans$d_transform(trans$inverse(x))) }) -test_that("yj_trans derivatives work", { - trans <- yj_trans(p = 0) +test_that("transform_yj derivatives work", { + trans <- transform_yj(p = 0) expect_equal(trans$d_transform(c(-2, -1, 1, 2)), c(3, 2, 0.5, 1/3)) expect_equal(trans$d_inverse(c(-1/2, 1, 2)), c(sqrt(2) / 2, exp(1), exp(2))) x <- c(-10:10) expect_equal(trans$d_transform(x), 1 / trans$d_inverse(trans$transform(x))) expect_equal(trans$d_inverse(x), 1 / trans$d_transform(trans$inverse(x))) - trans <- yj_trans(p = 3) + trans <- transform_yj(p = 3) expect_equal(trans$d_transform(c(-2, -1, 1, 2)), c(1/9, 1/4, 4, 9)) expect_equal(trans$d_inverse(c(-4, -0.5, 1)), c(1/9, 4, (1/16)^(1/3))) expect_equal(trans$d_transform(x), 1 / trans$d_inverse(trans$transform(x))) expect_equal(trans$d_inverse(0:10), 1 / trans$d_transform(trans$inverse(0:10))) }) -test_that("exp_trans derivatives work", { - trans <- exp_trans(10) +test_that("transform_exp derivatives work", { + trans <- transform_exp(10) expect_equal(trans$d_transform(c(0, 1, 2)), c(1, 10, 100) * log(10)) expect_equal(trans$d_inverse(c(0.1, 1, 10) / log(10)), c(10, 1, 0.1)) x <- 1:10 @@ -205,16 +205,16 @@ test_that("exp_trans derivatives work", { expect_equal(trans$d_inverse(x), 1 / trans$d_transform(trans$inverse(x))) }) -test_that("identity_trans derivatives work", { - trans <- identity_trans() +test_that("transform_identity derivatives work", { + trans <- transform_identity() expect_equal(trans$d_transform(numeric(0)), numeric(0)) expect_equal(trans$d_transform(c(0, 1, 2)), c(1, 1, 1)) expect_equal(trans$d_inverse(numeric(0)), numeric(0)) expect_equal(trans$d_inverse(c(0, 1, 2)), c(1, 1, 1)) }) -test_that("log_trans derivatives work", { - trans <- log_trans(10) +test_that("transform_log derivatives work", { + trans <- transform_log(10) expect_equal(trans$d_transform(c(0.1, 1, 10) / log(10)), c(10, 1, 0.1)) expect_equal(trans$d_inverse(c(0, 1, 2)), c(1, 10, 100) * log(10)) x <- 1:10 @@ -222,8 +222,8 @@ test_that("log_trans derivatives work", { expect_equal(trans$d_inverse(x), 1 / trans$d_transform(trans$inverse(x))) }) -test_that("log1p_trans derivatives work", { - trans <- log1p_trans() +test_that("transform_log1p derivatives work", { + trans <- transform_log1p() expect_equal(trans$d_transform(c(0, 1, 2)), c(1, 1/2, 1/3)) expect_equal(trans$d_inverse(c(0, 1, 2)), exp(c(0, 1, 2))) x <- 0:10 @@ -231,8 +231,8 @@ test_that("log1p_trans derivatives work", { expect_equal(trans$d_inverse(x), 1 / trans$d_transform(trans$inverse(x))) }) -test_that("pseudo_log_trans derivatives work", { - trans <- pseudo_log_trans(0.5) +test_that("transform_pseudo_log derivatives work", { + trans <- transform_pseudo_log(0.5) expect_equal(trans$d_transform(c(0, 1)), c(1, sqrt(2) / 2)) expect_equal(trans$d_inverse(c(0, 1)), c(1, cosh(1))) x <- 1:10 @@ -240,8 +240,8 @@ test_that("pseudo_log_trans derivatives work", { expect_equal(trans$d_inverse(x), 1 / trans$d_transform(trans$inverse(x))) }) -test_that("logit_trans derivatives work", { - trans <- logit_trans() +test_that("transform_logit derivatives work", { + trans <- transform_logit() expect_equal(trans$d_transform(c(0.1, 0.5, 0.8)), c(100/9, 4, 6.25)) expect_equal(trans$d_inverse(c(0, 1, 2)), dlogis(c(0, 1, 2))) x <- seq(0.1, 0.9, length.out = 10) @@ -249,8 +249,8 @@ test_that("logit_trans derivatives work", { expect_equal(trans$d_inverse(x), 1 / trans$d_transform(trans$inverse(x))) }) -test_that("reciprocal_trans derivatives work", { - trans <- reciprocal_trans() +test_that("transform_reciprocal derivatives work", { + trans <- transform_reciprocal() expect_equal(trans$d_transform(c(0.1, 1, 10)), c(-100, -1, -0.01)) expect_equal(trans$d_inverse(c(0.1, 1, 10)), c(-100, -1, -0.01)) x <- (1:20)/10 @@ -258,16 +258,16 @@ test_that("reciprocal_trans derivatives work", { expect_equal(trans$d_inverse(x), 1 / trans$d_transform(trans$inverse(x))) }) -test_that("reverse_trans derivatives work", { - trans <- reverse_trans() +test_that("transform_reverse derivatives work", { + trans <- transform_reverse() expect_equal(trans$d_transform(numeric(0)), numeric(0)) expect_equal(trans$d_transform(c(-1, 1, 2)), c(-1, -1, -1)) expect_equal(trans$d_inverse(numeric(0)), numeric(0)) expect_equal(trans$d_inverse(c(-1, 1, 2)), c(-1, -1, -1)) }) -test_that("sqrt_trans derivatives work", { - trans <- sqrt_trans() +test_that("transform_sqrt derivatives work", { + trans <- transform_sqrt() expect_equal(trans$d_transform(c(1, 4, 9)), c(1/2, 1/4, 1/6)) expect_equal(trans$d_inverse(c(1, 2, 3)), c(2, 4, 6)) x <- 1:10 diff --git a/tests/testthat/test-trans.R b/tests/testthat/test-trans.R index 7030d43f..39eba2e5 100644 --- a/tests/testthat/test-trans.R +++ b/tests/testthat/test-trans.R @@ -1,24 +1,24 @@ test_that("Transformed ranges silently drop out-of-domain values", { - r1 <- trans_range(log_trans(), -1:10) + r1 <- trans_range(transform_log(), -1:10) expect_equal(r1, log(c(1e-100, 10))) - r2 <- trans_range(sqrt_trans(), -1:10) + r2 <- trans_range(transform_sqrt(), -1:10) expect_equal(r2, sqrt(c(0, 10))) }) -test_that("as.trans handles character inputs", { - expect_equal(as.trans("log10"), log10_trans()) +test_that("as.transform handles character inputs", { + expect_equal(as.trans("log10"), transform_log10()) expect_equal( - as.trans(c("log10", "reverse")), - compose_trans(log10_trans(), reverse_trans()) + as.transform(c("log10", "reverse")), + transform_compose(transform_log10(), transform_reverse()) ) }) -test_that("as.trans generates informative error", { +test_that("as.transform generates informative error", { expect_snapshot(error = TRUE, { - as.trans(1) - as.trans("x") + as.transform(1) + as.transform("x") }) }) @@ -29,5 +29,5 @@ test_that("trans has useful print method", { }) test_that("inverse of trans_sqrt() returns NA for values outside of range", { - expect_equal(sqrt_trans()$inverse(-2), NA_real_) + expect_equal(transform_sqrt()$inverse(-2), NA_real_) }) From a73a3d4c7931ef8d7faf3c6e2b9e156f5779d772 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 6 Nov 2023 16:07:54 +0100 Subject: [PATCH 2/8] make `as.transform` backward compatible --- R/transform.R | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/R/transform.R b/R/transform.R index 0c978840..9adf546f 100644 --- a/R/transform.R +++ b/R/transform.R @@ -98,11 +98,18 @@ lines.transform <- function(x, ..., xlim) { #' @rdname trans_new #' @export as.transform <- function(x, arg = deparse(substitute(x))) { - if (is.trans(x)) { + if (is.tranform(x)) { x } else if (is.character(x) && length(x) >= 1) { if (length(x) == 1) { - f <- paste0(x, "_trans") + f <- paste0("transform_", x) + # For backward compatibility + if (!exists(f, mode = "function")) { + f2 <- paste0(x, "_trans") + if (exists(f2, mode = "function")) { + f <- f2 + } + } match.fun(f)() } else { transform_compose(!!!x) From 16f4287421e3afa2651a939879576ced0ccc5824 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 6 Nov 2023 16:13:27 +0100 Subject: [PATCH 3/8] Add news bullet, rename functions in news --- NEWS.md | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/NEWS.md b/NEWS.md index 83b6802d..dd1c7d38 100644 --- a/NEWS.md +++ b/NEWS.md @@ -17,19 +17,24 @@ make it easier to align positive and negative values as figure space takes up the same amount of space as `-` (#366) * `label_dollar()` has been superseeded by `label_currency()` for clarity (#344) -* `sqrt_trans()` no longer returns an inverse for values outside of its domain - (#214) +* `transform_sqrt()` no longer returns an inverse for values outside of its + domain (#214) * Add better support for `difftime` objects. `label_timespan()` adds functionality for adding correct unit suffix to timespan data, `breaks_timespan()` adds functionality for finding pleasant breakpoints across - the various bases in time units, while `timespan_trans()` wraps it all - together and provides an alternative to `hms_trans()` (#212) -* Add an inverse (area) hyperbolic sine transformation `asinh_trans()`, which - provides a logarithm-like transformation of a space, but which accommodates - negative values (#297) -* Correct the domain calculation for `compose_trans()` (@mjskay, #408). + the various bases in time units, while `transform_timespan()` wraps it all + together and provides an alternative to `transform_hms()` (#212) +* Add an inverse (area) hyperbolic sine transformation `transform_asinh()`, + which provides a logarithm-like transformation of a space, but which + accommodates negative values (#297) +* Correct the domain calculation for `transform_compose()` (@mjskay, #408). * Transformation objects can optionally include the derivatives of the transform and the inverse transform (@mjskay, #322). +* Transformation function have been renamed to `transform_*`-prefixed names + instead of `*_trans`-suffixed names. This allows for a better tab-completion + search of transformations. The S3 class of transformations has been + renamed from `"trans"` to `"transform"`. + # scales 1.2.1 From fc519016dacad6a8941868a5ed0deed22e0b6931 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 6 Nov 2023 16:22:58 +0100 Subject: [PATCH 4/8] fix typo --- R/transform.R | 2 +- tests/testthat/_snaps/trans.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/transform.R b/R/transform.R index 9adf546f..583e8811 100644 --- a/R/transform.R +++ b/R/transform.R @@ -98,7 +98,7 @@ lines.transform <- function(x, ..., xlim) { #' @rdname trans_new #' @export as.transform <- function(x, arg = deparse(substitute(x))) { - if (is.tranform(x)) { + if (is.transform(x)) { x } else if (is.character(x) && length(x) >= 1) { if (length(x) == 1) { diff --git a/tests/testthat/_snaps/trans.md b/tests/testthat/_snaps/trans.md index 0fd5a98a..ab284e85 100644 --- a/tests/testthat/_snaps/trans.md +++ b/tests/testthat/_snaps/trans.md @@ -9,7 +9,7 @@ as.transform("x") Condition Error in `get()`: - ! object 'x_trans' of mode 'function' was not found + ! object 'transform_x' of mode 'function' was not found # trans has useful print method From 1c528b295b85ebe01e5296c648e7b45d68a6a603 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Tue, 7 Nov 2023 08:18:39 +0100 Subject: [PATCH 5/8] rename `trans_new()` to `new_transform()` --- NAMESPACE | 1 + NEWS.md | 3 ++- R/transform-compose.R | 2 +- R/transform-date.R | 9 ++++---- R/transform-numeric.R | 30 +++++++++++++------------- R/transform.R | 22 +++++++++++-------- README.Rmd | 4 ++-- README.md | 4 ++-- man/{trans_new.Rd => new_transform.Rd} | 17 +++++++++++++-- tests/testthat/_snaps/trans.md | 2 +- tests/testthat/test-trans-compose.R | 2 +- tests/testthat/test-trans.R | 2 +- 12 files changed, 59 insertions(+), 39 deletions(-) rename man/{trans_new.Rd => new_transform.Rd} (89%) diff --git a/NAMESPACE b/NAMESPACE index ccd670b1..d63362c9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -123,6 +123,7 @@ export(minor_breaks_n) export(minor_breaks_width) export(modulus_trans) export(muted) +export(new_transform) export(number) export(number_bytes) export(number_bytes_format) diff --git a/NEWS.md b/NEWS.md index dd1c7d38..adaaf04f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -33,7 +33,8 @@ * Transformation function have been renamed to `transform_*`-prefixed names instead of `*_trans`-suffixed names. This allows for a better tab-completion search of transformations. The S3 class of transformations has been - renamed from `"trans"` to `"transform"`. + renamed from `"trans"` to `"transform"`. `new_transform()` is the new + `trans_new()`. # scales 1.2.1 diff --git a/R/transform-compose.R b/R/transform-compose.R index ab3a739d..4af412f4 100644 --- a/R/transform-compose.R +++ b/R/transform-compose.R @@ -42,7 +42,7 @@ transform_compose <- function(...) { has_d_transform <- all(lengths(lapply(trans_list, "[[", "d_transform")) > 0) has_d_inverse <- all(lengths(lapply(trans_list, "[[", "d_inverse")) > 0) - trans_new( + new_transform( paste0("composition(", paste0(names, collapse = ","), ")"), transform = function(x) compose_fwd(x, trans_list), inverse = function(x) compose_rev(x, trans_list), diff --git a/R/transform-date.R b/R/transform-date.R index 743bb091..f17e7a52 100644 --- a/R/transform-date.R +++ b/R/transform-date.R @@ -8,7 +8,8 @@ #' t$inverse(t$transform(years)) #' t$format(t$breaks(range(years))) transform_date <- function() { - trans_new("date", + new_transform( + "date", transform = "from_date", inverse = "to_date", breaks = breaks_pretty(), @@ -55,7 +56,7 @@ transform_time <- function(tz = NULL) { structure(as.numeric(x), names = names(x)) } - trans_new("time", + new_transform("time", transform = "from_time", inverse = "to_time", breaks = breaks_pretty(), @@ -97,7 +98,7 @@ time_trans <- transform_time #' transform_timespan <- function(unit = c("secs", "mins", "hours", "days", "weeks")) { unit <- arg_match(unit) - trans_new( + new_transform( "timespan", transform = function(x) { structure(as.numeric(as.difftime(x, units = unit), units = "secs"), names = names(x)) @@ -119,7 +120,7 @@ timespan_trans <- transform_timespan #' @rdname transform_timespan #' @export transform_hms <- function() { - trans_new( + new_transform( "hms", transform = function(x) { structure(as.numeric(x), names = names(x)) diff --git a/R/transform-numeric.R b/R/transform-numeric.R index ade69f63..641b7f53 100644 --- a/R/transform-numeric.R +++ b/R/transform-numeric.R @@ -7,7 +7,7 @@ #' @examples #' plot(transform_asn(), xlim = c(0, 1)) transform_asn <- function() { - trans_new( + new_transform( "asn", function(x) 2 * asin(sqrt(x)), function(x) sin(x / 2)^2, @@ -27,7 +27,7 @@ asn_trans <- transform_asn #' @examples #' plot(transform_atanh(), xlim = c(-1, 1)) transform_atanh <- function() { - trans_new( + new_transform( "atanh", "atanh", "tanh", @@ -47,7 +47,7 @@ atanh_trans <- transform_atanh #' @examples #' plot(transform_asinh(), xlim = c(-1e2, 1e2)) transform_asinh <- function() { - trans_new( + new_transform( "asinh", transform = asinh, inverse = sinh, @@ -125,7 +125,7 @@ transform_boxcox <- function(p, offset = 0) { trans(x) } - trans_new( + new_transform( paste0("pow-", format(p)), trans_with_check, inv, @@ -153,7 +153,7 @@ transform_modulus <- function(p, offset = 1) { d_trans <- function(x) (abs(x) + offset)^(p - 1) d_inv <- function(x) (abs(x) * p + 1)^(1 / p - 1) } - trans_new( + new_transform( paste0("mt-pow-", format(p)), trans, inv, d_transform = d_trans, d_inverse = d_inv ) @@ -217,7 +217,7 @@ transform_yj <- function(p) { d_inv_neg <- function(x) (-(2 - p) * x + 1)^(1 / (2 - p) - 1) } - trans_new( + new_transform( paste0("yeo-johnson-", format(p)), function(x) trans_two_sided(x, trans_pos, trans_neg), function(x) trans_two_sided(x, inv_pos, inv_neg), @@ -250,7 +250,7 @@ trans_two_sided <- function(x, pos, neg, f_at_0 = 0) { #' plot(transform_exp(), xlim = c(-2, 2)) transform_exp <- function(base = exp(1)) { force(base) - trans_new( + new_transform( paste0("power-", format(base)), function(x) base^x, function(x) log(x, base = base), @@ -269,7 +269,7 @@ exp_trans <- transform_exp #' @examples #' plot(transform_identity(), xlim = c(-1, 1)) transform_identity <- function() { - trans_new( + new_transform( "identity", "force", "force", @@ -308,7 +308,7 @@ identity_trans <- transform_identity #' lines(transform_log(), xlim = c(1, 20), col = "red") transform_log <- function(base = exp(1)) { force(base) - trans_new( + new_transform( paste0("log-", format(base)), function(x) log(x, base), function(x) base^x, @@ -333,7 +333,7 @@ transform_log2 <- function() { #' @rdname transform_log #' @export transform_log1p <- function() { - trans_new( + new_transform( "log1p", "log1p", "expm1", @@ -361,7 +361,7 @@ log1p_trans <- transform_log1p #' @param sigma Scaling factor for the linear part of pseudo-log transformation. #' @export transform_pseudo_log <- function(sigma = 1, base = exp(1)) { - trans_new( + new_transform( "pseudo_log", function(x) asinh(x / (2 * sigma)) / log(base), function(x) 2 * sigma * sinh(x * log(base)), @@ -390,7 +390,7 @@ transform_probability <- function(distribution, ...) { pfun <- match.fun(paste0("p", distribution)) dfun <- match.fun(paste0("d", distribution)) - trans_new( + new_transform( paste0("prob-", distribution), function(x) qfun(x, ...), function(x) pfun(x, ...), @@ -425,7 +425,7 @@ probit_trans <- transform_probit #' @examples #' plot(transform_reciprocal(), xlim = c(0, 1)) transform_reciprocal <- function() { - trans_new( + new_transform( "reciprocal", function(x) 1 / x, function(x) 1 / x, @@ -448,7 +448,7 @@ reciprocal_trans <- transform_reciprocal #' @examples #' plot(transform_reverse(), xlim = c(-1, 1)) transform_reverse <- function() { - trans_new( + new_transform( "reverse", function(x) -x, function(x) -x, @@ -471,7 +471,7 @@ reverse_trans <- transform_reverse #' @examples #' plot(transform_sqrt(), xlim = c(0, 5)) transform_sqrt <- function() { - trans_new( + new_transform( "sqrt", "sqrt", function(x) ifelse(x < 0, NA_real_, x ^ 2), diff --git a/R/transform.R b/R/transform.R index 583e8811..bdd2c764 100644 --- a/R/transform.R +++ b/R/transform.R @@ -28,11 +28,11 @@ #' @export #' @keywords internal #' @aliases trans -trans_new <- function(name, transform, inverse, - d_transform = NULL, d_inverse = NULL, - breaks = extended_breaks(), - minor_breaks = regular_minor_breaks(), - format = format_format(), domain = c(-Inf, Inf)) { +new_transform <- function(name, transform, inverse, + d_transform = NULL, d_inverse = NULL, + breaks = extended_breaks(), + minor_breaks = regular_minor_breaks(), + format = format_format(), domain = c(-Inf, Inf)) { if (is.character(transform)) transform <- match.fun(transform) if (is.character(inverse)) inverse <- match.fun(inverse) if (is.character(d_transform)) d_transform <- match.fun(d_transform) @@ -54,12 +54,16 @@ trans_new <- function(name, transform, inverse, ) } -#' @rdname trans_new +#' @rdname new_transform +#' @export +trans_new <- new_transform + +#' @rdname new_transform #' @export is.transform <- function(x) inherits(x, "transform") #' @export -#' @rdname trans_new +#' @rdname new_transform is.trans <- is.transform #' @export @@ -95,7 +99,7 @@ lines.transform <- function(x, ..., xlim) { graphics::lines(xgrid, y, ...) } -#' @rdname trans_new +#' @rdname new_transform #' @export as.transform <- function(x, arg = deparse(substitute(x))) { if (is.transform(x)) { @@ -120,7 +124,7 @@ as.transform <- function(x, arg = deparse(substitute(x))) { } #' @export -#' @rdname trans_new +#' @rdname new_transform as.trans <- as.transform #' Compute range of transformed values diff --git a/README.Rmd b/README.Rmd index d63d324e..1f82152c 100644 --- a/README.Rmd +++ b/README.Rmd @@ -98,8 +98,8 @@ scales also gives users the ability to define and apply their own custom transformation functions for repeated use. ```{r transforms} -# use trans_new to build a new transformation -transform_logp3 <- trans_new( +# use new_transform to build a new transformation +transform_logp3 <- new_transform( name = "logp", transform = function(x) log(x + 3), inverse = function(x) exp(x) - 3, diff --git a/README.md b/README.md index 18b81eee..af4404c6 100644 --- a/README.md +++ b/README.md @@ -111,8 +111,8 @@ scales also gives users the ability to define and apply their own custom transformation functions for repeated use. ``` r -# use trans_new to build a new transformation -transform_logp3 <- trans_new( +# use new_transform to build a new transformation +transform_logp3 <- new_transform( name = "logp", transform = function(x) log(x + 3), inverse = function(x) exp(x) - 3, diff --git a/man/trans_new.Rd b/man/new_transform.Rd similarity index 89% rename from man/trans_new.Rd rename to man/new_transform.Rd index 7c244e41..b8cc7af6 100644 --- a/man/trans_new.Rd +++ b/man/new_transform.Rd @@ -1,14 +1,27 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/transform.R -\name{trans_new} -\alias{trans_new} +\name{new_transform} +\alias{new_transform} \alias{trans} +\alias{trans_new} \alias{is.transform} \alias{is.trans} \alias{as.transform} \alias{as.trans} \title{Create a new transformation object} \usage{ +new_transform( + name, + transform, + inverse, + d_transform = NULL, + d_inverse = NULL, + breaks = extended_breaks(), + minor_breaks = regular_minor_breaks(), + format = format_format(), + domain = c(-Inf, Inf) +) + trans_new( name, transform, diff --git a/tests/testthat/_snaps/trans.md b/tests/testthat/_snaps/trans.md index ab284e85..a2304d14 100644 --- a/tests/testthat/_snaps/trans.md +++ b/tests/testthat/_snaps/trans.md @@ -14,7 +14,7 @@ # trans has useful print method Code - trans_new("test", transform = identity, inverse = identity) + new_transform("test", transform = identity, inverse = identity) Output Transformer: test [-Inf, Inf] diff --git a/tests/testthat/test-trans-compose.R b/tests/testthat/test-trans-compose.R index c11f07f5..06a95d9f 100644 --- a/tests/testthat/test-trans-compose.R +++ b/tests/testthat/test-trans-compose.R @@ -11,7 +11,7 @@ test_that("composes derivatives correctly", { }) test_that("produces NULL derivatives if not all transforms have derivatives", { - t <- transform_compose("sqrt", trans_new("no_deriv", identity, identity)) + t <- transform_compose("sqrt", new_transform("no_deriv", identity, identity)) expect_null(t$d_transform) expect_null(t$d_inverse) }) diff --git a/tests/testthat/test-trans.R b/tests/testthat/test-trans.R index 39eba2e5..a6f50aac 100644 --- a/tests/testthat/test-trans.R +++ b/tests/testthat/test-trans.R @@ -24,7 +24,7 @@ test_that("as.transform generates informative error", { test_that("trans has useful print method", { expect_snapshot({ - trans_new("test", transform = identity, inverse = identity) + new_transform("test", transform = identity, inverse = identity) }) }) From 2a4a1dc7dbb6b43c81c3297ba4b673e1fdf160d2 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Tue, 7 Nov 2023 08:21:05 +0100 Subject: [PATCH 6/8] rename `trans_range()` to `range_transform()` --- NAMESPACE | 1 + R/transform.R | 6 +++++- man/{trans_range.Rd => range_transform.Rd} | 5 ++++- tests/testthat/test-trans.R | 4 ++-- 4 files changed, 12 insertions(+), 4 deletions(-) rename man/{trans_range.Rd => range_transform.Rd} (85%) diff --git a/NAMESPACE b/NAMESPACE index d63362c9..61b8e4cc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -149,6 +149,7 @@ export(probit_trans) export(pseudo_log_trans) export(pvalue) export(pvalue_format) +export(range_transform) export(reciprocal_trans) export(regular_minor_breaks) export(rescale) diff --git a/R/transform.R b/R/transform.R index bdd2c764..94761311 100644 --- a/R/transform.R +++ b/R/transform.R @@ -136,7 +136,11 @@ as.trans <- as.transform #' @param x a numeric vector to compute the range of #' @export #' @keywords internal -trans_range <- function(transform, x) { +range_transform <- function(transform, x) { transform <- as.transform(transform) range(transform$transform(range(squish(x, transform$domain), na.rm = TRUE))) } + +#' @export +#' @rdname range_transform +trans_range <- range_transform diff --git a/man/trans_range.Rd b/man/range_transform.Rd similarity index 85% rename from man/trans_range.Rd rename to man/range_transform.Rd index 2c988c99..0b3d2993 100644 --- a/man/trans_range.Rd +++ b/man/range_transform.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/transform.R -\name{trans_range} +\name{range_transform} +\alias{range_transform} \alias{trans_range} \title{Compute range of transformed values} \usage{ +range_transform(transform, x) + trans_range(transform, x) } \arguments{ diff --git a/tests/testthat/test-trans.R b/tests/testthat/test-trans.R index a6f50aac..0965c3ce 100644 --- a/tests/testthat/test-trans.R +++ b/tests/testthat/test-trans.R @@ -1,8 +1,8 @@ test_that("Transformed ranges silently drop out-of-domain values", { - r1 <- trans_range(transform_log(), -1:10) + r1 <- range_transform(transform_log(), -1:10) expect_equal(r1, log(c(1e-100, 10))) - r2 <- trans_range(transform_sqrt(), -1:10) + r2 <- range_transform(transform_sqrt(), -1:10) expect_equal(r2, sqrt(c(0, 10))) }) From 34479532a47fff61a0fc5caba9dfc89a63947f67 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Tue, 7 Nov 2023 08:57:56 +0100 Subject: [PATCH 7/8] resolve merge conflict --- NAMESPACE | 14 +++++++ R/colour-manip.R | 8 ++-- R/colour-mapping.R | 8 ++-- R/pal-area.R | 8 +++- R/pal-brewer.R | 16 ++++--- R/pal-dichromat.R | 14 ++++--- R/pal-gradient.R | 42 ++++++++++++------- R/pal-grey.R | 12 ++++-- R/pal-hue.R | 39 +++++++++-------- R/pal-identity.R | 7 +++- R/pal-linetype.R | 6 ++- R/pal-manual.R | 6 ++- R/pal-rescale.R | 6 ++- R/pal-shape.R | 6 ++- R/pal-viridis.R | 14 ++++--- R/scale-continuous.R | 2 +- R/scale-discrete.R | 2 +- README.Rmd | 4 +- README.md | 4 +- man/cscale.Rd | 2 +- man/dscale.Rd | 2 +- man/{area_pal.Rd => pal_area.Rd} | 5 ++- man/{brewer_pal.Rd => pal_brewer.Rd} | 15 ++++--- man/{dichromat_pal.Rd => pal_dichromat.Rd} | 13 +++--- ...iv_gradient_pal.Rd => pal_div_gradient.Rd} | 18 +++++--- man/{gradient_n_pal.Rd => pal_gradient_n.Rd} | 5 ++- man/{grey_pal.Rd => pal_grey.Rd} | 11 +++-- man/{hue_pal.Rd => pal_hue.Rd} | 31 +++++++------- man/{identity_pal.Rd => pal_identity.Rd} | 5 ++- man/{linetype_pal.Rd => pal_linetype.Rd} | 5 ++- man/{manual_pal.Rd => pal_manual.Rd} | 5 ++- man/{rescale_pal.Rd => pal_rescale.Rd} | 5 ++- ...eq_gradient_pal.Rd => pal_seq_gradient.Rd} | 11 +++-- man/{shape_pal.Rd => pal_shape.Rd} | 7 +++- man/{viridis_pal.Rd => pal_viridis.Rd} | 13 +++--- man/show_col.Rd | 8 ++-- tests/testthat/test-pal-hue.R | 14 +++---- tests/testthat/test-pal-manual.R | 8 ++-- 38 files changed, 260 insertions(+), 141 deletions(-) rename man/{area_pal.Rd => pal_area.Rd} (87%) rename man/{brewer_pal.Rd => pal_brewer.Rd} (74%) rename man/{dichromat_pal.Rd => pal_dichromat.Rd} (65%) rename man/{div_gradient_pal.Rd => pal_div_gradient.Rd} (64%) rename man/{gradient_n_pal.Rd => pal_gradient_n.Rd} (88%) rename man/{grey_pal.Rd => pal_grey.Rd} (67%) rename man/{hue_pal.Rd => pal_hue.Rd} (54%) rename man/{identity_pal.Rd => pal_identity.Rd} (81%) rename man/{linetype_pal.Rd => pal_linetype.Rd} (82%) rename man/{manual_pal.Rd => pal_manual.Rd} (83%) rename man/{rescale_pal.Rd => pal_rescale.Rd} (85%) rename man/{seq_gradient_pal.Rd => pal_seq_gradient.Rd} (69%) rename man/{shape_pal.Rd => pal_shape.Rd} (68%) rename man/{viridis_pal.Rd => pal_viridis.Rd} (79%) diff --git a/NAMESPACE b/NAMESPACE index 61b8e4cc..c097380d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -140,6 +140,20 @@ export(ordinal_english) export(ordinal_format) export(ordinal_french) export(ordinal_spanish) +export(pal_area) +export(pal_brewer) +export(pal_dichromat) +export(pal_div_gradient) +export(pal_gradient_n) +export(pal_grey) +export(pal_hue) +export(pal_identity) +export(pal_linetype) +export(pal_manual) +export(pal_rescale) +export(pal_seq_gradient) +export(pal_shape) +export(pal_viridis) export(parse_format) export(percent) export(percent_format) diff --git a/R/colour-manip.R b/R/colour-manip.R index 3ea479e9..cb99c7cf 100644 --- a/R/colour-manip.R +++ b/R/colour-manip.R @@ -84,11 +84,11 @@ alpha <- function(colour, alpha = NA) { #' @importFrom graphics par plot rect text #' @keywords internal #' @examples -#' show_col(hue_pal()(9)) -#' show_col(hue_pal()(9), borders = NA) +#' show_col(pal_hue()(9)) +#' show_col(pal_hue()(9), borders = NA) #' -#' show_col(viridis_pal()(16)) -#' show_col(viridis_pal()(16), labels = FALSE) +#' show_col(pal_viridis()(16)) +#' show_col(pal_viridis()(16), labels = FALSE) show_col <- function(colours, labels = TRUE, borders = NULL, cex_label = 1, ncol = NULL) { n <- length(colours) diff --git a/R/colour-mapping.R b/R/colour-mapping.R index 76b82066..8ed05d0e 100644 --- a/R/colour-mapping.R +++ b/R/colour-mapping.R @@ -322,14 +322,14 @@ toPaletteFunc.character <- function(pal, alpha, nlevels) { if (length(pal) == 1 && pal %in% row.names(RColorBrewer::brewer.pal.info)) { paletteInfo <- RColorBrewer::brewer.pal.info[pal, ] if (!is.null(nlevels)) { - # brewer_pal will return NAs if you ask for more colors than the palette has - colors <- brewer_pal(palette = pal)(abs(nlevels)) + # pal_brewer will return NAs if you ask for more colors than the palette has + colors <- pal_brewer(palette = pal)(abs(nlevels)) colors <- colors[!is.na(colors)] } else { - colors <- brewer_pal(palette = pal)(RColorBrewer::brewer.pal.info[pal, "maxcolors"]) # Get all colors + colors <- pal_brewer(palette = pal)(RColorBrewer::brewer.pal.info[pal, "maxcolors"]) # Get all colors } } else if (length(pal) == 1 && pal %in% c("viridis", "magma", "inferno", "plasma")) { - colors <- viridis_pal(option = pal)(256) + colors <- pal_viridis(option = pal)(256) } else { colors <- pal } diff --git a/R/pal-area.R b/R/pal-area.R index 7ce1d40d..ecc87365 100644 --- a/R/pal-area.R +++ b/R/pal-area.R @@ -3,14 +3,18 @@ #' @param range Numeric vector of length two, giving range of possible sizes. #' Should be greater than 0. #' @export -area_pal <- function(range = c(1, 6)) { +pal_area <- function(range = c(1, 6)) { force(range) function(x) rescale(sqrt(x), range, c(0, 1)) } +#' @export +#' @rdname pal_area +area_pal <- pal_area + #' @param max A number representing the maximum size. #' @export -#' @rdname area_pal +#' @rdname pal_area abs_area <- function(max) { force(max) function(x) rescale(sqrt(abs(x)), c(0, max), c(0, 1)) diff --git a/R/pal-brewer.R b/R/pal-brewer.R index 72c5b358..597eee74 100644 --- a/R/pal-brewer.R +++ b/R/pal-brewer.R @@ -10,14 +10,14 @@ #' @references #' @export #' @examples -#' show_col(brewer_pal()(10)) -#' show_col(brewer_pal("div")(5)) -#' show_col(brewer_pal(palette = "Greens")(5)) +#' show_col(pal_brewer()(10)) +#' show_col(pal_brewer("div")(5)) +#' show_col(pal_brewer(palette = "Greens")(5)) #' #' # Can use with gradient_n to create a continuous gradient -#' cols <- brewer_pal("div")(5) -#' show_col(gradient_n_pal(cols)(seq(0, 1, length.out = 30))) -brewer_pal <- function(type = "seq", palette = 1, direction = 1) { +#' cols <- pal_brewer("div")(5) +#' show_col(pal_gradient_n(cols)(seq(0, 1, length.out = 30))) +pal_brewer <- function(type = "seq", palette = 1, direction = 1) { pal <- pal_name(palette, type) force(direction) function(n) { @@ -42,6 +42,10 @@ brewer_pal <- function(type = "seq", palette = 1, direction = 1) { } } +#' @export +#' @rdname pal_brewer +brewer_pal <- pal_brewer + pal_name <- function(palette, type) { if (is.character(palette)) { if (!palette %in% unlist(brewer)) { diff --git a/R/pal-dichromat.R b/R/pal-dichromat.R index 65ebe98b..fcf5b8fa 100644 --- a/R/pal-dichromat.R +++ b/R/pal-dichromat.R @@ -5,14 +5,14 @@ #' @export #' @examples #' if (requireNamespace("dichromat", quietly = TRUE)) { -#' show_col(dichromat_pal("BluetoOrange.10")(10)) -#' show_col(dichromat_pal("BluetoOrange.10")(5)) +#' show_col(pal_dichromat("BluetoOrange.10")(10)) +#' show_col(pal_dichromat("BluetoOrange.10")(5)) #' #' # Can use with gradient_n to create a continous gradient -#' cols <- dichromat_pal("DarkRedtoBlue.12")(12) -#' show_col(gradient_n_pal(cols)(seq(0, 1, length.out = 30))) +#' cols <- pal_dichromat("DarkRedtoBlue.12")(12) +#' show_col(pal_gradient_n(cols)(seq(0, 1, length.out = 30))) #' } -dichromat_pal <- function(name) { +pal_dichromat <- function(name) { check_installed("dichromat") if (!any(name == names(dichromat::colorschemes))) { @@ -23,6 +23,10 @@ dichromat_pal <- function(name) { function(n) pal[seq_len(n)] } +#' @export +#' @rdname pal_dichromat +dichromat_pal <- pal_dichromat + dichromat_schemes <- function() { if (requireNamespace("dichromat", quietly = TRUE)) { diff --git a/R/pal-gradient.R b/R/pal-gradient.R index aff3ce1a..ae4e23d5 100644 --- a/R/pal-gradient.R +++ b/R/pal-gradient.R @@ -9,9 +9,9 @@ #' other values are deprecated. #' @export -gradient_n_pal <- function(colours, values = NULL, space = "Lab") { +pal_gradient_n <- function(colours, values = NULL, space = "Lab") { if (!identical(space, "Lab")) { - lifecycle::deprecate_warn("0.3.0", "gradient_n_pal(space = 'only supports be \"Lab\"')") + lifecycle::deprecate_warn("0.3.0", "pal_gradient_n(space = 'only supports be \"Lab\"')") } ramp <- colour_ramp(colours) force(values) @@ -31,41 +31,53 @@ gradient_n_pal <- function(colours, values = NULL, space = "Lab") { } } +#' @export +#' @rdname pal_gradient_n +gradient_n_pal <- pal_gradient_n + #' Diverging colour gradient (continuous). #' #' @param low colour for low end of gradient. #' @param mid colour for mid point #' @param high colour for high end of gradient. -#' @inheritParams gradient_n_pal +#' @inheritParams pal_gradient_n #' @export #' @examples #' x <- seq(-1, 1, length.out = 100) #' r <- sqrt(outer(x^2, x^2, "+")) -#' image(r, col = div_gradient_pal()(seq(0, 1, length.out = 12))) -#' image(r, col = div_gradient_pal()(seq(0, 1, length.out = 30))) -#' image(r, col = div_gradient_pal()(seq(0, 1, length.out = 100))) +#' image(r, col = pal_div_gradient()(seq(0, 1, length.out = 12))) +#' image(r, col = pal_div_gradient()(seq(0, 1, length.out = 30))) +#' image(r, col = pal_div_gradient()(seq(0, 1, length.out = 100))) #' #' library(munsell) -#' pal <- div_gradient_pal(low = mnsl(complement("10R 4/6"), fix = TRUE)) +#' pal <- pal_div_gradient(low = mnsl(complement("10R 4/6"), fix = TRUE)) #' image(r, col = pal(seq(0, 1, length.out = 100))) #' @importFrom munsell mnsl -div_gradient_pal <- function(low = mnsl("10B 4/6"), mid = mnsl("N 8/0"), high = mnsl("10R 4/6"), space = "Lab") { - gradient_n_pal(c(low, mid, high), space = space) +pal_div_gradient <- function(low = mnsl("10B 4/6"), mid = mnsl("N 8/0"), high = mnsl("10R 4/6"), space = "Lab") { + pal_gradient_n(c(low, mid, high), space = space) } +#' @export +#' @rdname pal_div_gradient +div_gradient_pal <- pal_div_gradient + #' Sequential colour gradient palette (continuous) #' #' @param low colour for low end of gradient. #' @param high colour for high end of gradient. -#' @inheritParams gradient_n_pal +#' @inheritParams pal_gradient_n #' @export #' @examples #' x <- seq(0, 1, length.out = 25) -#' show_col(seq_gradient_pal()(x)) -#' show_col(seq_gradient_pal("white", "black")(x)) +#' show_col(pal_seq_gradient()(x)) +#' show_col(pal_seq_gradient("white", "black")(x)) #' #' library(munsell) -#' show_col(seq_gradient_pal("white", mnsl("10R 4/6"))(x)) -seq_gradient_pal <- function(low = mnsl("10B 4/6"), high = mnsl("10R 4/6"), space = "Lab") { - gradient_n_pal(c(low, high), space = space) +#' show_col(pal_seq_gradient("white", mnsl("10R 4/6"))(x)) +pal_seq_gradient <- function(low = mnsl("10B 4/6"), high = mnsl("10R 4/6"), space = "Lab") { + pal_gradient_n(c(low, high), space = space) } + +#' @export +#' @rdname pal_seq_gradient +seq_gradient_pal <- pal_seq_gradient diff --git a/R/pal-grey.R b/R/pal-grey.R index 7125b575..fa0b558c 100644 --- a/R/pal-grey.R +++ b/R/pal-grey.R @@ -2,12 +2,16 @@ #' #' @param start grey value at low end of palette #' @param end grey value at high end of palette -#' @seealso [seq_gradient_pal()] for continuous version +#' @seealso [pal_seq_gradient()] for continuous version #' @export #' @examples -#' show_col(grey_pal()(25)) -#' show_col(grey_pal(0, 1)(25)) -grey_pal <- function(start = 0.2, end = 0.8) { +#' show_col(pal_grey()(25)) +#' show_col(pal_grey(0, 1)(25)) +pal_grey <- function(start = 0.2, end = 0.8) { force_all(start, end) function(n) grDevices::grey.colors(n, start = start, end = end) } + +#' @export +#' @rdname pal_grey +grey_pal <- pal_grey diff --git a/R/pal-hue.R b/R/pal-hue.R index 5dcdb5aa..2826a424 100644 --- a/R/pal-hue.R +++ b/R/pal-hue.R @@ -9,25 +9,25 @@ #' 1 = clockwise, -1 = counter-clockwise #' @export #' @examples -#' show_col(hue_pal()(4)) -#' show_col(hue_pal()(9)) -#' show_col(hue_pal(l = 90)(9)) -#' show_col(hue_pal(l = 30)(9)) +#' show_col(pal_hue()(4)) +#' show_col(pal_hue()(9)) +#' show_col(pal_hue(l = 90)(9)) +#' show_col(pal_hue(l = 30)(9)) #' -#' show_col(hue_pal()(9)) -#' show_col(hue_pal(direction = -1)(9)) -#' show_col(hue_pal(h.start = 30)(9)) -#' show_col(hue_pal(h.start = 90)(9)) +#' show_col(pal_hue()(9)) +#' show_col(pal_hue(direction = -1)(9)) +#' show_col(pal_hue(h.start = 30)(9)) +#' show_col(pal_hue(h.start = 90)(9)) #' -#' show_col(hue_pal()(9)) -#' show_col(hue_pal(h = c(0, 90))(9)) -#' show_col(hue_pal(h = c(90, 180))(9)) -#' show_col(hue_pal(h = c(180, 270))(9)) -#' show_col(hue_pal(h = c(270, 360))(9)) -hue_pal <- function(h = c(0, 360) + 15, c = 100, l = 65, h.start = 0, direction = 1) { - if (length(h) != 2) cli::cli_abort("{.arg h} must have length 2") - if (length(l) != 1) cli::cli_abort("{.arg l} must have length 1") - if (length(c) != 1) cli::cli_abort("{.arg c} must have length 1") +#' show_col(pal_hue()(9)) +#' show_col(pal_hue(h = c(0, 90))(9)) +#' show_col(pal_hue(h = c(90, 180))(9)) +#' show_col(pal_hue(h = c(180, 270))(9)) +#' show_col(pal_hue(h = c(270, 360))(9)) +pal_hue <- function(h = c(0, 360) + 15, c = 100, l = 65, h.start = 0, direction = 1) { + if (length(h) != 2) cli::cli_abort("{.arg h} must have length 2.") + if (length(l) != 1) cli::cli_abort("{.arg l} must have length 1.") + if (length(c) != 1) cli::cli_abort("{.arg c} must have length 1.") force_all(h, c, l, h.start, direction) function(n) { if (n == 0) { @@ -52,3 +52,8 @@ hue_pal <- function(h = c(0, 360) + 15, c = 100, l = 65, h.start = 0, direction } } } + +#' @export +#' @rdname pal_hue +hue_pal <- pal_hue + diff --git a/R/pal-identity.R b/R/pal-identity.R index a68a0565..fef79227 100644 --- a/R/pal-identity.R +++ b/R/pal-identity.R @@ -3,6 +3,11 @@ #' Leaves values unchanged - useful when the data is already scaled. #' #' @export -identity_pal <- function() { +pal_identity <- function() { function(x) x } + + +#' @export +#' @rdname pal_identity +identity_pal <- pal_identity diff --git a/R/pal-linetype.R b/R/pal-linetype.R index c03728df..8dbb0d23 100644 --- a/R/pal-linetype.R +++ b/R/pal-linetype.R @@ -3,7 +3,7 @@ #' Based on a set supplied by Richard Pearson, University of Manchester #' #' @export -linetype_pal <- function() { +pal_linetype <- function() { types <- c( "solid", "22", "42", "44", "13", "1343", "73", "2262", "12223242", "F282", "F4448444", "224282F2", "F1" @@ -13,3 +13,7 @@ linetype_pal <- function() { types[seq_len(n)] } } + +#' @export +#' @rdname pal_linetype +linetype_pal <- pal_linetype diff --git a/R/pal-manual.R b/R/pal-manual.R index 8a4bef85..923a4f9b 100644 --- a/R/pal-manual.R +++ b/R/pal-manual.R @@ -2,7 +2,7 @@ #' #' @param values vector of values to be used as a palette. #' @export -manual_pal <- function(values) { +pal_manual <- function(values) { force(values) function(n) { n_values <- length(values) @@ -12,3 +12,7 @@ manual_pal <- function(values) { unname(values[seq_len(n)]) } } + +#' @export +#' @rdname pal_manual +manual_pal <- pal_manual diff --git a/R/pal-rescale.R b/R/pal-rescale.R index 8c32ab6b..bbff22fa 100644 --- a/R/pal-rescale.R +++ b/R/pal-rescale.R @@ -6,9 +6,13 @@ #' @param range Numeric vector of length two, giving range of possible #' values. Should be between 0 and 1. #' @export -rescale_pal <- function(range = c(0.1, 1)) { +pal_rescale <- function(range = c(0.1, 1)) { force(range) function(x) { rescale(x, range, c(0, 1)) } } + +#' @export +#' @rdname pal_rescale +rescale_pal <- pal_rescale diff --git a/R/pal-shape.R b/R/pal-shape.R index b0d9d76f..7c93ed3e 100644 --- a/R/pal-shape.R +++ b/R/pal-shape.R @@ -2,7 +2,7 @@ #' #' @param solid should shapes be solid or not? #' @export -shape_pal <- function(solid = TRUE) { +pal_shape <- function(solid = TRUE) { force(solid) function(n) { if (n > 6) { @@ -19,3 +19,7 @@ shape_pal <- function(solid = TRUE) { } } } + +#' @export +#' @rdname pal_shape +shape_pal <- pal_shape diff --git a/R/pal-viridis.R b/R/pal-viridis.R index 94e12656..04d02d55 100644 --- a/R/pal-viridis.R +++ b/R/pal-viridis.R @@ -6,13 +6,17 @@ #' @references #' @export #' @examples -#' show_col(viridis_pal()(10)) -#' show_col(viridis_pal(direction = -1)(6)) -#' show_col(viridis_pal(begin = 0.2, end = 0.8)(4)) -#' show_col(viridis_pal(option = "plasma")(6)) -viridis_pal <- function(alpha = 1, begin = 0, end = 1, direction = 1, option = "D") { +#' show_col(pal_viridis()(10)) +#' show_col(pal_viridis(direction = -1)(6)) +#' show_col(pal_viridis(begin = 0.2, end = 0.8)(4)) +#' show_col(pal_viridis(option = "plasma")(6)) +pal_viridis <- function(alpha = 1, begin = 0, end = 1, direction = 1, option = "D") { force_all(alpha, begin, end, direction, option) function(n) { viridisLite::viridis(n, alpha, begin, end, direction, option) } } + +#' @export +#' @rdname pal_viridis +viridis_pal <- pal_viridis diff --git a/R/scale-continuous.R b/R/scale-continuous.R index 0957e802..e687937c 100644 --- a/R/scale-continuous.R +++ b/R/scale-continuous.R @@ -21,7 +21,7 @@ #' with(mtcars, plot(disp, mpg, cex = cscale(hp, area_pal()))) #' with(mtcars, plot(disp, mpg, #' pch = 20, cex = 5, -#' col = cscale(hp, seq_gradient_pal("grey80", "black")) +#' col = cscale(hp, pal_seq_gradient("grey80", "black")) #' )) cscale <- function(x, palette, na.value = NA_real_, trans = transform_identity()) { if (!is.trans(trans)) cli::cli_abort("{.arg trans} must be a {.cls trans} object") diff --git a/R/scale-discrete.R b/R/scale-discrete.R index 38ec0c36..1766495d 100644 --- a/R/scale-discrete.R +++ b/R/scale-discrete.R @@ -7,7 +7,7 @@ #' @examples #' with(mtcars, plot(disp, mpg, #' pch = 20, cex = 3, -#' col = dscale(factor(cyl), brewer_pal()) +#' col = dscale(factor(cyl), pal_brewer()) #' )) dscale <- function(x, palette, na.value = NA) { limits <- train_discrete(x) diff --git a/README.Rmd b/README.Rmd index 1f82152c..d7d1b6fa 100644 --- a/README.Rmd +++ b/README.Rmd @@ -86,10 +86,10 @@ Scales colour palettes are used to power the scales in ggplot2, but you can use ```{r, palettes} library(scales) # pull a list of colours from any palette -viridis_pal()(4) +pal_viridis()(4) # use in combination with baseR `palette()` to set new defaults -palette(brewer_pal(palette = "Set2")(4)) +palette(pal_brewer(palette = "Set2")(4)) par(mar = c(5, 5, 1, 1)) plot(Sepal.Length ~ Sepal.Width, data = iris, col = Species, pch = 20) ``` diff --git a/README.md b/README.md index af4404c6..394b3663 100644 --- a/README.md +++ b/README.md @@ -96,11 +96,11 @@ might apply them to a base plot. ``` r library(scales) # pull a list of colours from any palette -viridis_pal()(4) +pal_viridis()(4) #> [1] "#440154FF" "#31688EFF" "#35B779FF" "#FDE725FF" # use in combination with baseR `palette()` to set new defaults -palette(brewer_pal(palette = "Set2")(4)) +palette(pal_brewer(palette = "Set2")(4)) par(mar = c(5, 5, 1, 1)) plot(Sepal.Length ~ Sepal.Width, data = iris, col = Species, pch = 20) ``` diff --git a/man/cscale.Rd b/man/cscale.Rd index f535dce1..001520bd 100644 --- a/man/cscale.Rd +++ b/man/cscale.Rd @@ -34,6 +34,6 @@ with(mtcars, plot(disp, mpg, cex = cscale(hp, rescale_pal(), with(mtcars, plot(disp, mpg, cex = cscale(hp, area_pal()))) with(mtcars, plot(disp, mpg, pch = 20, cex = 5, - col = cscale(hp, seq_gradient_pal("grey80", "black")) + col = cscale(hp, pal_seq_gradient("grey80", "black")) )) } diff --git a/man/dscale.Rd b/man/dscale.Rd index 212253eb..87972ba8 100644 --- a/man/dscale.Rd +++ b/man/dscale.Rd @@ -19,6 +19,6 @@ Discrete scale \examples{ with(mtcars, plot(disp, mpg, pch = 20, cex = 3, - col = dscale(factor(cyl), brewer_pal()) + col = dscale(factor(cyl), pal_brewer()) )) } diff --git a/man/area_pal.Rd b/man/pal_area.Rd similarity index 87% rename from man/area_pal.Rd rename to man/pal_area.Rd index 2dc3184c..b388b53a 100644 --- a/man/area_pal.Rd +++ b/man/pal_area.Rd @@ -1,10 +1,13 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/pal-area.R -\name{area_pal} +\name{pal_area} +\alias{pal_area} \alias{area_pal} \alias{abs_area} \title{Area palettes (continuous)} \usage{ +pal_area(range = c(1, 6)) + area_pal(range = c(1, 6)) abs_area(max) diff --git a/man/brewer_pal.Rd b/man/pal_brewer.Rd similarity index 74% rename from man/brewer_pal.Rd rename to man/pal_brewer.Rd index dd7e72be..c129cfc8 100644 --- a/man/brewer_pal.Rd +++ b/man/pal_brewer.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/pal-brewer.R -\name{brewer_pal} +\name{pal_brewer} +\alias{pal_brewer} \alias{brewer_pal} \title{Colour Brewer palette (discrete)} \usage{ +pal_brewer(type = "seq", palette = 1, direction = 1) + brewer_pal(type = "seq", palette = 1, direction = 1) } \arguments{ @@ -21,13 +24,13 @@ order of colours is reversed.} Colour Brewer palette (discrete) } \examples{ -show_col(brewer_pal()(10)) -show_col(brewer_pal("div")(5)) -show_col(brewer_pal(palette = "Greens")(5)) +show_col(pal_brewer()(10)) +show_col(pal_brewer("div")(5)) +show_col(pal_brewer(palette = "Greens")(5)) # Can use with gradient_n to create a continuous gradient -cols <- brewer_pal("div")(5) -show_col(gradient_n_pal(cols)(seq(0, 1, length.out = 30))) +cols <- pal_brewer("div")(5) +show_col(pal_gradient_n(cols)(seq(0, 1, length.out = 30))) } \references{ \url{https://colorbrewer2.org} diff --git a/man/dichromat_pal.Rd b/man/pal_dichromat.Rd similarity index 65% rename from man/dichromat_pal.Rd rename to man/pal_dichromat.Rd index e79862e7..a0ba08ca 100644 --- a/man/dichromat_pal.Rd +++ b/man/pal_dichromat.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/pal-dichromat.R -\name{dichromat_pal} +\name{pal_dichromat} +\alias{pal_dichromat} \alias{dichromat_pal} \title{Dichromat (colour-blind) palette (discrete)} \usage{ +pal_dichromat(name) + dichromat_pal(name) } \arguments{ @@ -15,11 +18,11 @@ Dichromat (colour-blind) palette (discrete) } \examples{ if (requireNamespace("dichromat", quietly = TRUE)) { - show_col(dichromat_pal("BluetoOrange.10")(10)) - show_col(dichromat_pal("BluetoOrange.10")(5)) + show_col(pal_dichromat("BluetoOrange.10")(10)) + show_col(pal_dichromat("BluetoOrange.10")(5)) # Can use with gradient_n to create a continous gradient - cols <- dichromat_pal("DarkRedtoBlue.12")(12) - show_col(gradient_n_pal(cols)(seq(0, 1, length.out = 30))) + cols <- pal_dichromat("DarkRedtoBlue.12")(12) + show_col(pal_gradient_n(cols)(seq(0, 1, length.out = 30))) } } diff --git a/man/div_gradient_pal.Rd b/man/pal_div_gradient.Rd similarity index 64% rename from man/div_gradient_pal.Rd rename to man/pal_div_gradient.Rd index 33aaa41d..c633eef9 100644 --- a/man/div_gradient_pal.Rd +++ b/man/pal_div_gradient.Rd @@ -1,9 +1,17 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/pal-gradient.R -\name{div_gradient_pal} +\name{pal_div_gradient} +\alias{pal_div_gradient} \alias{div_gradient_pal} \title{Diverging colour gradient (continuous).} \usage{ +pal_div_gradient( + low = mnsl("10B 4/6"), + mid = mnsl("N 8/0"), + high = mnsl("10R 4/6"), + space = "Lab" +) + div_gradient_pal( low = mnsl("10B 4/6"), mid = mnsl("N 8/0"), @@ -27,11 +35,11 @@ Diverging colour gradient (continuous). \examples{ x <- seq(-1, 1, length.out = 100) r <- sqrt(outer(x^2, x^2, "+")) -image(r, col = div_gradient_pal()(seq(0, 1, length.out = 12))) -image(r, col = div_gradient_pal()(seq(0, 1, length.out = 30))) -image(r, col = div_gradient_pal()(seq(0, 1, length.out = 100))) +image(r, col = pal_div_gradient()(seq(0, 1, length.out = 12))) +image(r, col = pal_div_gradient()(seq(0, 1, length.out = 30))) +image(r, col = pal_div_gradient()(seq(0, 1, length.out = 100))) library(munsell) -pal <- div_gradient_pal(low = mnsl(complement("10R 4/6"), fix = TRUE)) +pal <- pal_div_gradient(low = mnsl(complement("10R 4/6"), fix = TRUE)) image(r, col = pal(seq(0, 1, length.out = 100))) } diff --git a/man/gradient_n_pal.Rd b/man/pal_gradient_n.Rd similarity index 88% rename from man/gradient_n_pal.Rd rename to man/pal_gradient_n.Rd index 7cbbefeb..239dcc22 100644 --- a/man/gradient_n_pal.Rd +++ b/man/pal_gradient_n.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/pal-gradient.R -\name{gradient_n_pal} +\name{pal_gradient_n} +\alias{pal_gradient_n} \alias{gradient_n_pal} \title{Arbitrary colour gradient palette (continuous)} \usage{ +pal_gradient_n(colours, values = NULL, space = "Lab") + gradient_n_pal(colours, values = NULL, space = "Lab") } \arguments{ diff --git a/man/grey_pal.Rd b/man/pal_grey.Rd similarity index 67% rename from man/grey_pal.Rd rename to man/pal_grey.Rd index 7bd01256..08fab5f3 100644 --- a/man/grey_pal.Rd +++ b/man/pal_grey.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/pal-grey.R -\name{grey_pal} +\name{pal_grey} +\alias{pal_grey} \alias{grey_pal} \title{Grey scale palette (discrete)} \usage{ +pal_grey(start = 0.2, end = 0.8) + grey_pal(start = 0.2, end = 0.8) } \arguments{ @@ -15,9 +18,9 @@ grey_pal(start = 0.2, end = 0.8) Grey scale palette (discrete) } \examples{ -show_col(grey_pal()(25)) -show_col(grey_pal(0, 1)(25)) +show_col(pal_grey()(25)) +show_col(pal_grey(0, 1)(25)) } \seealso{ -\code{\link[=seq_gradient_pal]{seq_gradient_pal()}} for continuous version +\code{\link[=pal_seq_gradient]{pal_seq_gradient()}} for continuous version } diff --git a/man/hue_pal.Rd b/man/pal_hue.Rd similarity index 54% rename from man/hue_pal.Rd rename to man/pal_hue.Rd index 3abcd8bd..d105de05 100644 --- a/man/hue_pal.Rd +++ b/man/pal_hue.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/pal-hue.R -\name{hue_pal} +\name{pal_hue} +\alias{pal_hue} \alias{hue_pal} \title{Hue palette (discrete)} \usage{ +pal_hue(h = c(0, 360) + 15, c = 100, l = 65, h.start = 0, direction = 1) + hue_pal(h = c(0, 360) + 15, c = 100, l = 65, h.start = 0, direction = 1) } \arguments{ @@ -23,19 +26,19 @@ combination of hue and luminance.} Hue palette (discrete) } \examples{ -show_col(hue_pal()(4)) -show_col(hue_pal()(9)) -show_col(hue_pal(l = 90)(9)) -show_col(hue_pal(l = 30)(9)) +show_col(pal_hue()(4)) +show_col(pal_hue()(9)) +show_col(pal_hue(l = 90)(9)) +show_col(pal_hue(l = 30)(9)) -show_col(hue_pal()(9)) -show_col(hue_pal(direction = -1)(9)) -show_col(hue_pal(h.start = 30)(9)) -show_col(hue_pal(h.start = 90)(9)) +show_col(pal_hue()(9)) +show_col(pal_hue(direction = -1)(9)) +show_col(pal_hue(h.start = 30)(9)) +show_col(pal_hue(h.start = 90)(9)) -show_col(hue_pal()(9)) -show_col(hue_pal(h = c(0, 90))(9)) -show_col(hue_pal(h = c(90, 180))(9)) -show_col(hue_pal(h = c(180, 270))(9)) -show_col(hue_pal(h = c(270, 360))(9)) +show_col(pal_hue()(9)) +show_col(pal_hue(h = c(0, 90))(9)) +show_col(pal_hue(h = c(90, 180))(9)) +show_col(pal_hue(h = c(180, 270))(9)) +show_col(pal_hue(h = c(270, 360))(9)) } diff --git a/man/identity_pal.Rd b/man/pal_identity.Rd similarity index 81% rename from man/identity_pal.Rd rename to man/pal_identity.Rd index 028a6246..a4d2c243 100644 --- a/man/identity_pal.Rd +++ b/man/pal_identity.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/pal-identity.R -\name{identity_pal} +\name{pal_identity} +\alias{pal_identity} \alias{identity_pal} \title{Identity palette} \usage{ +pal_identity() + identity_pal() } \description{ diff --git a/man/linetype_pal.Rd b/man/pal_linetype.Rd similarity index 82% rename from man/linetype_pal.Rd rename to man/pal_linetype.Rd index d4404b8f..7c79c0d6 100644 --- a/man/linetype_pal.Rd +++ b/man/pal_linetype.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/pal-linetype.R -\name{linetype_pal} +\name{pal_linetype} +\alias{pal_linetype} \alias{linetype_pal} \title{Line type palette (discrete)} \usage{ +pal_linetype() + linetype_pal() } \description{ diff --git a/man/manual_pal.Rd b/man/pal_manual.Rd similarity index 83% rename from man/manual_pal.Rd rename to man/pal_manual.Rd index b7ff407d..394abff2 100644 --- a/man/manual_pal.Rd +++ b/man/pal_manual.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/pal-manual.R -\name{manual_pal} +\name{pal_manual} +\alias{pal_manual} \alias{manual_pal} \title{Manual palette (discrete)} \usage{ +pal_manual(values) + manual_pal(values) } \arguments{ diff --git a/man/rescale_pal.Rd b/man/pal_rescale.Rd similarity index 85% rename from man/rescale_pal.Rd rename to man/pal_rescale.Rd index 7fa3a584..74104b83 100644 --- a/man/rescale_pal.Rd +++ b/man/pal_rescale.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/pal-rescale.R -\name{rescale_pal} +\name{pal_rescale} +\alias{pal_rescale} \alias{rescale_pal} \title{Rescale palette (continuous)} \usage{ +pal_rescale(range = c(0.1, 1)) + rescale_pal(range = c(0.1, 1)) } \arguments{ diff --git a/man/seq_gradient_pal.Rd b/man/pal_seq_gradient.Rd similarity index 69% rename from man/seq_gradient_pal.Rd rename to man/pal_seq_gradient.Rd index 32973b3f..253e133e 100644 --- a/man/seq_gradient_pal.Rd +++ b/man/pal_seq_gradient.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/pal-gradient.R -\name{seq_gradient_pal} +\name{pal_seq_gradient} +\alias{pal_seq_gradient} \alias{seq_gradient_pal} \title{Sequential colour gradient palette (continuous)} \usage{ +pal_seq_gradient(low = mnsl("10B 4/6"), high = mnsl("10R 4/6"), space = "Lab") + seq_gradient_pal(low = mnsl("10B 4/6"), high = mnsl("10R 4/6"), space = "Lab") } \arguments{ @@ -19,9 +22,9 @@ Sequential colour gradient palette (continuous) } \examples{ x <- seq(0, 1, length.out = 25) -show_col(seq_gradient_pal()(x)) -show_col(seq_gradient_pal("white", "black")(x)) +show_col(pal_seq_gradient()(x)) +show_col(pal_seq_gradient("white", "black")(x)) library(munsell) -show_col(seq_gradient_pal("white", mnsl("10R 4/6"))(x)) +show_col(pal_seq_gradient("white", mnsl("10R 4/6"))(x)) } diff --git a/man/shape_pal.Rd b/man/pal_shape.Rd similarity index 68% rename from man/shape_pal.Rd rename to man/pal_shape.Rd index a3c69ccc..32f9213a 100644 --- a/man/shape_pal.Rd +++ b/man/pal_shape.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/pal-shape.R -\name{shape_pal} +% Please edit documentation in R/pal-shape.r +\name{pal_shape} +\alias{pal_shape} \alias{shape_pal} \title{Shape palette (discrete)} \usage{ +pal_shape(solid = TRUE) + shape_pal(solid = TRUE) } \arguments{ diff --git a/man/viridis_pal.Rd b/man/pal_viridis.Rd similarity index 79% rename from man/viridis_pal.Rd rename to man/pal_viridis.Rd index 0fa20328..d4b989c2 100644 --- a/man/viridis_pal.Rd +++ b/man/pal_viridis.Rd @@ -1,9 +1,12 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/pal-viridis.R -\name{viridis_pal} +\name{pal_viridis} +\alias{pal_viridis} \alias{viridis_pal} \title{Viridis palette} \usage{ +pal_viridis(alpha = 1, begin = 0, end = 1, direction = 1, option = "D") + viridis_pal(alpha = 1, begin = 0, end = 1, direction = 1, option = "D") } \arguments{ @@ -34,10 +37,10 @@ Eight options are available: Viridis palette } \examples{ -show_col(viridis_pal()(10)) -show_col(viridis_pal(direction = -1)(6)) -show_col(viridis_pal(begin = 0.2, end = 0.8)(4)) -show_col(viridis_pal(option = "plasma")(6)) +show_col(pal_viridis()(10)) +show_col(pal_viridis(direction = -1)(6)) +show_col(pal_viridis(begin = 0.2, end = 0.8)(4)) +show_col(pal_viridis(option = "plasma")(6)) } \references{ \url{https://bids.github.io/colormap/} diff --git a/man/show_col.Rd b/man/show_col.Rd index e51c00f1..10c5c15d 100644 --- a/man/show_col.Rd +++ b/man/show_col.Rd @@ -23,10 +23,10 @@ possible.} A quick and dirty way to show colours in a plot. } \examples{ -show_col(hue_pal()(9)) -show_col(hue_pal()(9), borders = NA) +show_col(pal_hue()(9)) +show_col(pal_hue()(9), borders = NA) -show_col(viridis_pal()(16)) -show_col(viridis_pal()(16), labels = FALSE) +show_col(pal_viridis()(16)) +show_col(pal_viridis()(16), labels = FALSE) } \keyword{internal} diff --git a/tests/testthat/test-pal-hue.R b/tests/testthat/test-pal-hue.R index f50fd147..2ef2fdaa 100644 --- a/tests/testthat/test-pal-hue.R +++ b/tests/testthat/test-pal-hue.R @@ -1,19 +1,19 @@ -test_that("hue_pal arguments are forcely evaluated on each call #81", { - col1 <- hue_pal(h.start = 0) - col2 <- hue_pal(h.start = 90) +test_that("pal_hue arguments are forcely evaluated on each call #81", { + col1 <- pal_hue(h.start = 0) + col2 <- pal_hue(h.start = 90) colours <- list() hues <- c(0, 90) for (i in 1:2) { - colours[[i]] <- hue_pal(h.start = hues[i]) + colours[[i]] <- pal_hue(h.start = hues[i]) } expect_equal(col1(1), colours[[1]](1)) expect_equal(col2(1), colours[[2]](1)) }) test_that("hue_pal respects direction argument #252", { - col1 <- hue_pal() - col2 <- hue_pal(direction = -1) + col1 <- pal_hue() + col2 <- pal_hue(direction = -1) expect_equal(col1(3), rev(col2(3))) expect_equal(col1(9), rev(col2(9))) @@ -21,7 +21,7 @@ test_that("hue_pal respects direction argument #252", { test_that("hue_pal respects h.start", { hue <- function(...) { - farver::decode_colour(hue_pal(...)(2), to = "hcl")[, "h"] + farver::decode_colour(pal_hue(...)(2), to = "hcl")[, "h"] } # Have to use large tolerance since we're generating out of gamut colours. diff --git a/tests/testthat/test-pal-manual.R b/tests/testthat/test-pal-manual.R index 5b8acc17..d77e05a4 100644 --- a/tests/testthat/test-pal-manual.R +++ b/tests/testthat/test-pal-manual.R @@ -1,8 +1,8 @@ -test_that("manual_pal gives warning if n greater than the number of values", { - expect_warning(manual_pal(c("red", "blue", "green"))(4), "can handle a maximum") +test_that("pal_manual gives warning if n greater than the number of values", { + expect_warning(pal_manual(c("red", "blue", "green"))(4), "can handle a maximum") }) -test_that("manual_pal returns an unnamed vector", { +test_that("pal_manual returns an unnamed vector", { x <- c(foo = "red", bar = "blue") - expect_equal(manual_pal(x)(2), unname(x)) + expect_equal(pal_manual(x)(2), unname(x)) }) From 0d60a0c2272831e1feb9eddb7f24aee75bdd55d5 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Tue, 7 Nov 2023 09:44:00 +0100 Subject: [PATCH 8/8] rename to `trim_to_domain()` --- NAMESPACE | 2 +- NEWS.md | 4 ++-- R/transform.R | 6 +++--- man/{range_transform.Rd => trim_to_domain.Rd} | 6 +++--- tests/testthat/test-trans.R | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) rename man/{range_transform.Rd => trim_to_domain.Rd} (85%) diff --git a/NAMESPACE b/NAMESPACE index c097380d..71ad1aa8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -163,7 +163,6 @@ export(probit_trans) export(pseudo_log_trans) export(pvalue) export(pvalue_format) -export(range_transform) export(reciprocal_trans) export(regular_minor_breaks) export(rescale) @@ -213,6 +212,7 @@ export(transform_sqrt) export(transform_time) export(transform_timespan) export(transform_yj) +export(trim_to_domain) export(unit_format) export(viridis_pal) export(wrap_format) diff --git a/NEWS.md b/NEWS.md index f74a4e7f..5e2ff322 100644 --- a/NEWS.md +++ b/NEWS.md @@ -33,8 +33,8 @@ * Transformation function have been renamed to `transform_*`-prefixed names instead of `*_trans`-suffixed names. This allows for a better tab-completion search of transformations. The S3 class of transformations has been - renamed from `"trans"` to `"transform"`. `new_transform()` is the new - `trans_new()`. + renamed from `"trans"` to `"transform"`. `new_transform()` replaces + `trans_new()` and `trim_to_domain()` replaces `trans_range()`. * Palette functions now have the `pal_`-prefix. The old `_pal`-suffixed versions are kept for backward compatibility. diff --git a/R/transform.R b/R/transform.R index 94761311..d6da0e74 100644 --- a/R/transform.R +++ b/R/transform.R @@ -136,11 +136,11 @@ as.trans <- as.transform #' @param x a numeric vector to compute the range of #' @export #' @keywords internal -range_transform <- function(transform, x) { +trim_to_domain <- function(transform, x) { transform <- as.transform(transform) range(transform$transform(range(squish(x, transform$domain), na.rm = TRUE))) } #' @export -#' @rdname range_transform -trans_range <- range_transform +#' @rdname trim_to_domain +trans_range <- trim_to_domain diff --git a/man/range_transform.Rd b/man/trim_to_domain.Rd similarity index 85% rename from man/range_transform.Rd rename to man/trim_to_domain.Rd index 0b3d2993..fa224c55 100644 --- a/man/range_transform.Rd +++ b/man/trim_to_domain.Rd @@ -1,11 +1,11 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/transform.R -\name{range_transform} -\alias{range_transform} +\name{trim_to_domain} +\alias{trim_to_domain} \alias{trans_range} \title{Compute range of transformed values} \usage{ -range_transform(transform, x) +trim_to_domain(transform, x) trans_range(transform, x) } diff --git a/tests/testthat/test-trans.R b/tests/testthat/test-trans.R index 0965c3ce..72fae75a 100644 --- a/tests/testthat/test-trans.R +++ b/tests/testthat/test-trans.R @@ -1,8 +1,8 @@ test_that("Transformed ranges silently drop out-of-domain values", { - r1 <- range_transform(transform_log(), -1:10) + r1 <- trim_to_domain(transform_log(), -1:10) expect_equal(r1, log(c(1e-100, 10))) - r2 <- range_transform(transform_sqrt(), -1:10) + r2 <- trim_to_domain(transform_sqrt(), -1:10) expect_equal(r2, sqrt(c(0, 10))) })