Skip to content

Commit 95ba60b

Browse files
authored
Type argument in scale_[fill/colour]_[continuous/binned] can accept a function (#3828)
1 parent 248e94c commit 95ba60b

File tree

3 files changed

+52
-32
lines changed

3 files changed

+52
-32
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
* `annotation_raster()` adds support for native rasters. For large rasters,
77
native rasters render significantly faster than arrays (@kent37, #3388)
8+
9+
* 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)
810

911
* A newly added geom `geom_density_2d_filled()` and associated stat
1012
`stat_density_2d_filled()` can draw filled density contours

R/scale-colour.r

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#' Continuous and binned colour scales
22
#'
33
#' 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.
4+
#' `ggplot2.continuous.colour` and `ggplot2.continuous.fill` options. These
5+
#' [options()] default to `"gradient"` (i.e., [scale_colour_gradient()] and
6+
#' [scale_fill_gradient()])
77
#'
88
#' @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
9+
#' @param type One of the following:
10+
#' * "gradient" (the default)
11+
#' * "viridis"
12+
#' * A function that returns a continuous colour scale.
1113
#' @seealso [scale_colour_gradient()], [scale_colour_viridis_c()],
1214
#' [scale_colour_steps()], [scale_colour_viridis_b()], [scale_fill_gradient()],
1315
#' [scale_fill_viridis_c()], [scale_fill_steps()], and [scale_fill_viridis_b()]
@@ -40,48 +42,60 @@
4042
#' v + scale_fill_viridis_c()
4143
scale_colour_continuous <- function(...,
4244
type = getOption("ggplot2.continuous.colour", default = "gradient")) {
43-
switch(
44-
type,
45-
gradient = scale_colour_gradient(...),
46-
viridis = scale_colour_viridis_c(...),
45+
if (is.function(type)) {
46+
type(...)
47+
} else if (identical(type, "gradient")) {
48+
scale_colour_gradient(...)
49+
} else if (identical(type, "viridis")) {
50+
scale_colour_viridis_c(...)
51+
} else {
4752
abort("Unknown scale type")
48-
)
53+
}
4954
}
5055

5156
#' @rdname scale_colour_continuous
5257
#' @export
5358
scale_fill_continuous <- function(...,
5459
type = getOption("ggplot2.continuous.fill", default = "gradient")) {
55-
switch(
56-
type,
57-
gradient = scale_fill_gradient(...),
58-
viridis = scale_fill_viridis_c(...),
60+
if (is.function(type)) {
61+
type(...)
62+
} else if (identical(type, "gradient")) {
63+
scale_fill_gradient(...)
64+
} else if (identical(type, "viridis")) {
65+
scale_fill_viridis_c(...)
66+
} else {
5967
abort("Unknown scale type")
60-
)
68+
}
6169
}
6270

6371
#' @export
6472
#' @rdname scale_colour_continuous
6573
#' @usage NULL
6674
scale_colour_binned <- function(...,
67-
type = getOption("ggplot2.continuous.colour", default = "gradient")) {
68-
switch(
69-
type,
70-
gradient = scale_colour_steps(...),
71-
viridis = scale_colour_viridis_b(...),
75+
type = getOption("ggplot2.binned.colour", default = getOption("ggplot2.continuous.colour", default = "gradient"))) {
76+
if (is.function(type)) {
77+
type(...)
78+
} else if (identical(type, "gradient")) {
79+
scale_colour_steps(...)
80+
} else if (identical(type, "viridis")) {
81+
scale_colour_viridis_b(...)
82+
} else {
7283
abort("Unknown scale type")
73-
)
84+
}
7485
}
7586

7687
#' @export
7788
#' @rdname scale_colour_continuous
7889
#' @usage NULL
7990
scale_fill_binned <- function(...,
80-
type = getOption("ggplot2.continuous.colour", default = "gradient")) {
81-
switch(
82-
type,
83-
gradient = scale_fill_steps(...),
84-
viridis = scale_fill_viridis_b(...),
91+
type = getOption("ggplot2.binned.fill", default = getOption("ggplot2.continuous.fill", default = "gradient"))) {
92+
if (is.function(type)) {
93+
type(...)
94+
} else if (identical(type, "gradient")) {
95+
scale_fill_steps(...)
96+
} else if (identical(type, "viridis")) {
97+
scale_fill_viridis_b(...)
98+
} else {
8599
abort("Unknown scale type")
86-
)
100+
}
87101
}

man/scale_colour_continuous.Rd

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

0 commit comments

Comments
 (0)