Skip to content

Commit 77fc5c7

Browse files
authored
Add built-in support for viridis color scales (#2178)
* Begin viridis scales * Add to NEWS.md * Add documentation for continuous_scale parameters na.value and guide * Remove @description * tidyverse style for long calls to discrete_scale/continuous_scale oops * Use markdown url syntax * viridis_pal is now in the scales package * Shorten names to viridis_c and viridis_d * Add viridis examples based on the brewer examples * Add viridis tests * Update viridis tests * Use global options to control default continuous colour/fill scales * Don't set options in .onLoad() * Put closing paren on new line * Update NEWS.md
1 parent cb6ff90 commit 77fc5c7

14 files changed

+342
-19
lines changed

DESCRIPTION

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ Suggests:
4343
rpart,
4444
rmarkdown,
4545
sf (>= 0.3-4),
46-
svglite (>= 1.2.0.9001)
46+
svglite (>= 1.2.0.9001),
47+
viridisLite
4748
Remotes:
4849
hadley/scales,
4950
hadley/svglite
@@ -169,6 +170,7 @@ Collate:
169170
'scale-.r'
170171
'scale-alpha.r'
171172
'scale-brewer.r'
173+
'scale-colour.r'
172174
'scale-continuous.r'
173175
'scale-date.r'
174176
'scale-discrete-.r'
@@ -181,6 +183,7 @@ Collate:
181183
'scale-shape.r'
182184
'scale-size.r'
183185
'scale-type.R'
186+
'scale-viridis.r'
184187
'scales-.r'
185188
'sf.R'
186189
'stat-bin.r'

NAMESPACE

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,8 @@ export(scale_color_grey)
385385
export(scale_color_hue)
386386
export(scale_color_identity)
387387
export(scale_color_manual)
388+
export(scale_color_viridis_c)
389+
export(scale_color_viridis_d)
388390
export(scale_colour_brewer)
389391
export(scale_colour_continuous)
390392
export(scale_colour_date)
@@ -398,6 +400,8 @@ export(scale_colour_grey)
398400
export(scale_colour_hue)
399401
export(scale_colour_identity)
400402
export(scale_colour_manual)
403+
export(scale_colour_viridis_c)
404+
export(scale_colour_viridis_d)
401405
export(scale_fill_brewer)
402406
export(scale_fill_continuous)
403407
export(scale_fill_date)
@@ -411,6 +415,8 @@ export(scale_fill_grey)
411415
export(scale_fill_hue)
412416
export(scale_fill_identity)
413417
export(scale_fill_manual)
418+
export(scale_fill_viridis_c)
419+
export(scale_fill_viridis_d)
414420
export(scale_linetype)
415421
export(scale_linetype_continuous)
416422
export(scale_linetype_discrete)

NEWS.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# ggplot2 2.2.1.9000
22

3+
* Default colour maps for continuous data are controlled by global options
4+
`ggplot2.continuous.colour` and `ggplot2.continuous.fill`, which can be set to
5+
either `"gradient"` or `"viridis"` (@karawoo).
6+
7+
* Adds built-in support for `viridis` and related colour maps. Use the functions
8+
`scale_colour_viridis_c()`/`scale_fill_viridis_c()` for continuous data and
9+
`scale_colour_viridis_d()`/`scale_fill_viridis_d()` for discrete data
10+
(@karawoo, #1526).
11+
312
* Updated datetime scales for `alpha`, `size`, `colour`, and `fill` can take
413
`date_breaks` and `date_labels` arguments (@karawoo, #1526).
514

R/scale-colour.r

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#' Continuous colour scales
2+
#'
3+
#' Colour scales for continuous data default to the values of the
4+
#' `ggplot2.continuous.colour` and `ggplot2.continuous.fill` options. If these
5+
#' options are not present, `"gradient"` will be used. See [options()] for more
6+
#' information.
7+
#'
8+
#' @param ... Additional parameters passed on to the scale type
9+
#' @param type One of "gradient" (the default) or "viridis" indicating the
10+
#' colour scale to use
11+
#' @seealso [scale_colour_gradient()], [scale_colour_viridis_c()],
12+
#' [scale_fill_gradient()], and [scale_fill_viridis_c()]
13+
#' @export
14+
#' @rdname scale_colour_continuous
15+
#' @examples
16+
#' v <- ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
17+
#' geom_tile()
18+
#' v
19+
#'
20+
#' v + scale_fill_continuous(type = "gradient")
21+
#' v + scale_fill_continuous(type = "viridis")
22+
#'
23+
#' # The above are equivalent to
24+
#' v + scale_fill_gradient()
25+
#' v + scale_fill_viridis_c()
26+
scale_colour_continuous <- function(...,
27+
type = getOption("ggplot2.continuous.colour", default = "gradient")) {
28+
switch(
29+
type,
30+
gradient = scale_colour_gradient(...),
31+
viridis = scale_colour_viridis_c(...),
32+
stop("Unknown scale type", call. = FALSE)
33+
)
34+
}
35+
36+
#' @rdname scale_colour_continuous
37+
#' @export
38+
scale_fill_continuous <- function(...,
39+
type = getOption("ggplot2.continuous.fill", default = "gradient")) {
40+
switch(
41+
type,
42+
gradient = scale_fill_gradient(...),
43+
viridis = scale_fill_viridis_c(...),
44+
stop("Unknown scale type", call. = FALSE)
45+
)
46+
}

R/scale-viridis.r

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#' Viridis colour scales from viridisLite
2+
#'
3+
#' The `viridis` scales provide color maps that are perceptually uniform in both
4+
#' color and black-and-white. They are also designed to be perceived by viewers
5+
#' with common forms of color blindness. See also
6+
#' <https://bids.github.io/colormap/>.
7+
#'
8+
#' @inheritParams viridisLite::viridis
9+
#' @inheritParams scales::gradient_n_pal
10+
#' @inheritParams continuous_scale
11+
#' @param ... Other arguments passed on to [discrete_scale()] or
12+
#' [continuous_scale()] to control name, limits, breaks, labels and so forth.
13+
#' @family colour scales
14+
#' @rdname scale_viridis
15+
#' @export
16+
#' @examples
17+
#' dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
18+
#' (d <- ggplot(dsamp, aes(carat, price)) +
19+
#' geom_point(aes(colour = clarity)))
20+
#' d + scale_colour_viridis_d()
21+
#'
22+
#' # Change scale label
23+
#' d + scale_colour_viridis_d("Diamond\nclarity")
24+
#'
25+
#' # Select palette to use, see ?scales::viridis_pal for more details
26+
#' d + scale_colour_viridis_d(option = "plasma")
27+
#' d + scale_colour_viridis_d(option = "inferno")
28+
#'
29+
#' \donttest{
30+
#' # scale_fill_viridis_d works just the same as
31+
#' # scale_colour_viridis_d but for fill colours
32+
#' p <- ggplot(diamonds, aes(x = price, fill = cut)) +
33+
#' geom_histogram(position = "dodge", binwidth = 1000)
34+
#' p + scale_fill_viridis_d()
35+
#' # the order of colour can be reversed
36+
#' p + scale_fill_viridis_d(direction = -1)
37+
#' }
38+
#'
39+
#' # Use viridis_c with continous data
40+
#' v <- ggplot(faithfuld) +
41+
#' geom_tile(aes(waiting, eruptions, fill = density))
42+
#' v
43+
#' v + scale_fill_viridis_c()
44+
#' v + scale_fill_viridis_c(option = "plasma")
45+
scale_colour_viridis_d <- function(..., alpha = 1, begin = 0, end = 1,
46+
direction = 1, option = "D") {
47+
discrete_scale(
48+
"colour",
49+
"viridis_d",
50+
viridis_pal(alpha, begin, end, direction, option),
51+
...
52+
)
53+
}
54+
55+
#' @export
56+
#' @rdname scale_viridis
57+
scale_fill_viridis_d <- function(..., alpha = 1, begin = 0, end = 1,
58+
direction = 1, option = "D") {
59+
discrete_scale(
60+
"fill",
61+
"viridis_d",
62+
viridis_pal(alpha, begin, end, direction, option),
63+
...
64+
)
65+
}
66+
67+
#' @export
68+
#' @rdname scale_viridis
69+
scale_colour_viridis_c <- function(..., alpha = 1, begin = 0, end = 1,
70+
direction = 1, option = "D", values = NULL,
71+
space = "Lab", na.value = "grey50",
72+
guide = "colourbar") {
73+
continuous_scale(
74+
"colour",
75+
"viridis_c",
76+
gradient_n_pal(
77+
viridis_pal(alpha, begin, end, direction, option)(6),
78+
values,
79+
space
80+
),
81+
na.value = na.value,
82+
guide = guide,
83+
...
84+
)
85+
}
86+
87+
#' @export
88+
#' @rdname scale_viridis
89+
scale_fill_viridis_c <- function(..., alpha = 1, begin = 0, end = 1,
90+
direction = 1, option = "D", values = NULL,
91+
space = "Lab", na.value = "grey50",
92+
guide = "colourbar") {
93+
continuous_scale(
94+
"fill",
95+
"viridis_c",
96+
gradient_n_pal(
97+
viridis_pal(alpha, begin, end, direction, option)(6),
98+
values,
99+
space
100+
),
101+
na.value = na.value,
102+
guide = guide,
103+
...
104+
)
105+
}

R/zxx.r

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
#' @usage NULL
66
scale_colour_discrete <- scale_colour_hue
77

8-
#' @export
9-
#' @rdname scale_gradient
10-
#' @usage NULL
11-
scale_colour_continuous <- scale_colour_gradient
12-
138
#' @export
149
#' @rdname scale_gradient
1510
#' @usage NULL
@@ -53,11 +48,6 @@ scale_colour_date <- function(...,
5348
#' @usage NULL
5449
scale_fill_discrete <- scale_fill_hue
5550

56-
#' @export
57-
#' @rdname scale_gradient
58-
#' @usage NULL
59-
scale_fill_continuous <- scale_fill_gradient
60-
6151
#' @export
6252
#' @rdname scale_gradient
6353
#' @usage NULL
@@ -75,7 +65,6 @@ scale_fill_datetime <- function(...,
7565
guide = guide,
7666
...
7767
)
78-
7968
}
8069

8170
#' @export
@@ -154,3 +143,13 @@ scale_color_identity <- scale_colour_identity
154143
#' @rdname scale_manual
155144
#' @usage NULL
156145
scale_color_manual <- scale_colour_manual
146+
147+
#' @export
148+
#' @rdname scale_viridis
149+
#' @usage NULL
150+
scale_color_viridis_d <- scale_colour_viridis_d
151+
152+
#' @export
153+
#' @rdname scale_viridis
154+
#' @usage NULL
155+
scale_color_viridis_c <- scale_colour_viridis_c

man/scale_alpha.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/scale_brewer.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/scale_colour_continuous.Rd

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/scale_gradient.Rd

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/scale_grey.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/scale_hue.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)