Skip to content

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

Merged
merged 15 commits into from
Jul 13, 2017
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
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ Suggests:
rpart,
rmarkdown,
sf (>= 0.3-4),
svglite (>= 1.2.0.9001)
svglite (>= 1.2.0.9001),
viridisLite
Remotes:
hadley/scales,
hadley/svglite
Expand Down Expand Up @@ -169,6 +170,7 @@ Collate:
'scale-.r'
'scale-alpha.r'
'scale-brewer.r'
'scale-colour.r'
'scale-continuous.r'
'scale-date.r'
'scale-discrete-.r'
Expand All @@ -181,6 +183,7 @@ Collate:
'scale-shape.r'
'scale-size.r'
'scale-type.R'
'scale-viridis.r'
'scales-.r'
'sf.R'
'stat-bin.r'
Expand Down
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ export(scale_color_grey)
export(scale_color_hue)
export(scale_color_identity)
export(scale_color_manual)
export(scale_color_viridis_c)
export(scale_color_viridis_d)
export(scale_colour_brewer)
export(scale_colour_continuous)
export(scale_colour_date)
Expand All @@ -397,6 +399,8 @@ export(scale_colour_grey)
export(scale_colour_hue)
export(scale_colour_identity)
export(scale_colour_manual)
export(scale_colour_viridis_c)
export(scale_colour_viridis_d)
export(scale_fill_brewer)
export(scale_fill_continuous)
export(scale_fill_date)
Expand All @@ -410,6 +414,8 @@ export(scale_fill_grey)
export(scale_fill_hue)
export(scale_fill_identity)
export(scale_fill_manual)
export(scale_fill_viridis_c)
export(scale_fill_viridis_d)
export(scale_linetype)
export(scale_linetype_continuous)
export(scale_linetype_discrete)
Expand Down
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# ggplot2 2.2.1.9000

* Default colour maps for continuous data are controlled by global options
`ggplot2.continuous.colour` and `ggplot2.continuous.fill`, which can be set to
either `"gradient"` or `"viridis"` (@karawoo).

* Adds built-in support for `viridis` and related colour maps. Use the functions
`scale_colour_viridis_c()`/`scale_fill_viridis_c()` for continuous data and
`scale_colour_viridis_d()`/`scale_fill_viridis_d()` for discrete data
(@karawoo, #1526).

* Updated datetime scales for `alpha`, `size`, `colour`, and `fill` can take
`date_breaks` and `date_labels` arguments (@karawoo, #1526).

Expand Down
46 changes: 46 additions & 0 deletions R/scale-colour.r
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(...,
Copy link
Member

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.

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)
)
}
105 changes: 105 additions & 0 deletions R/scale-viridis.r
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,
...
)
}
21 changes: 10 additions & 11 deletions R/zxx.r
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
#' @usage NULL
scale_colour_discrete <- scale_colour_hue

#' @export
#' @rdname scale_gradient
#' @usage NULL
scale_colour_continuous <- scale_colour_gradient

#' @export
#' @rdname scale_gradient
#' @usage NULL
Expand Down Expand Up @@ -53,11 +48,6 @@ scale_colour_date <- function(...,
#' @usage NULL
scale_fill_discrete <- scale_fill_hue

#' @export
#' @rdname scale_gradient
#' @usage NULL
scale_fill_continuous <- scale_fill_gradient

#' @export
#' @rdname scale_gradient
#' @usage NULL
Expand All @@ -75,7 +65,6 @@ scale_fill_datetime <- function(...,
guide = guide,
...
)

}

#' @export
Expand Down Expand Up @@ -154,3 +143,13 @@ scale_color_identity <- scale_colour_identity
#' @rdname scale_manual
#' @usage NULL
scale_color_manual <- scale_colour_manual

#' @export
#' @rdname scale_viridis
#' @usage NULL
scale_color_viridis_d <- scale_colour_viridis_d

#' @export
#' @rdname scale_viridis
#' @usage NULL
scale_color_viridis_c <- scale_colour_viridis_c
3 changes: 2 additions & 1 deletion man/scale_alpha.Rd

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

3 changes: 2 additions & 1 deletion man/scale_brewer.Rd

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

41 changes: 41 additions & 0 deletions man/scale_colour_continuous.Rd

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

5 changes: 2 additions & 3 deletions man/scale_gradient.Rd

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

3 changes: 2 additions & 1 deletion man/scale_grey.Rd

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

3 changes: 2 additions & 1 deletion man/scale_hue.Rd

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

Loading