|
27 | 27 | #' you want to manually set the colors of a scale, consider using
|
28 | 28 | #' [scale_colour_gradient()] or [scale_colour_steps()].
|
29 | 29 | #'
|
| 30 | +#' @inheritParams continuous_scale |
30 | 31 | #' @param ... Additional parameters passed on to the scale type
|
31 | 32 | #' @param type One of the following:
|
32 | 33 | #' * "gradient" (the default)
|
|
77 | 78 | #' v
|
78 | 79 | #' options(ggplot2.continuous.fill = tmp) # restore previous setting
|
79 | 80 | #' @export
|
80 |
| -scale_colour_continuous <- function(..., |
| 81 | +scale_colour_continuous <- function(..., aesthetics = "colour", |
| 82 | + guide = "colourbar", na.value = "grey50", |
81 | 83 | type = getOption("ggplot2.continuous.colour")) {
|
82 |
| - type <- type %||% "gradient" |
83 |
| - args <- list2(...) |
84 |
| - args$call <- args$call %||% current_call() |
85 | 84 |
|
86 |
| - if (is.function(type)) { |
87 |
| - if (!any(c("...", "call") %in% fn_fmls_names(type))) { |
88 |
| - args$call <- NULL |
89 |
| - } |
90 |
| - check_scale_type(exec(type, !!!args), "scale_colour_continuous", "colour") |
91 |
| - } else if (identical(type, "gradient")) { |
92 |
| - exec(scale_colour_gradient, !!!args) |
93 |
| - } else if (identical(type, "viridis")) { |
94 |
| - exec(scale_colour_viridis_c, !!!args) |
95 |
| - } else { |
96 |
| - cli::cli_abort(c( |
97 |
| - "Unknown scale type: {.val {type}}", |
98 |
| - "i" = "Use either {.val gradient} or {.val viridis}." |
99 |
| - )) |
| 85 | + if (!is.null(type)) { |
| 86 | + scale <- scale_backward_compatibility( |
| 87 | + ..., guide = guide, na.value = na.value, scale = type, |
| 88 | + aesthetic = "colour", type = "continuous" |
| 89 | + ) |
| 90 | + return(scale) |
100 | 91 | }
|
| 92 | + |
| 93 | + continuous_scale( |
| 94 | + aesthetics, palette = NULL, guide = guide, na.value = na.value, |
| 95 | + ... |
| 96 | + ) |
101 | 97 | }
|
102 | 98 |
|
103 | 99 | #' @rdname scale_colour_continuous
|
104 | 100 | #' @export
|
105 |
| -scale_fill_continuous <- function(..., |
| 101 | +scale_fill_continuous <- function(..., aesthetics = "fill", guide = "colourbar", |
| 102 | + na.value = "grey50", |
106 | 103 | type = getOption("ggplot2.continuous.fill")) {
|
107 |
| - type <- type %||% "gradient" |
108 |
| - args <- list2(...) |
109 |
| - args$call <- args$call %||% current_call() |
110 | 104 |
|
111 |
| - if (is.function(type)) { |
112 |
| - if (!any(c("...", "call") %in% fn_fmls_names(type))) { |
113 |
| - args$call <- NULL |
114 |
| - } |
115 |
| - check_scale_type(exec(type, !!!args), "scale_fill_continuous", "fill") |
116 |
| - } else if (identical(type, "gradient")) { |
117 |
| - exec(scale_fill_gradient, !!!args) |
118 |
| - } else if (identical(type, "viridis")) { |
119 |
| - exec(scale_fill_viridis_c, !!!args) |
120 |
| - } else { |
121 |
| - cli::cli_abort(c( |
122 |
| - "Unknown scale type: {.val {type}}", |
123 |
| - "i" = "Use either {.val gradient} or {.val viridis}." |
124 |
| - )) |
| 105 | + if (!is.null(type)) { |
| 106 | + scale <- scale_backward_compatibility( |
| 107 | + ..., guide = guide, na.value = na.value, scale = type, |
| 108 | + aesthetic = "fill", type = "continuous" |
| 109 | + ) |
| 110 | + return(scale) |
125 | 111 | }
|
| 112 | + |
| 113 | + continuous_scale( |
| 114 | + aesthetics, palette = NULL, guide = guide, na.value = na.value, |
| 115 | + ... |
| 116 | + ) |
126 | 117 | }
|
127 | 118 |
|
128 | 119 | #' @export
|
129 | 120 | #' @rdname scale_colour_continuous
|
130 |
| -scale_colour_binned <- function(..., |
| 121 | +scale_colour_binned <- function(..., aesthetics = "colour", guide = "coloursteps", |
| 122 | + na.value = "grey50", |
131 | 123 | type = getOption("ggplot2.binned.colour")) {
|
132 |
| - args <- list2(...) |
133 |
| - args$call <- args$call %||% current_call() |
134 |
| - if (is.function(type)) { |
135 |
| - if (!any(c("...", "call") %in% fn_fmls_names(type))) { |
136 |
| - args$call <- NULL |
137 |
| - } |
138 |
| - check_scale_type(exec(type, !!!args), "scale_colour_binned", "colour") |
139 |
| - } else { |
140 |
| - type_fallback <- getOption("ggplot2.continuous.colour", default = "gradient") |
141 |
| - # don't use fallback from scale_colour_continuous() if it is |
142 |
| - # a function, since that would change the type of the color |
143 |
| - # scale from binned to continuous |
144 |
| - if (is.function(type_fallback)) { |
145 |
| - type_fallback <- "gradient" |
146 |
| - } |
147 |
| - type <- type %||% type_fallback |
148 |
| - |
149 |
| - if (identical(type, "gradient")) { |
150 |
| - exec(scale_colour_steps, !!!args) |
151 |
| - } else if (identical(type, "viridis")) { |
152 |
| - exec(scale_colour_viridis_b, !!!args) |
153 |
| - } else { |
154 |
| - cli::cli_abort(c( |
155 |
| - "Unknown scale type: {.val {type}}", |
156 |
| - "i" = "Use either {.val gradient} or {.val viridis}." |
157 |
| - )) |
158 |
| - } |
| 124 | + if (!is.null(type)) { |
| 125 | + scale <- scale_backward_compatibility( |
| 126 | + ..., guide = guide, na.value = na.value, scale = type, |
| 127 | + aesthetic = "colour", type = "binned" |
| 128 | + ) |
| 129 | + return(scale) |
159 | 130 | }
|
| 131 | + |
| 132 | + binned_scale( |
| 133 | + aesthetics, palette = NULL, guide = guide, na.value = na.value, |
| 134 | + ... |
| 135 | + ) |
160 | 136 | }
|
161 | 137 |
|
162 | 138 | #' @export
|
163 | 139 | #' @rdname scale_colour_continuous
|
164 |
| -scale_fill_binned <- function(..., |
| 140 | +scale_fill_binned <- function(..., aesthetics = "fill", guide = "coloursteps", |
| 141 | + na.value = "grey50", |
165 | 142 | type = getOption("ggplot2.binned.fill")) {
|
166 |
| - args <- list2(...) |
167 |
| - args$call <- args$call %||% current_call() |
168 |
| - if (is.function(type)) { |
169 |
| - if (!any(c("...", "call") %in% fn_fmls_names(type))) { |
170 |
| - args$call <- NULL |
171 |
| - } |
172 |
| - check_scale_type(exec(type, !!!args), "scale_fill_binned", "fill") |
173 |
| - } else { |
174 |
| - type_fallback <- getOption("ggplot2.continuous.fill", default = "gradient") |
175 |
| - # don't use fallback from scale_colour_continuous() if it is |
176 |
| - # a function, since that would change the type of the color |
177 |
| - # scale from binned to continuous |
178 |
| - if (is.function(type_fallback)) { |
179 |
| - type_fallback <- "gradient" |
180 |
| - } |
181 |
| - type <- type %||% type_fallback |
182 |
| - |
183 |
| - if (identical(type, "gradient")) { |
184 |
| - exec(scale_fill_steps, !!!args) |
185 |
| - } else if (identical(type, "viridis")) { |
186 |
| - exec(scale_fill_viridis_b, !!!args) |
187 |
| - } else { |
188 |
| - cli::cli_abort(c( |
189 |
| - "Unknown scale type: {.val {type}}", |
190 |
| - "i" = "Use either {.val gradient} or {.val viridis}." |
191 |
| - )) |
192 |
| - } |
| 143 | + if (!is.null(type)) { |
| 144 | + scale <- scale_backward_compatibility( |
| 145 | + ..., guide = guide, na.value = na.value, scale = type, |
| 146 | + aesthetic = "fill", type = "binned" |
| 147 | + ) |
| 148 | + return(scale) |
193 | 149 | }
|
194 |
| -} |
195 | 150 |
|
| 151 | + binned_scale( |
| 152 | + aesthetics, palette = NULL, guide = guide, na.value = na.value, |
| 153 | + ... |
| 154 | + ) |
| 155 | +} |
196 | 156 |
|
197 | 157 | # helper function to make sure that the provided scale is of the correct
|
198 | 158 | # type (i.e., is continuous and works with the provided aesthetic)
|
@@ -222,3 +182,73 @@ check_scale_type <- function(scale, name, aesthetic, scale_is_discrete = FALSE,
|
222 | 182 |
|
223 | 183 | scale
|
224 | 184 | }
|
| 185 | + |
| 186 | +# helper function for backwards compatibility through setting defaults |
| 187 | +# scales through `options()` instead of `theme()`. |
| 188 | +scale_backward_compatibility <- function(..., scale, aesthetic, type) { |
| 189 | + aesthetic <- standardise_aes_names(aesthetic[1]) |
| 190 | + |
| 191 | + args <- list2(...) |
| 192 | + args$call <- args$call %||% caller_call() %||% current_call() |
| 193 | + |
| 194 | + if (type == "binned") { |
| 195 | + fallback <- getOption( |
| 196 | + paste("ggplot2", type, aesthetic, sep = "."), |
| 197 | + default = "gradient" |
| 198 | + ) |
| 199 | + if (is.function(fallback)) { |
| 200 | + fallback <- "gradient" |
| 201 | + } |
| 202 | + scale <- scale %||% fallback |
| 203 | + } |
| 204 | + |
| 205 | + if (is_bare_string(scale)) { |
| 206 | + if (scale == "continuous") { |
| 207 | + scale <- "gradient" |
| 208 | + } |
| 209 | + if (scale == "discrete") { |
| 210 | + scale <- "hue" |
| 211 | + } |
| 212 | + if (scale == "viridis") { |
| 213 | + scale <- switch( |
| 214 | + type, discrete = "viridis_d", binned = "viridis_b", "viridis_c" |
| 215 | + ) |
| 216 | + } |
| 217 | + |
| 218 | + candidates <- paste("scale", aesthetic, scale, sep = "_") |
| 219 | + for (candi in candidates) { |
| 220 | + f <- find_global(candi, env = caller_env(), mode = "function") |
| 221 | + if (!is.null(f)) { |
| 222 | + scale <- f |
| 223 | + break |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + if (!is.function(scale) && type == "discrete") { |
| 229 | + args$type <- scale |
| 230 | + scale <- switch( |
| 231 | + aesthetic, |
| 232 | + colour = scale_colour_qualitative, |
| 233 | + fill = scale_fill_qualitative |
| 234 | + ) |
| 235 | + } |
| 236 | + |
| 237 | + if (is.function(scale)) { |
| 238 | + if (!any(c("...", "call") %in% fn_fmls_names(scale))) { |
| 239 | + args$call <- NULL |
| 240 | + } |
| 241 | + if (!"..." %in% fn_fmls_names(scale)) { |
| 242 | + args <- args[intersect(names(args), fn_fmls_names(scale))] |
| 243 | + } |
| 244 | + scale <- check_scale_type( |
| 245 | + exec(scale, !!!args), |
| 246 | + paste("scale", aesthetic, type, sep = "_"), |
| 247 | + aesthetic, |
| 248 | + scale_is_discrete = type == "discrete" |
| 249 | + ) |
| 250 | + return(scale) |
| 251 | + } |
| 252 | + |
| 253 | + cli::cli_abort("Unknown scale type: {.val {scale}}") |
| 254 | +} |
0 commit comments