diff --git a/NEWS.md b/NEWS.md index f96283e156..3ab9c9b10c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -77,6 +77,9 @@ * `ggsave()` now returns the saved file location invisibly (#3379, @eliocamp). +* The scale arguments `limits`, `breaks`, `minor_breaks`, `labels`, `rescaler` + and `oob` now accept purrr style lambda notation (@teunbrand, #4427). + # ggplot2 3.3.3 This is a small patch release mainly intended to address changes in R and CRAN. It further changes the licensing model of ggplot2 to an MIT license. diff --git a/R/scale-.r b/R/scale-.r index 801b839109..3fa2f6cbde 100644 --- a/R/scale-.r +++ b/R/scale-.r @@ -18,13 +18,15 @@ #' [transformation object][scales::trans_new()] #' - A numeric vector of positions #' - A function that takes the limits as input and returns breaks -#' as output (e.g., a function returned by [scales::extended_breaks()]) +#' as output (e.g., a function returned by [scales::extended_breaks()]). +#' Also accepts rlang [lambda][rlang::as_function()] function notation. #' @param minor_breaks One of: #' - `NULL` for no minor breaks #' - `waiver()` for the default breaks (one minor break between #' each major break) #' - A numeric vector of positions -#' - A function that given the limits returns a vector of minor breaks. +#' - A function that given the limits returns a vector of minor breaks. Also +#' accepts rlang [lambda][rlang::as_function()] function notation. #' @param n.breaks An integer guiding the number of major breaks. The algorithm #' may choose a slightly different number to ensure nice break labels. Will #' only have an effect if `breaks = waiver()`. Use `NULL` to use the default @@ -35,13 +37,15 @@ #' transformation object #' - A character vector giving labels (must be same length as `breaks`) #' - A function that takes the breaks as input and returns labels -#' as output +#' as output. Also accepts rlang [lambda][rlang::as_function()] function +#' notation. #' @param limits One of: #' - `NULL` to use the default scale range #' - A numeric vector of length two providing limits of the scale. #' Use `NA` to refer to the existing minimum or maximum #' - A function that accepts the existing (automatic) limits and returns -#' new limits +#' new limits. Also accepts rlang [lambda][rlang::as_function()] function +#' notation. #' Note that setting limits on positional scales will **remove** data outside of the limits. #' If the purpose is to zoom, use the limit argument in the coordinate system #' (see [coord_cartesian()]). @@ -49,10 +53,12 @@ #' range \[0, 1]. This is always [scales::rescale()], except for #' diverging and n colour gradients (i.e., [scale_colour_gradient2()], #' [scale_colour_gradientn()]). The `rescaler` is ignored by position -#' scales, which always use [scales::rescale()]. +#' scales, which always use [scales::rescale()]. Also accepts rlang +#' [lambda][rlang::as_function()] function notation. #' @param oob One of: #' - Function that handles limits outside of the scale limits -#' (out of bounds). +#' (out of bounds). Also accepts rlang [lambda][rlang::as_function()] +#' function notation. #' - The default ([scales::censor()]) replaces out of #' bounds values with `NA`. #' - [scales::squish()] for squishing out of bounds values into range. @@ -104,6 +110,14 @@ continuous_scale <- function(aesthetics, scale_name, palette, name = waiver(), limits <- trans$transform(limits) } + # Convert formula to function if appropriate + limits <- allow_lambda(limits) + breaks <- allow_lambda(breaks) + labels <- allow_lambda(labels) + rescaler <- allow_lambda(rescaler) + oob <- allow_lambda(oob) + minor_breaks <- allow_lambda(minor_breaks) + ggproto(NULL, super, call = match.call(), @@ -142,13 +156,15 @@ continuous_scale <- function(aesthetics, scale_name, palette, name = waiver(), #' - `waiver()` for the default breaks (the scale limits) #' - A character vector of breaks #' - A function that takes the limits as input and returns breaks -#' as output +#' as output. Also accepts rlang [lambda][rlang::as_function()] function +#' notation. #' @param limits One of: #' - `NULL` to use the default scale values #' - A character vector that defines possible values of the scale and their #' order #' - A function that accepts the existing (automatic) values and returns -#' new ones +#' new ones. Also accepts rlang [lambda][rlang::as_function()] function +#' notation. #' @param drop Should unused factor levels be omitted from the scale? #' The default, `TRUE`, uses the levels that appear in the data; #' `FALSE` uses all the levels in the factor. @@ -168,6 +184,11 @@ discrete_scale <- function(aesthetics, scale_name, palette, name = waiver(), check_breaks_labels(breaks, labels) + # Convert formula input to function if appropriate + limits <- allow_lambda(limits) + breaks <- allow_lambda(breaks) + labels <- allow_lambda(labels) + if (!is.function(limits) && (length(limits) > 0) && !is.discrete(limits)) { warn( glue( @@ -217,7 +238,7 @@ discrete_scale <- function(aesthetics, scale_name, palette, name = waiver(), #' instead of exactly evenly spaced between the limits. If `TRUE` (default) #' the scale will ask the transformation object to create breaks, and this #' may result in a different number of breaks than requested. Ignored if -#' breaks are given explicetly. +#' breaks are given explicitly. #' @param right Should values on the border between bins be part of the right #' (upper) bin? #' @param show.limits should the limits of the scale appear as ticks @@ -244,6 +265,13 @@ binned_scale <- function(aesthetics, scale_name, palette, name = waiver(), limits <- trans$transform(limits) } + # Convert formula input to function if appropriate + limits <- allow_lambda(limits) + breaks <- allow_lambda(breaks) + labels <- allow_lambda(labels) + rescaler <- allow_lambda(rescaler) + oob <- allow_lambda(oob) + ggproto(NULL, super, call = match.call(), @@ -1167,3 +1195,7 @@ check_transformation <- function(x, transformed, name, axis) { trans_support_nbreaks <- function(trans) { "n" %in% names(formals(trans$breaks)) } + +allow_lambda <- function(x) { + if (is_formula(x)) as_function(x) else x +} diff --git a/man/binned_scale.Rd b/man/binned_scale.Rd index df1eaa5b49..951ac64858 100644 --- a/man/binned_scale.Rd +++ b/man/binned_scale.Rd @@ -48,7 +48,8 @@ omitted.} \link[scales:trans_new]{transformation object} \item A numeric vector of positions \item A function that takes the limits as input and returns breaks -as output (e.g., a function returned by \code{\link[scales:breaks_extended]{scales::extended_breaks()}}) +as output (e.g., a function returned by \code{\link[scales:breaks_extended]{scales::extended_breaks()}}). +Also accepts rlang \link[rlang:as_function]{lambda} function notation. }} \item{labels}{One of: @@ -58,7 +59,8 @@ as output (e.g., a function returned by \code{\link[scales:breaks_extended]{scal transformation object \item A character vector giving labels (must be same length as \code{breaks}) \item A function that takes the breaks as input and returns labels -as output +as output. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{limits}{One of: @@ -67,7 +69,8 @@ as output \item A numeric vector of length two providing limits of the scale. Use \code{NA} to refer to the existing minimum or maximum \item A function that accepts the existing (automatic) limits and returns -new limits +new limits. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. Note that setting limits on positional scales will \strong{remove} data outside of the limits. If the purpose is to zoom, use the limit argument in the coordinate system (see \code{\link[=coord_cartesian]{coord_cartesian()}}). @@ -77,12 +80,14 @@ If the purpose is to zoom, use the limit argument in the coordinate system range [0, 1]. This is always \code{\link[scales:rescale]{scales::rescale()}}, except for diverging and n colour gradients (i.e., \code{\link[=scale_colour_gradient2]{scale_colour_gradient2()}}, \code{\link[=scale_colour_gradientn]{scale_colour_gradientn()}}). The \code{rescaler} is ignored by position -scales, which always use \code{\link[scales:rescale]{scales::rescale()}}.} +scales, which always use \code{\link[scales:rescale]{scales::rescale()}}. Also accepts rlang +\link[rlang:as_function]{lambda} function notation.} \item{oob}{One of: \itemize{ \item Function that handles limits outside of the scale limits -(out of bounds). +(out of bounds). Also accepts rlang \link[rlang:as_function]{lambda} +function notation. \item The default (\code{\link[scales:oob]{scales::censor()}}) replaces out of bounds values with \code{NA}. \item \code{\link[scales:oob]{scales::squish()}} for squishing out of bounds values into range. @@ -105,7 +110,7 @@ directly.} instead of exactly evenly spaced between the limits. If \code{TRUE} (default) the scale will ask the transformation object to create breaks, and this may result in a different number of breaks than requested. Ignored if -breaks are given explicetly.} +breaks are given explicitly.} \item{right}{Should values on the border between bins be part of the right (upper) bin?} diff --git a/man/continuous_scale.Rd b/man/continuous_scale.Rd index e8effb6aaa..5f55695097 100644 --- a/man/continuous_scale.Rd +++ b/man/continuous_scale.Rd @@ -46,7 +46,8 @@ omitted.} \link[scales:trans_new]{transformation object} \item A numeric vector of positions \item A function that takes the limits as input and returns breaks -as output (e.g., a function returned by \code{\link[scales:breaks_extended]{scales::extended_breaks()}}) +as output (e.g., a function returned by \code{\link[scales:breaks_extended]{scales::extended_breaks()}}). +Also accepts rlang \link[rlang:as_function]{lambda} function notation. }} \item{minor_breaks}{One of: @@ -55,7 +56,8 @@ as output (e.g., a function returned by \code{\link[scales:breaks_extended]{scal \item \code{waiver()} for the default breaks (one minor break between each major break) \item A numeric vector of positions -\item A function that given the limits returns a vector of minor breaks. +\item A function that given the limits returns a vector of minor breaks. Also +accepts rlang \link[rlang:as_function]{lambda} function notation. }} \item{n.breaks}{An integer guiding the number of major breaks. The algorithm @@ -70,7 +72,8 @@ number of breaks given by the transformation.} transformation object \item A character vector giving labels (must be same length as \code{breaks}) \item A function that takes the breaks as input and returns labels -as output +as output. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{limits}{One of: @@ -79,7 +82,8 @@ as output \item A numeric vector of length two providing limits of the scale. Use \code{NA} to refer to the existing minimum or maximum \item A function that accepts the existing (automatic) limits and returns -new limits +new limits. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. Note that setting limits on positional scales will \strong{remove} data outside of the limits. If the purpose is to zoom, use the limit argument in the coordinate system (see \code{\link[=coord_cartesian]{coord_cartesian()}}). @@ -89,12 +93,14 @@ If the purpose is to zoom, use the limit argument in the coordinate system range [0, 1]. This is always \code{\link[scales:rescale]{scales::rescale()}}, except for diverging and n colour gradients (i.e., \code{\link[=scale_colour_gradient2]{scale_colour_gradient2()}}, \code{\link[=scale_colour_gradientn]{scale_colour_gradientn()}}). The \code{rescaler} is ignored by position -scales, which always use \code{\link[scales:rescale]{scales::rescale()}}.} +scales, which always use \code{\link[scales:rescale]{scales::rescale()}}. Also accepts rlang +\link[rlang:as_function]{lambda} function notation.} \item{oob}{One of: \itemize{ \item Function that handles limits outside of the scale limits -(out of bounds). +(out of bounds). Also accepts rlang \link[rlang:as_function]{lambda} +function notation. \item The default (\code{\link[scales:oob]{scales::censor()}}) replaces out of bounds values with \code{NA}. \item \code{\link[scales:oob]{scales::squish()}} for squishing out of bounds values into range. diff --git a/man/discrete_scale.Rd b/man/discrete_scale.Rd index a50a4f1ba8..456361bb61 100644 --- a/man/discrete_scale.Rd +++ b/man/discrete_scale.Rd @@ -42,7 +42,8 @@ omitted.} \item \code{waiver()} for the default breaks (the scale limits) \item A character vector of breaks \item A function that takes the limits as input and returns breaks -as output +as output. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{labels}{One of: @@ -52,7 +53,8 @@ as output transformation object \item A character vector giving labels (must be same length as \code{breaks}) \item A function that takes the breaks as input and returns labels -as output +as output. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{limits}{One of: @@ -61,7 +63,8 @@ as output \item A character vector that defines possible values of the scale and their order \item A function that accepts the existing (automatic) values and returns -new ones +new ones. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{expand}{For position scales, a vector of range expansion constants used to add some diff --git a/man/scale_binned.Rd b/man/scale_binned.Rd index 4aa542f23e..f2a34bfbb1 100644 --- a/man/scale_binned.Rd +++ b/man/scale_binned.Rd @@ -52,7 +52,7 @@ directly.} instead of exactly evenly spaced between the limits. If \code{TRUE} (default) the scale will ask the transformation object to create breaks, and this may result in a different number of breaks than requested. Ignored if -breaks are given explicetly.} +breaks are given explicitly.} \item{breaks}{One of: \itemize{ @@ -61,7 +61,8 @@ breaks are given explicetly.} \link[scales:trans_new]{transformation object} \item A numeric vector of positions \item A function that takes the limits as input and returns breaks -as output (e.g., a function returned by \code{\link[scales:breaks_extended]{scales::extended_breaks()}}) +as output (e.g., a function returned by \code{\link[scales:breaks_extended]{scales::extended_breaks()}}). +Also accepts rlang \link[rlang:as_function]{lambda} function notation. }} \item{labels}{One of: @@ -71,7 +72,8 @@ as output (e.g., a function returned by \code{\link[scales:breaks_extended]{scal transformation object \item A character vector giving labels (must be same length as \code{breaks}) \item A function that takes the breaks as input and returns labels -as output +as output. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{limits}{One of: @@ -80,7 +82,8 @@ as output \item A numeric vector of length two providing limits of the scale. Use \code{NA} to refer to the existing minimum or maximum \item A function that accepts the existing (automatic) limits and returns -new limits +new limits. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. Note that setting limits on positional scales will \strong{remove} data outside of the limits. If the purpose is to zoom, use the limit argument in the coordinate system (see \code{\link[=coord_cartesian]{coord_cartesian()}}). @@ -96,7 +99,8 @@ expand the scale by 5\% on each side for continuous variables, and by \item{oob}{One of: \itemize{ \item Function that handles limits outside of the scale limits -(out of bounds). +(out of bounds). Also accepts rlang \link[rlang:as_function]{lambda} +function notation. \item The default (\code{\link[scales:oob]{scales::censor()}}) replaces out of bounds values with \code{NA}. \item \code{\link[scales:oob]{scales::squish()}} for squishing out of bounds values into range. diff --git a/man/scale_continuous.Rd b/man/scale_continuous.Rd index 6eceed116d..926885a2b8 100644 --- a/man/scale_continuous.Rd +++ b/man/scale_continuous.Rd @@ -68,7 +68,8 @@ omitted.} \link[scales:trans_new]{transformation object} \item A numeric vector of positions \item A function that takes the limits as input and returns breaks -as output (e.g., a function returned by \code{\link[scales:breaks_extended]{scales::extended_breaks()}}) +as output (e.g., a function returned by \code{\link[scales:breaks_extended]{scales::extended_breaks()}}). +Also accepts rlang \link[rlang:as_function]{lambda} function notation. }} \item{minor_breaks}{One of: @@ -77,7 +78,8 @@ as output (e.g., a function returned by \code{\link[scales:breaks_extended]{scal \item \code{waiver()} for the default breaks (one minor break between each major break) \item A numeric vector of positions -\item A function that given the limits returns a vector of minor breaks. +\item A function that given the limits returns a vector of minor breaks. Also +accepts rlang \link[rlang:as_function]{lambda} function notation. }} \item{n.breaks}{An integer guiding the number of major breaks. The algorithm @@ -92,7 +94,8 @@ number of breaks given by the transformation.} transformation object \item A character vector giving labels (must be same length as \code{breaks}) \item A function that takes the breaks as input and returns labels -as output +as output. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{limits}{One of: @@ -101,7 +104,8 @@ as output \item A numeric vector of length two providing limits of the scale. Use \code{NA} to refer to the existing minimum or maximum \item A function that accepts the existing (automatic) limits and returns -new limits +new limits. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. Note that setting limits on positional scales will \strong{remove} data outside of the limits. If the purpose is to zoom, use the limit argument in the coordinate system (see \code{\link[=coord_cartesian]{coord_cartesian()}}). @@ -117,7 +121,8 @@ expand the scale by 5\% on each side for continuous variables, and by \item{oob}{One of: \itemize{ \item Function that handles limits outside of the scale limits -(out of bounds). +(out of bounds). Also accepts rlang \link[rlang:as_function]{lambda} +function notation. \item The default (\code{\link[scales:oob]{scales::censor()}}) replaces out of bounds values with \code{NA}. \item \code{\link[scales:oob]{scales::squish()}} for squishing out of bounds values into range. diff --git a/man/scale_date.Rd b/man/scale_date.Rd index 6a89828023..f4ccbb0f49 100644 --- a/man/scale_date.Rd +++ b/man/scale_date.Rd @@ -128,7 +128,8 @@ specified, \code{date_breaks} wins.} transformation object \item A character vector giving labels (must be same length as \code{breaks}) \item A function that takes the breaks as input and returns labels -as output +as output. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{date_labels}{A string giving the formatting specification for the @@ -154,7 +155,8 @@ like "2 weeks", or "10 years". If both \code{minor_breaks} and \item A numeric vector of length two providing limits of the scale. Use \code{NA} to refer to the existing minimum or maximum \item A function that accepts the existing (automatic) limits and returns -new limits +new limits. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. Note that setting limits on positional scales will \strong{remove} data outside of the limits. If the purpose is to zoom, use the limit argument in the coordinate system (see \code{\link[=coord_cartesian]{coord_cartesian()}}). @@ -170,7 +172,8 @@ expand the scale by 5\% on each side for continuous variables, and by \item{oob}{One of: \itemize{ \item Function that handles limits outside of the scale limits -(out of bounds). +(out of bounds). Also accepts rlang \link[rlang:as_function]{lambda} +function notation. \item The default (\code{\link[scales:oob]{scales::censor()}}) replaces out of bounds values with \code{NA}. \item \code{\link[scales:oob]{scales::squish()}} for squishing out of bounds values into range. diff --git a/man/scale_discrete.Rd b/man/scale_discrete.Rd index ed83875716..b7c901a2d6 100644 --- a/man/scale_discrete.Rd +++ b/man/scale_discrete.Rd @@ -22,7 +22,8 @@ they should take (e.g., \code{\link[scales:hue_pal]{scales::hue_pal()}}).} \item \code{waiver()} for the default breaks (the scale limits) \item A character vector of breaks \item A function that takes the limits as input and returns breaks -as output +as output. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{\code{limits}}{One of: \itemize{ @@ -30,7 +31,8 @@ as output \item A character vector that defines possible values of the scale and their order \item A function that accepts the existing (automatic) values and returns -new ones +new ones. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{\code{drop}}{Should unused factor levels be omitted from the scale? The default, \code{TRUE}, uses the levels that appear in the data; @@ -55,7 +57,8 @@ omitted.} transformation object \item A character vector giving labels (must be same length as \code{breaks}) \item A function that takes the breaks as input and returns labels -as output +as output. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{\code{super}}{The super class to use for the constructed scale} }} diff --git a/man/scale_gradient.Rd b/man/scale_gradient.Rd index af7720a1e9..bbb946f37a 100644 --- a/man/scale_gradient.Rd +++ b/man/scale_gradient.Rd @@ -104,7 +104,8 @@ omitted.} \link[scales:trans_new]{transformation object} \item A numeric vector of positions \item A function that takes the limits as input and returns breaks -as output (e.g., a function returned by \code{\link[scales:breaks_extended]{scales::extended_breaks()}}) +as output (e.g., a function returned by \code{\link[scales:breaks_extended]{scales::extended_breaks()}}). +Also accepts rlang \link[rlang:as_function]{lambda} function notation. }} \item{\code{minor_breaks}}{One of: \itemize{ @@ -112,7 +113,8 @@ as output (e.g., a function returned by \code{\link[scales:breaks_extended]{scal \item \code{waiver()} for the default breaks (one minor break between each major break) \item A numeric vector of positions -\item A function that given the limits returns a vector of minor breaks. +\item A function that given the limits returns a vector of minor breaks. Also +accepts rlang \link[rlang:as_function]{lambda} function notation. }} \item{\code{n.breaks}}{An integer guiding the number of major breaks. The algorithm may choose a slightly different number to ensure nice break labels. Will @@ -125,7 +127,8 @@ number of breaks given by the transformation.} transformation object \item A character vector giving labels (must be same length as \code{breaks}) \item A function that takes the breaks as input and returns labels -as output +as output. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{\code{limits}}{One of: \itemize{ @@ -133,7 +136,8 @@ as output \item A numeric vector of length two providing limits of the scale. Use \code{NA} to refer to the existing minimum or maximum \item A function that accepts the existing (automatic) limits and returns -new limits +new limits. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. Note that setting limits on positional scales will \strong{remove} data outside of the limits. If the purpose is to zoom, use the limit argument in the coordinate system (see \code{\link[=coord_cartesian]{coord_cartesian()}}). @@ -142,11 +146,13 @@ If the purpose is to zoom, use the limit argument in the coordinate system range [0, 1]. This is always \code{\link[scales:rescale]{scales::rescale()}}, except for diverging and n colour gradients (i.e., \code{\link[=scale_colour_gradient2]{scale_colour_gradient2()}}, \code{\link[=scale_colour_gradientn]{scale_colour_gradientn()}}). The \code{rescaler} is ignored by position -scales, which always use \code{\link[scales:rescale]{scales::rescale()}}.} +scales, which always use \code{\link[scales:rescale]{scales::rescale()}}. Also accepts rlang +\link[rlang:as_function]{lambda} function notation.} \item{\code{oob}}{One of: \itemize{ \item Function that handles limits outside of the scale limits -(out of bounds). +(out of bounds). Also accepts rlang \link[rlang:as_function]{lambda} +function notation. \item The default (\code{\link[scales:oob]{scales::censor()}}) replaces out of bounds values with \code{NA}. \item \code{\link[scales:oob]{scales::squish()}} for squishing out of bounds values into range. diff --git a/man/scale_grey.Rd b/man/scale_grey.Rd index a6812aafc7..38040f9c27 100644 --- a/man/scale_grey.Rd +++ b/man/scale_grey.Rd @@ -4,8 +4,6 @@ \alias{scale_colour_grey} \alias{scale_fill_grey} \alias{scale_color_grey} -\alias{scale_color_gray} -\alias{scale_fill_gray} \title{Sequential grey colour scales} \usage{ scale_colour_grey( @@ -37,7 +35,8 @@ they should take (e.g., \code{\link[scales:hue_pal]{scales::hue_pal()}}).} \item \code{waiver()} for the default breaks (the scale limits) \item A character vector of breaks \item A function that takes the limits as input and returns breaks -as output +as output. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{\code{limits}}{One of: \itemize{ @@ -45,7 +44,8 @@ as output \item A character vector that defines possible values of the scale and their order \item A function that accepts the existing (automatic) values and returns -new ones +new ones. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{\code{drop}}{Should unused factor levels be omitted from the scale? The default, \code{TRUE}, uses the levels that appear in the data; @@ -66,7 +66,8 @@ omitted.} transformation object \item A character vector giving labels (must be same length as \code{breaks}) \item A function that takes the breaks as input and returns labels -as output +as output. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{\code{expand}}{For position scales, a vector of range expansion constants used to add some padding around the data to ensure that they are placed some distance diff --git a/man/scale_hue.Rd b/man/scale_hue.Rd index dc0699eceb..8c67a32a3e 100644 --- a/man/scale_hue.Rd +++ b/man/scale_hue.Rd @@ -41,7 +41,8 @@ they should take (e.g., \code{\link[scales:hue_pal]{scales::hue_pal()}}).} \item \code{waiver()} for the default breaks (the scale limits) \item A character vector of breaks \item A function that takes the limits as input and returns breaks -as output +as output. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{\code{limits}}{One of: \itemize{ @@ -49,7 +50,8 @@ as output \item A character vector that defines possible values of the scale and their order \item A function that accepts the existing (automatic) values and returns -new ones +new ones. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{\code{drop}}{Should unused factor levels be omitted from the scale? The default, \code{TRUE}, uses the levels that appear in the data; @@ -70,7 +72,8 @@ omitted.} transformation object \item A character vector giving labels (must be same length as \code{breaks}) \item A function that takes the breaks as input and returns labels -as output +as output. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{\code{expand}}{For position scales, a vector of range expansion constants used to add some padding around the data to ensure that they are placed some distance diff --git a/man/scale_linetype.Rd b/man/scale_linetype.Rd index 9c86ef29a2..05c0d84193 100644 --- a/man/scale_linetype.Rd +++ b/man/scale_linetype.Rd @@ -28,7 +28,8 @@ they should take (e.g., \code{\link[scales:hue_pal]{scales::hue_pal()}}).} \item \code{waiver()} for the default breaks (the scale limits) \item A character vector of breaks \item A function that takes the limits as input and returns breaks -as output +as output. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{\code{limits}}{One of: \itemize{ @@ -36,7 +37,8 @@ as output \item A character vector that defines possible values of the scale and their order \item A function that accepts the existing (automatic) values and returns -new ones +new ones. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{\code{drop}}{Should unused factor levels be omitted from the scale? The default, \code{TRUE}, uses the levels that appear in the data; @@ -58,7 +60,8 @@ omitted.} transformation object \item A character vector giving labels (must be same length as \code{breaks}) \item A function that takes the breaks as input and returns labels -as output +as output. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{\code{guide}}{A function used to create a guide or its name. See \code{\link[=guides]{guides()}} for more information.} diff --git a/man/scale_manual.Rd b/man/scale_manual.Rd index feb2aa068c..ad9a742df3 100644 --- a/man/scale_manual.Rd +++ b/man/scale_manual.Rd @@ -38,7 +38,8 @@ they should take (e.g., \code{\link[scales:hue_pal]{scales::hue_pal()}}).} \item A character vector that defines possible values of the scale and their order \item A function that accepts the existing (automatic) values and returns -new ones +new ones. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{\code{drop}}{Should unused factor levels be omitted from the scale? The default, \code{TRUE}, uses the levels that appear in the data; @@ -62,7 +63,8 @@ omitted.} transformation object \item A character vector giving labels (must be same length as \code{breaks}) \item A function that takes the breaks as input and returns labels -as output +as output. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{\code{guide}}{A function used to create a guide or its name. See \code{\link[=guides]{guides()}} for more information.} diff --git a/man/scale_shape.Rd b/man/scale_shape.Rd index 8a627b4f5c..ccf0906ac2 100644 --- a/man/scale_shape.Rd +++ b/man/scale_shape.Rd @@ -25,7 +25,8 @@ they should take (e.g., \code{\link[scales:hue_pal]{scales::hue_pal()}}).} \item \code{waiver()} for the default breaks (the scale limits) \item A character vector of breaks \item A function that takes the limits as input and returns breaks -as output +as output. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{\code{limits}}{One of: \itemize{ @@ -33,7 +34,8 @@ as output \item A character vector that defines possible values of the scale and their order \item A function that accepts the existing (automatic) values and returns -new ones +new ones. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{\code{drop}}{Should unused factor levels be omitted from the scale? The default, \code{TRUE}, uses the levels that appear in the data; @@ -58,7 +60,8 @@ omitted.} transformation object \item A character vector giving labels (must be same length as \code{breaks}) \item A function that takes the breaks as input and returns labels -as output +as output. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{\code{guide}}{A function used to create a guide or its name. See \code{\link[=guides]{guides()}} for more information.} diff --git a/man/scale_size.Rd b/man/scale_size.Rd index c0c7646e78..649267b2e8 100644 --- a/man/scale_size.Rd +++ b/man/scale_size.Rd @@ -62,7 +62,8 @@ omitted.} \link[scales:trans_new]{transformation object} \item A numeric vector of positions \item A function that takes the limits as input and returns breaks -as output (e.g., a function returned by \code{\link[scales:breaks_extended]{scales::extended_breaks()}}) +as output (e.g., a function returned by \code{\link[scales:breaks_extended]{scales::extended_breaks()}}). +Also accepts rlang \link[rlang:as_function]{lambda} function notation. }} \item{labels}{One of: @@ -72,7 +73,8 @@ as output (e.g., a function returned by \code{\link[scales:breaks_extended]{scal transformation object \item A character vector giving labels (must be same length as \code{breaks}) \item A function that takes the breaks as input and returns labels -as output +as output. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{limits}{One of: @@ -81,7 +83,8 @@ as output \item A numeric vector of length two providing limits of the scale. Use \code{NA} to refer to the existing minimum or maximum \item A function that accepts the existing (automatic) limits and returns -new limits +new limits. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. Note that setting limits on positional scales will \strong{remove} data outside of the limits. If the purpose is to zoom, use the limit argument in the coordinate system (see \code{\link[=coord_cartesian]{coord_cartesian()}}). @@ -114,7 +117,7 @@ number of breaks given by the transformation.} instead of exactly evenly spaced between the limits. If \code{TRUE} (default) the scale will ask the transformation object to create breaks, and this may result in a different number of breaks than requested. Ignored if -breaks are given explicetly.} +breaks are given explicitly.} \item{...}{ Arguments passed on to \code{\link[=continuous_scale]{continuous_scale}} @@ -125,12 +128,14 @@ breaks are given explicetly.} \item \code{waiver()} for the default breaks (one minor break between each major break) \item A numeric vector of positions -\item A function that given the limits returns a vector of minor breaks. +\item A function that given the limits returns a vector of minor breaks. Also +accepts rlang \link[rlang:as_function]{lambda} function notation. }} \item{\code{oob}}{One of: \itemize{ \item Function that handles limits outside of the scale limits -(out of bounds). +(out of bounds). Also accepts rlang \link[rlang:as_function]{lambda} +function notation. \item The default (\code{\link[scales:oob]{scales::censor()}}) replaces out of bounds values with \code{NA}. \item \code{\link[scales:oob]{scales::squish()}} for squishing out of bounds values into range. diff --git a/man/scale_steps.Rd b/man/scale_steps.Rd index fa57f6a198..4437c4928f 100644 --- a/man/scale_steps.Rd +++ b/man/scale_steps.Rd @@ -88,7 +88,7 @@ directly.} instead of exactly evenly spaced between the limits. If \code{TRUE} (default) the scale will ask the transformation object to create breaks, and this may result in a different number of breaks than requested. Ignored if -breaks are given explicetly.} +breaks are given explicitly.} \item{\code{right}}{Should values on the border between bins be part of the right (upper) bin?} \item{\code{show.limits}}{should the limits of the scale appear as ticks} @@ -103,7 +103,8 @@ omitted.} \link[scales:trans_new]{transformation object} \item A numeric vector of positions \item A function that takes the limits as input and returns breaks -as output (e.g., a function returned by \code{\link[scales:breaks_extended]{scales::extended_breaks()}}) +as output (e.g., a function returned by \code{\link[scales:breaks_extended]{scales::extended_breaks()}}). +Also accepts rlang \link[rlang:as_function]{lambda} function notation. }} \item{\code{labels}}{One of: \itemize{ @@ -112,7 +113,8 @@ as output (e.g., a function returned by \code{\link[scales:breaks_extended]{scal transformation object \item A character vector giving labels (must be same length as \code{breaks}) \item A function that takes the breaks as input and returns labels -as output +as output. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. }} \item{\code{limits}}{One of: \itemize{ @@ -120,7 +122,8 @@ as output \item A numeric vector of length two providing limits of the scale. Use \code{NA} to refer to the existing minimum or maximum \item A function that accepts the existing (automatic) limits and returns -new limits +new limits. Also accepts rlang \link[rlang:as_function]{lambda} function +notation. Note that setting limits on positional scales will \strong{remove} data outside of the limits. If the purpose is to zoom, use the limit argument in the coordinate system (see \code{\link[=coord_cartesian]{coord_cartesian()}}). @@ -128,7 +131,8 @@ If the purpose is to zoom, use the limit argument in the coordinate system \item{\code{oob}}{One of: \itemize{ \item Function that handles limits outside of the scale limits -(out of bounds). +(out of bounds). Also accepts rlang \link[rlang:as_function]{lambda} +function notation. \item The default (\code{\link[scales:oob]{scales::censor()}}) replaces out of bounds values with \code{NA}. \item \code{\link[scales:oob]{scales::squish()}} for squishing out of bounds values into range. diff --git a/tests/testthat/test-scales.r b/tests/testthat/test-scales.r index e1e5be83dd..c0982662f2 100644 --- a/tests/testthat/test-scales.r +++ b/tests/testthat/test-scales.r @@ -382,3 +382,50 @@ test_that("All scale_colour_*() have their American versions", { sub("color", "colour", color_scale_exports) ) }) + +test_that("scales accept lambda notation for function input", { + check_lambda <- function(items, ggproto) { + vapply(items, function(x) { + f <- environment(ggproto[[x]])$f + is_lambda(f) + }, logical(1)) + } + + # Test continuous scale + scale <- scale_fill_gradient( + limits = ~ .x + c(-1, 1), + breaks = ~ seq(.x[1], .x[2], by = 2), + minor_breaks = ~ seq(.x[1], .x[2], by = 1), + labels = ~ toupper(.x), + rescaler = ~ rescale_mid(.x, mid = 0), + oob = ~ oob_squish(.x, .y, only.finite = FALSE) + ) + check <- check_lambda( + c("limits", "breaks", "minor_breaks", "labels", "rescaler"), + scale + ) + expect_true(all(check)) + + # Test discrete scale + scale <- scale_x_discrete( + limits = ~ rev(.x), + breaks = ~ .x[-1], + labels = ~ toupper(.x) + ) + check <- check_lambda(c("limits", "breaks", "labels"), scale) + expect_true(all(check)) + + # Test binned scale + scale <- scale_fill_steps( + limits = ~ .x + c(-1, 1), + breaks = ~ seq(.x[1], .x[2], by = 2), + labels = ~ toupper(.x), + rescaler = ~ rescale_mid(.x, mid = 0), + oob = ~ oob_squish(.x, .y, only.finite = FALSE) + ) + check <- check_lambda( + c("limits", "breaks", "labels", "rescaler"), + scale + ) + expect_true(all(check)) +})