-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add built-in support for viridis color scales #2178
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
Changes from all commits
3942e30
e7174ff
92d92d7
d868d8e
ff59231
6f55d9f
101397b
8bd3b7c
6920076
769c515
94582b8
eaf3ddf
d76ac85
dc77651
ec3ae12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#' Continuous 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. | ||
#' | ||
#' @param ... Additional parameters passed on to the scale type | ||
#' @param type One of "gradient" (the default) or "viridis" indicating the | ||
#' colour scale to use | ||
#' @seealso [scale_colour_gradient()], [scale_colour_viridis_c()], | ||
#' [scale_fill_gradient()], and [scale_fill_viridis_c()] | ||
#' @export | ||
#' @rdname scale_colour_continuous | ||
#' @examples | ||
#' v <- ggplot(faithfuld, aes(waiting, eruptions, fill = density)) + | ||
#' geom_tile() | ||
#' v | ||
#' | ||
#' v + scale_fill_continuous(type = "gradient") | ||
#' v + scale_fill_continuous(type = "viridis") | ||
#' | ||
#' # The above are equivalent to | ||
#' v + scale_fill_gradient() | ||
#' 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(...), | ||
stop("Unknown scale type", call. = FALSE) | ||
) | ||
} | ||
|
||
#' @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(...), | ||
stop("Unknown scale type", call. = FALSE) | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#' Viridis colour scales from viridisLite | ||
#' | ||
#' The `viridis` scales provide color maps that are perceptually uniform in both | ||
#' color and black-and-white. They are also designed to be perceived by viewers | ||
#' with common forms of color blindness. See also | ||
#' <https://bids.github.io/colormap/>. | ||
#' | ||
#' @inheritParams viridisLite::viridis | ||
#' @inheritParams scales::gradient_n_pal | ||
#' @inheritParams continuous_scale | ||
#' @param ... Other arguments passed on to [discrete_scale()] or | ||
#' [continuous_scale()] to control name, limits, breaks, labels and so forth. | ||
#' @family colour scales | ||
#' @rdname scale_viridis | ||
#' @export | ||
#' @examples | ||
#' dsamp <- diamonds[sample(nrow(diamonds), 1000), ] | ||
#' (d <- ggplot(dsamp, aes(carat, price)) + | ||
#' geom_point(aes(colour = clarity))) | ||
#' d + scale_colour_viridis_d() | ||
#' | ||
#' # Change scale label | ||
#' d + scale_colour_viridis_d("Diamond\nclarity") | ||
#' | ||
#' # Select palette to use, see ?scales::viridis_pal for more details | ||
#' d + scale_colour_viridis_d(option = "plasma") | ||
#' d + scale_colour_viridis_d(option = "inferno") | ||
#' | ||
#' \donttest{ | ||
#' # scale_fill_viridis_d works just the same as | ||
#' # scale_colour_viridis_d but for fill colours | ||
#' p <- ggplot(diamonds, aes(x = price, fill = cut)) + | ||
#' geom_histogram(position = "dodge", binwidth = 1000) | ||
#' p + scale_fill_viridis_d() | ||
#' # the order of colour can be reversed | ||
#' p + scale_fill_viridis_d(direction = -1) | ||
#' } | ||
#' | ||
#' # Use viridis_c with continous data | ||
#' v <- ggplot(faithfuld) + | ||
#' geom_tile(aes(waiting, eruptions, fill = density)) | ||
#' v | ||
#' v + scale_fill_viridis_c() | ||
#' v + scale_fill_viridis_c(option = "plasma") | ||
scale_colour_viridis_d <- function(..., alpha = 1, begin = 0, end = 1, | ||
direction = 1, option = "D") { | ||
discrete_scale( | ||
"colour", | ||
"viridis_d", | ||
viridis_pal(alpha, begin, end, direction, option), | ||
... | ||
) | ||
} | ||
|
||
#' @export | ||
#' @rdname scale_viridis | ||
scale_fill_viridis_d <- function(..., alpha = 1, begin = 0, end = 1, | ||
direction = 1, option = "D") { | ||
discrete_scale( | ||
"fill", | ||
"viridis_d", | ||
viridis_pal(alpha, begin, end, direction, option), | ||
... | ||
) | ||
} | ||
|
||
#' @export | ||
#' @rdname scale_viridis | ||
scale_colour_viridis_c <- function(..., alpha = 1, begin = 0, end = 1, | ||
direction = 1, option = "D", values = NULL, | ||
space = "Lab", na.value = "grey50", | ||
guide = "colourbar") { | ||
continuous_scale( | ||
"colour", | ||
"viridis_c", | ||
gradient_n_pal( | ||
viridis_pal(alpha, begin, end, direction, option)(6), | ||
values, | ||
space | ||
), | ||
na.value = na.value, | ||
guide = guide, | ||
... | ||
) | ||
} | ||
|
||
#' @export | ||
#' @rdname scale_viridis | ||
scale_fill_viridis_c <- function(..., alpha = 1, begin = 0, end = 1, | ||
direction = 1, option = "D", values = NULL, | ||
space = "Lab", na.value = "grey50", | ||
guide = "colourbar") { | ||
continuous_scale( | ||
"fill", | ||
"viridis_c", | ||
gradient_n_pal( | ||
viridis_pal(alpha, begin, end, direction, option)(6), | ||
values, | ||
space | ||
), | ||
na.value = na.value, | ||
guide = guide, | ||
... | ||
) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I now think it's probably better form to use the second argument to
getOption()
, rather than setting on load.