|
28 | 28 | #' [scale_colour_gradient()] or [scale_colour_steps()].
|
29 | 29 | #'
|
30 | 30 | #' @inheritParams continuous_scale
|
| 31 | +#' @param palette One of the following: |
| 32 | +#' * `NULL` for the default palette stored in the theme. |
| 33 | +#' * a character vector of colours. |
| 34 | +#' * a single string naming a palette. |
| 35 | +#' * a palette function that when called with a numeric vector with values |
| 36 | +#' between 0 and 1 returns the corresponding output values. |
31 | 37 | #' @param ... Additional parameters passed on to the scale type
|
32 | 38 | #' @param type One of the following:
|
33 | 39 | #' * "gradient" (the default)
|
|
57 | 63 | #' see the [paper on the colorspace package](https://arxiv.org/abs/1903.06490)
|
58 | 64 | #' and references therein.
|
59 | 65 | #' @examples
|
60 |
| -#' v <- ggplot(faithfuld, aes(waiting, eruptions, fill = density)) + |
61 |
| -#' geom_tile() |
62 |
| -#' v |
63 |
| -#' |
64 |
| -#' v + scale_fill_continuous(type = "gradient") |
65 |
| -#' v + scale_fill_continuous(type = "viridis") |
66 |
| -#' |
67 |
| -#' # The above are equivalent to |
68 |
| -#' v + scale_fill_gradient() |
69 |
| -#' v + scale_fill_viridis_c() |
70 |
| -#' |
71 |
| -#' # To make a binned version of this plot |
72 |
| -#' v + scale_fill_binned(type = "viridis") |
73 |
| -#' |
74 |
| -#' # Set a different default scale using the options |
75 |
| -#' # mechanism |
76 |
| -#' tmp <- getOption("ggplot2.continuous.fill") # store current setting |
77 |
| -#' options(ggplot2.continuous.fill = scale_fill_distiller) |
78 |
| -#' v |
79 |
| -#' options(ggplot2.continuous.fill = tmp) # restore previous setting |
| 66 | +#' # A standard plot |
| 67 | +#' p <- ggplot(mpg, aes(displ, hwy, colour = cty)) + |
| 68 | +#' geom_point() |
| 69 | +#' |
| 70 | +#' # You can use the scale to give a palette directly |
| 71 | +#' p + scale_colour_continuous(palette = c("#FEE0D2", "#FC9272", "#DE2D26")) |
| 72 | +#' |
| 73 | +#' # The default colours are encoded into the theme |
| 74 | +#' p + theme(palette.colour.continuous = c("#DEEBF7", "#9ECAE1", "#3182BD")) |
| 75 | +#' |
| 76 | +#' # You can globally set default colour palette via the theme |
| 77 | +#' old <- update_theme(palette.colour.continuous = c("#E5F5E0", "#A1D99B", "#31A354")) |
| 78 | +#' |
| 79 | +#' # Plot now shows new global default |
| 80 | +#' p |
| 81 | +#' |
| 82 | +#' # The default binned colour scale uses the continuous palette |
| 83 | +#' p + scale_colour_binned() + |
| 84 | +#' theme(palette.colour.continuous = c("#EFEDF5", "#BCBDDC", "#756BB1")) |
| 85 | +#' |
| 86 | +#' # Restoring the previous theme |
| 87 | +#' theme_set(old) |
80 | 88 | #' @export
|
81 |
| -scale_colour_continuous <- function(..., aesthetics = "colour", |
| 89 | +scale_colour_continuous <- function(..., palette = NULL, aesthetics = "colour", |
82 | 90 | guide = "colourbar", na.value = "grey50",
|
83 | 91 | type = getOption("ggplot2.continuous.colour")) {
|
84 |
| - if (!is.null(type)) { |
| 92 | + |
| 93 | + has_old_args <- any(names(enexprs(...)) %in% c("low", "high")) |
| 94 | + |
| 95 | + if (has_old_args || (!is.null(type) && is.null(palette))) { |
85 | 96 | scale <- scale_backward_compatibility(
|
86 | 97 | ..., guide = guide, na.value = na.value, scale = type,
|
87 | 98 | aesthetic = "colour", type = "continuous"
|
88 | 99 | )
|
89 | 100 | return(scale)
|
90 | 101 | }
|
91 |
| - |
| 102 | + palette <- if (!is.null(palette)) as_continuous_pal(palette) |
92 | 103 | continuous_scale(
|
93 |
| - aesthetics, palette = NULL, guide = guide, na.value = na.value, |
| 104 | + aesthetics, palette = palette, guide = guide, na.value = na.value, |
94 | 105 | ...
|
95 | 106 | )
|
96 | 107 | }
|
97 | 108 |
|
98 | 109 | #' @rdname scale_colour_continuous
|
99 | 110 | #' @export
|
100 |
| -scale_fill_continuous <- function(..., aesthetics = "fill", guide = "colourbar", |
| 111 | +scale_fill_continuous <- function(..., palette = NULL, aesthetics = "fill", guide = "colourbar", |
101 | 112 | na.value = "grey50",
|
102 | 113 | type = getOption("ggplot2.continuous.fill")) {
|
103 | 114 |
|
104 |
| - if (!is.null(type)) { |
| 115 | + has_old_args <- any(names(enexprs(...)) %in% c("low", "high")) |
| 116 | + |
| 117 | + if (has_old_args || (!is.null(type) && is.null(palette))) { |
105 | 118 | scale <- scale_backward_compatibility(
|
106 | 119 | ..., guide = guide, na.value = na.value, scale = type,
|
107 | 120 | aesthetic = "fill", type = "continuous"
|
108 | 121 | )
|
109 | 122 | return(scale)
|
110 | 123 | }
|
111 |
| - |
| 124 | + palette <- if (!is.null(palette)) as_continuous_pal(palette) |
112 | 125 | continuous_scale(
|
113 |
| - aesthetics, palette = NULL, guide = guide, na.value = na.value, |
| 126 | + aesthetics, palette = palette, guide = guide, na.value = na.value, |
114 | 127 | ...
|
115 | 128 | )
|
116 | 129 | }
|
117 | 130 |
|
118 | 131 | #' @export
|
119 | 132 | #' @rdname scale_colour_continuous
|
120 |
| -scale_colour_binned <- function(..., aesthetics = "colour", guide = "coloursteps", |
| 133 | +scale_colour_binned <- function(..., palette = NULL, aesthetics = "colour", guide = "coloursteps", |
121 | 134 | na.value = "grey50",
|
122 | 135 | type = getOption("ggplot2.binned.colour")) {
|
123 |
| - if (!is.null(type)) { |
| 136 | + |
| 137 | + has_old_args <- any(names(enexprs(...)) %in% c("low", "high")) |
| 138 | + |
| 139 | + if (has_old_args || (!is.null(type) && is.null(palette))) { |
124 | 140 | scale <- scale_backward_compatibility(
|
125 | 141 | ..., guide = guide, na.value = na.value, scale = type,
|
126 | 142 | aesthetic = "colour", type = "binned"
|
127 | 143 | )
|
128 | 144 | return(scale)
|
129 | 145 | }
|
130 |
| - |
| 146 | + palette <- if (!is.null(palette)) pal_binned(as_discrete_pal(palette)) |
131 | 147 | binned_scale(
|
132 |
| - aesthetics, palette = NULL, guide = guide, na.value = na.value, |
| 148 | + aesthetics, palette = palette, guide = guide, na.value = na.value, |
133 | 149 | ...
|
134 | 150 | )
|
135 | 151 | }
|
136 | 152 |
|
137 | 153 | #' @export
|
138 | 154 | #' @rdname scale_colour_continuous
|
139 |
| -scale_fill_binned <- function(..., aesthetics = "fill", guide = "coloursteps", |
| 155 | +scale_fill_binned <- function(..., palette = NULL, aesthetics = "fill", guide = "coloursteps", |
140 | 156 | na.value = "grey50",
|
141 | 157 | type = getOption("ggplot2.binned.fill")) {
|
142 |
| - if (!is.null(type)) { |
| 158 | + has_old_args <- any(names(enexprs(...)) %in% c("low", "high")) |
| 159 | + |
| 160 | + if (has_old_args || (!is.null(type) && is.null(palette))) { |
143 | 161 | scale <- scale_backward_compatibility(
|
144 | 162 | ..., guide = guide, na.value = na.value, scale = type,
|
145 | 163 | aesthetic = "fill", type = "binned"
|
146 | 164 | )
|
147 | 165 | return(scale)
|
148 | 166 | }
|
149 |
| - |
| 167 | + palette <- if (!is.null(palette)) pal_binned(as_discrete_pal(palette)) |
150 | 168 | binned_scale(
|
151 |
| - aesthetics, palette = NULL, guide = guide, na.value = na.value, |
| 169 | + aesthetics, palette = palette, guide = guide, na.value = na.value, |
| 170 | + ... |
| 171 | + ) |
| 172 | +} |
| 173 | + |
| 174 | +#' Discrete colour scales |
| 175 | +#' |
| 176 | +#' The default discrete colour scale. Defaults to [scale_fill_hue()]/[scale_fill_brewer()] |
| 177 | +#' unless `type` (which defaults to the `ggplot2.discrete.fill`/`ggplot2.discrete.colour` options) |
| 178 | +#' is specified. |
| 179 | +#' |
| 180 | +#' @param palette One of the following: |
| 181 | +#' * `NULL` for the default palette stored in the theme. |
| 182 | +#' * a character vector of colours. |
| 183 | +#' * a single string naming a palette. |
| 184 | +#' * a palette function that when called with a single integer argument (the |
| 185 | +#' number of levels in the scale) returns the values that they should take. |
| 186 | +#' @param ... Additional parameters passed on to the scale type, |
| 187 | +#' @inheritParams discrete_scale |
| 188 | +#' @param type One of the following: |
| 189 | +#' * A character vector of color codes. The codes are used for a 'manual' color |
| 190 | +#' scale as long as the number of codes exceeds the number of data levels |
| 191 | +#' (if there are more levels than codes, [scale_colour_hue()]/[scale_fill_hue()] |
| 192 | +#' are used to construct the default scale). If this is a named vector, then the color values |
| 193 | +#' will be matched to levels based on the names of the vectors. Data values that |
| 194 | +#' don't match will be set as `na.value`. |
| 195 | +#' * A list of character vectors of color codes. The minimum length vector that exceeds the |
| 196 | +#' number of data levels is chosen for the color scaling. This is useful if you |
| 197 | +#' want to change the color palette based on the number of levels. |
| 198 | +#' * A function that returns a discrete colour/fill scale (e.g., [scale_fill_hue()], |
| 199 | +#' [scale_fill_brewer()], etc). |
| 200 | +#' @export |
| 201 | +#' @seealso |
| 202 | +#' The `r link_book("discrete colour scales section", "scales-colour#sec-colour-discrete")` |
| 203 | +#' @examples |
| 204 | +#' # A standard plot |
| 205 | +#' p <- ggplot(mpg, aes(displ, hwy, colour = class)) + |
| 206 | +#' geom_point() |
| 207 | +#' |
| 208 | +#' # You can use the scale to give a palette directly |
| 209 | +#' p + scale_colour_discrete(palette = scales::pal_brewer(palette = "Dark2")) |
| 210 | +#' |
| 211 | +#' # The default colours are encoded into the theme |
| 212 | +#' p + theme(palette.colour.discrete = scales::pal_grey()) |
| 213 | +#' |
| 214 | +#' # You can globally set default colour palette via the theme |
| 215 | +#' old <- update_theme(palette.colour.discrete = scales::pal_viridis()) |
| 216 | +#' |
| 217 | +#' # Plot now shows new global default |
| 218 | +#' p |
| 219 | +#' |
| 220 | +#' # Restoring the previous theme |
| 221 | +#' theme_set(old) |
| 222 | +scale_colour_discrete <- function(..., palette = NULL, aesthetics = "colour", na.value = "grey50", |
| 223 | + type = getOption("ggplot2.discrete.colour")) { |
| 224 | + |
| 225 | + has_old_args <- any(names(enexprs(...)) %in% c("h", "c", "l", "h.start", "direction")) |
| 226 | + |
| 227 | + if (has_old_args || (!is.null(type) && is.null(palette))) { |
| 228 | + scale <- scale_backward_compatibility( |
| 229 | + ..., na.value = na.value, scale = type, |
| 230 | + aesthetic = "colour", type = "discrete" |
| 231 | + ) |
| 232 | + return(scale) |
| 233 | + } |
| 234 | + palette <- if (!is.null(palette)) as_discrete_pal(palette) |
| 235 | + discrete_scale( |
| 236 | + aesthetics, palette = palette, na.value = na.value, |
| 237 | + ... |
| 238 | + ) |
| 239 | +} |
| 240 | + |
| 241 | +#' @rdname scale_colour_discrete |
| 242 | +#' @export |
| 243 | +scale_fill_discrete <- function(..., palette = NULL, aesthetics = "fill", na.value = "grey50", |
| 244 | + type = getOption("ggplot2.discrete.fill")) { |
| 245 | + |
| 246 | + has_old_args <- any(names(enexprs(...)) %in% c("h", "c", "l", "h.start", "direction")) |
| 247 | + |
| 248 | + if (has_old_args || (!is.null(type) && is.null(palette))) { |
| 249 | + scale <- scale_backward_compatibility( |
| 250 | + ..., na.value = na.value, scale = type, |
| 251 | + aesthetic = "fill", type = "discrete" |
| 252 | + ) |
| 253 | + return(scale) |
| 254 | + } |
| 255 | + palette <- if (!is.null(palette)) as_discrete_pal(palette) |
| 256 | + discrete_scale( |
| 257 | + aesthetics, palette = palette, na.value = na.value, |
152 | 258 | ...
|
153 | 259 | )
|
154 | 260 | }
|
|
0 commit comments