Skip to content

Type argument in scale_[fill/colour]_[continuous/binned] can accept a function #3828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
68 changes: 41 additions & 27 deletions R/scale-colour.r
Original file line number Diff line number Diff line change
@@ -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()]
Expand Down Expand Up @@ -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")
)
}
}
14 changes: 9 additions & 5 deletions man/scale_colour_continuous.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.