diff --git a/NEWS.md b/NEWS.md index 3e81319b84..6b8d946391 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,8 @@ * `annotation_raster()` adds support for native rasters. For large rasters, native rasters render significantly faster than arrays (@kent37, #3388) + +* Default continuous color scales (i.e., the `options()` `ggplot2.continuous.colour` and `ggplot2.continuous.fill`, which inform the `type` argument of `scale_fill_continuous()` and `scale_colour_continuous()`) now accept a function, which allows more control over these default `continuous_scale()`s (@cpsievert, #3827) # ggplot2 3.3.0 diff --git a/R/scale-colour.r b/R/scale-colour.r index fd9cc2f1f4..262b8cfbfb 100644 --- a/R/scale-colour.r +++ b/R/scale-colour.r @@ -1,13 +1,15 @@ #' Continuous and binned colour scales #' #' Colour scales for continuous data default to the values of the -#' `ggplot2.continuous.colour` and `ggplot2.continuous.fill` options. If these -#' options are not present, `"gradient"` will be used. See [options()] for more -#' information. +#' `ggplot2.continuous.colour` and `ggplot2.continuous.fill` options. These +#' [options()] default to `"gradient"` (i.e., [scale_colour_gradient()] and +#' [scale_fill_gradient()]) #' #' @param ... Additional parameters passed on to the scale type -#' @param type One of "gradient" (the default) or "viridis" indicating the -#' colour scale to use +#' @param type One of the following: +#' * "gradient" (the default) +#' * "viridis" +#' * A function that returns a continuous colour scale. #' @seealso [scale_colour_gradient()], [scale_colour_viridis_c()], #' [scale_colour_steps()], [scale_colour_viridis_b()], [scale_fill_gradient()], #' [scale_fill_viridis_c()], [scale_fill_steps()], and [scale_fill_viridis_b()] @@ -40,48 +42,60 @@ #' v + scale_fill_viridis_c() scale_colour_continuous <- function(..., type = getOption("ggplot2.continuous.colour", default = "gradient")) { - switch( - type, - gradient = scale_colour_gradient(...), - viridis = scale_colour_viridis_c(...), + if (is.function(type)) { + type(...) + } else if (identical(type, "gradient")) { + scale_colour_gradient(...) + } else if (identical(type, "viridis")) { + scale_colour_viridis_c(...) + } else { abort("Unknown scale type") - ) + } } #' @rdname scale_colour_continuous #' @export scale_fill_continuous <- function(..., type = getOption("ggplot2.continuous.fill", default = "gradient")) { - switch( - type, - gradient = scale_fill_gradient(...), - viridis = scale_fill_viridis_c(...), + if (is.function(type)) { + type(...) + } else if (identical(type, "gradient")) { + scale_fill_gradient(...) + } else if (identical(type, "viridis")) { + scale_fill_viridis_c(...) + } else { abort("Unknown scale type") - ) + } } #' @export #' @rdname scale_colour_continuous #' @usage NULL scale_colour_binned <- function(..., - type = getOption("ggplot2.continuous.colour", default = "gradient")) { - switch( - type, - gradient = scale_colour_steps(...), - viridis = scale_colour_viridis_b(...), + type = getOption("ggplot2.binned.colour", default = getOption("ggplot2.continuous.colour", default = "gradient"))) { + if (is.function(type)) { + type(...) + } else if (identical(type, "gradient")) { + scale_colour_steps(...) + } else if (identical(type, "viridis")) { + scale_colour_viridis_b(...) + } else { abort("Unknown scale type") - ) + } } #' @export #' @rdname scale_colour_continuous #' @usage NULL scale_fill_binned <- function(..., - type = getOption("ggplot2.continuous.colour", default = "gradient")) { - switch( - type, - gradient = scale_fill_steps(...), - viridis = scale_fill_viridis_b(...), + type = getOption("ggplot2.binned.fill", default = getOption("ggplot2.continuous.fill", default = "gradient"))) { + if (is.function(type)) { + type(...) + } else if (identical(type, "gradient")) { + scale_fill_steps(...) + } else if (identical(type, "viridis")) { + scale_fill_viridis_b(...) + } else { abort("Unknown scale type") - ) + } } diff --git a/man/scale_colour_continuous.Rd b/man/scale_colour_continuous.Rd index 72718862c0..816581b7df 100644 --- a/man/scale_colour_continuous.Rd +++ b/man/scale_colour_continuous.Rd @@ -22,14 +22,18 @@ scale_fill_continuous( \arguments{ \item{...}{Additional parameters passed on to the scale type} -\item{type}{One of "gradient" (the default) or "viridis" indicating the -colour scale to use} +\item{type}{One of the following: +\itemize{ +\item "gradient" (the default) +\item "viridis" +\item A function that returns a continuous colour scale. +}} } \description{ Colour scales for continuous data default to the values of the -\code{ggplot2.continuous.colour} and \code{ggplot2.continuous.fill} options. If these -options are not present, \code{"gradient"} will be used. See \code{\link[=options]{options()}} for more -information. +\code{ggplot2.continuous.colour} and \code{ggplot2.continuous.fill} options. These +\code{\link[=options]{options()}} default to \code{"gradient"} (i.e., \code{\link[=scale_colour_gradient]{scale_colour_gradient()}} and +\code{\link[=scale_fill_gradient]{scale_fill_gradient()}}) } \section{Color Blindness}{