|
63 | 63 | #' see the [paper on the colorspace package](https://arxiv.org/abs/1903.06490)
|
64 | 64 | #' and references therein.
|
65 | 65 | #' @examples
|
66 |
| -#' v <- ggplot(faithfuld, aes(waiting, eruptions, fill = density)) + |
67 |
| -#' geom_tile() |
68 |
| -#' v |
| 66 | +#' # A standard plot |
| 67 | +#' p <- ggplot(mpg, aes(displ, hwy, colour = cty)) + |
| 68 | +#' geom_point() |
69 | 69 | #'
|
70 |
| -#' v + scale_fill_continuous(type = "gradient") |
71 |
| -#' v + scale_fill_continuous(type = "viridis") |
| 70 | +#' # You can use the scale to give a palette directly |
| 71 | +#' p + scale_colour_continuous(palette = c("#FEE0D2", "#FC9272", "#DE2D26")) |
72 | 72 | #'
|
73 |
| -#' # The above are equivalent to |
74 |
| -#' v + scale_fill_gradient() |
75 |
| -#' v + scale_fill_viridis_c() |
| 73 | +#' # The default colours are encoded into the theme |
| 74 | +#' p + theme(palette.colour.continuous = c("#DEEBF7", "#9ECAE1", "#3182BD")) |
76 | 75 | #'
|
77 |
| -#' # To make a binned version of this plot |
78 |
| -#' v + scale_fill_binned(type = "viridis") |
| 76 | +#' # You can globally set default colour palette via the theme |
| 77 | +#' old <- update_theme(palette.colour.continuous = c("#E5F5E0", "#A1D99B", "#31A354")) |
79 | 78 | #'
|
80 |
| -#' # Set a different default scale using the options |
81 |
| -#' # mechanism |
82 |
| -#' tmp <- getOption("ggplot2.continuous.fill") # store current setting |
83 |
| -#' options(ggplot2.continuous.fill = scale_fill_distiller) |
84 |
| -#' v |
85 |
| -#' options(ggplot2.continuous.fill = tmp) # restore previous setting |
| 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) |
86 | 88 | #' @export
|
87 | 89 | scale_colour_continuous <- function(..., palette = NULL, aesthetics = "colour",
|
88 | 90 | guide = "colourbar", na.value = "grey50",
|
@@ -190,38 +192,24 @@ scale_fill_binned <- function(..., palette = NULL, aesthetics = "fill", guide =
|
190 | 192 | #' @seealso
|
191 | 193 | #' The `r link_book("discrete colour scales section", "scales-colour#sec-colour-discrete")`
|
192 | 194 | #' @examples
|
193 |
| -#' # Template function for creating densities grouped by a variable |
194 |
| -#' cty_by_var <- function(var) { |
195 |
| -#' ggplot(mpg, aes(cty, colour = factor({{var}}), fill = factor({{var}}))) + |
196 |
| -#' geom_density(alpha = 0.2) |
197 |
| -#' } |
| 195 | +#' # A standard plot |
| 196 | +#' p <- ggplot(mpg, aes(displ, hwy, colour = class)) + |
| 197 | +#' geom_point() |
| 198 | +#' |
| 199 | +#' # You can use the scale to give a palette directly |
| 200 | +#' p + scale_colour_discrete(palette = pal_brewer(palette = "Dark2")) |
198 | 201 | #'
|
199 |
| -#' # The default, scale_fill_hue(), is not colour-blind safe |
200 |
| -#' cty_by_var(class) |
| 202 | +#' # The default colours are encoded into the theme |
| 203 | +#' p + theme(palette.colour.discrete = pal_grey()) |
201 | 204 | #'
|
202 |
| -#' # (Temporarily) set the default to Okabe-Ito (which is colour-blind safe) |
203 |
| -#' okabe <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7") |
204 |
| -#' withr::with_options( |
205 |
| -#' list(ggplot2.discrete.fill = okabe), |
206 |
| -#' print(cty_by_var(class)) |
207 |
| -#' ) |
| 205 | +#' # You can globally set default colour palette via the theme |
| 206 | +#' old <- update_theme(palette.colour.discrete = pal_viridis()) |
208 | 207 | #'
|
209 |
| -#' # Define a collection of palettes to alter the default based on number of levels to encode |
210 |
| -#' discrete_palettes <- list( |
211 |
| -#' c("skyblue", "orange"), |
212 |
| -#' RColorBrewer::brewer.pal(3, "Set2"), |
213 |
| -#' RColorBrewer::brewer.pal(6, "Accent") |
214 |
| -#' ) |
215 |
| -#' withr::with_options( |
216 |
| -#' list(ggplot2.discrete.fill = discrete_palettes), { |
217 |
| -#' # 1st palette is used when there 1-2 levels (e.g., year) |
218 |
| -#' print(cty_by_var(year)) |
219 |
| -#' # 2nd palette is used when there are 3 levels |
220 |
| -#' print(cty_by_var(drv)) |
221 |
| -#' # 3rd palette is used when there are 4-6 levels |
222 |
| -#' print(cty_by_var(fl)) |
223 |
| -#' }) |
| 208 | +#' # Plot now shows new global default |
| 209 | +#' p |
224 | 210 | #'
|
| 211 | +#' # Restoring the previous theme |
| 212 | +#' theme_set(old) |
225 | 213 | scale_colour_discrete <- function(..., palette = NULL, aesthetics = "colour", na.value = "grey50",
|
226 | 214 | type = getOption("ggplot2.discrete.colour")) {
|
227 | 215 | if (!is.null(type) && is.null(palette)) {
|
|
0 commit comments