|
1 | 1 | #' Continuous and binned colour scales
|
2 | 2 | #'
|
3 | 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. |
| 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()]) |
7 | 7 | #'
|
8 | 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 |
| 9 | +#' @param type One of the following: |
| 10 | +#' * "gradient" (the default) |
| 11 | +#' * "viridis" |
| 12 | +#' * A function that returns a continuous colour scale. |
11 | 13 | #' @seealso [scale_colour_gradient()], [scale_colour_viridis_c()],
|
12 | 14 | #' [scale_colour_steps()], [scale_colour_viridis_b()], [scale_fill_gradient()],
|
13 | 15 | #' [scale_fill_viridis_c()], [scale_fill_steps()], and [scale_fill_viridis_b()]
|
|
40 | 42 | #' v + scale_fill_viridis_c()
|
41 | 43 | scale_colour_continuous <- function(...,
|
42 | 44 | 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 { |
47 | 52 | abort("Unknown scale type")
|
48 |
| - ) |
| 53 | + } |
49 | 54 | }
|
50 | 55 |
|
51 | 56 | #' @rdname scale_colour_continuous
|
52 | 57 | #' @export
|
53 | 58 | scale_fill_continuous <- function(...,
|
54 | 59 | 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 { |
59 | 67 | abort("Unknown scale type")
|
60 |
| - ) |
| 68 | + } |
61 | 69 | }
|
62 | 70 |
|
63 | 71 | #' @export
|
64 | 72 | #' @rdname scale_colour_continuous
|
65 | 73 | #' @usage NULL
|
66 | 74 | 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 { |
72 | 83 | abort("Unknown scale type")
|
73 |
| - ) |
| 84 | + } |
74 | 85 | }
|
75 | 86 |
|
76 | 87 | #' @export
|
77 | 88 | #' @rdname scale_colour_continuous
|
78 | 89 | #' @usage NULL
|
79 | 90 | 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 { |
85 | 99 | abort("Unknown scale type")
|
86 |
| - ) |
| 100 | + } |
87 | 101 | }
|
0 commit comments