Skip to content

Commit c0e9b80

Browse files
committed
collect all default colour scales in one place
1 parent 73b4119 commit c0e9b80

File tree

2 files changed

+88
-89
lines changed

2 files changed

+88
-89
lines changed

R/scale-colour.R

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,94 @@ scale_fill_binned <- function(..., aesthetics = "fill", guide = "coloursteps",
154154
)
155155
}
156156

157+
#' Discrete colour scales
158+
#'
159+
#' The default discrete colour scale. Defaults to [scale_fill_hue()]/[scale_fill_brewer()]
160+
#' unless `type` (which defaults to the `ggplot2.discrete.fill`/`ggplot2.discrete.colour` options)
161+
#' is specified.
162+
#'
163+
#' @param ... Additional parameters passed on to the scale type,
164+
#' @inheritParams discrete_scale
165+
#' @param type One of the following:
166+
#' * A character vector of color codes. The codes are used for a 'manual' color
167+
#' scale as long as the number of codes exceeds the number of data levels
168+
#' (if there are more levels than codes, [scale_colour_hue()]/[scale_fill_hue()]
169+
#' are used to construct the default scale). If this is a named vector, then the color values
170+
#' will be matched to levels based on the names of the vectors. Data values that
171+
#' don't match will be set as `na.value`.
172+
#' * A list of character vectors of color codes. The minimum length vector that exceeds the
173+
#' number of data levels is chosen for the color scaling. This is useful if you
174+
#' want to change the color palette based on the number of levels.
175+
#' * A function that returns a discrete colour/fill scale (e.g., [scale_fill_hue()],
176+
#' [scale_fill_brewer()], etc).
177+
#' @export
178+
#' @seealso
179+
#' The `r link_book("discrete colour scales section", "scales-colour#sec-colour-discrete")`
180+
#' @examples
181+
#' # Template function for creating densities grouped by a variable
182+
#' cty_by_var <- function(var) {
183+
#' ggplot(mpg, aes(cty, colour = factor({{var}}), fill = factor({{var}}))) +
184+
#' geom_density(alpha = 0.2)
185+
#' }
186+
#'
187+
#' # The default, scale_fill_hue(), is not colour-blind safe
188+
#' cty_by_var(class)
189+
#'
190+
#' # (Temporarily) set the default to Okabe-Ito (which is colour-blind safe)
191+
#' okabe <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
192+
#' withr::with_options(
193+
#' list(ggplot2.discrete.fill = okabe),
194+
#' print(cty_by_var(class))
195+
#' )
196+
#'
197+
#' # Define a collection of palettes to alter the default based on number of levels to encode
198+
#' discrete_palettes <- list(
199+
#' c("skyblue", "orange"),
200+
#' RColorBrewer::brewer.pal(3, "Set2"),
201+
#' RColorBrewer::brewer.pal(6, "Accent")
202+
#' )
203+
#' withr::with_options(
204+
#' list(ggplot2.discrete.fill = discrete_palettes), {
205+
#' # 1st palette is used when there 1-2 levels (e.g., year)
206+
#' print(cty_by_var(year))
207+
#' # 2nd palette is used when there are 3 levels
208+
#' print(cty_by_var(drv))
209+
#' # 3rd palette is used when there are 4-6 levels
210+
#' print(cty_by_var(fl))
211+
#' })
212+
#'
213+
scale_colour_discrete <- function(..., aesthetics = "colour", na.value = "grey50",
214+
type = getOption("ggplot2.discrete.colour")) {
215+
if (!is.null(type)) {
216+
scale <- scale_backward_compatibility(
217+
..., na.value = na.value, scale = type,
218+
aesthetic = "colour", type = "discrete"
219+
)
220+
return(scale)
221+
}
222+
discrete_scale(
223+
aesthetics, palette = NULL, na.value = na.value,
224+
...
225+
)
226+
}
227+
228+
#' @rdname scale_colour_discrete
229+
#' @export
230+
scale_fill_discrete <- function(..., aesthetics = "fill", na.value = "grey50",
231+
type = getOption("ggplot2.discrete.fill")) {
232+
if (!is.null(type)) {
233+
scale <- scale_backward_compatibility(
234+
..., na.value = na.value, scale = type,
235+
aesthetic = "fill", type = "discrete"
236+
)
237+
return(scale)
238+
}
239+
discrete_scale(
240+
aesthetics, palette = NULL, na.value = na.value,
241+
...
242+
)
243+
}
244+
157245
# helper function to make sure that the provided scale is of the correct
158246
# type (i.e., is continuous and works with the provided aesthetic)
159247
check_scale_type <- function(scale, name, aesthetic, scale_is_discrete = FALSE, call = caller_env()) {

R/scale-hue.R

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -78,95 +78,6 @@ scale_fill_hue <- function(name = waiver(), ..., h = c(0, 360) + 15, c = 100,
7878
)
7979
}
8080

81-
82-
#' Discrete colour scales
83-
#'
84-
#' The default discrete colour scale. Defaults to [scale_fill_hue()]/[scale_fill_brewer()]
85-
#' unless `type` (which defaults to the `ggplot2.discrete.fill`/`ggplot2.discrete.colour` options)
86-
#' is specified.
87-
#'
88-
#' @param ... Additional parameters passed on to the scale type,
89-
#' @inheritParams discrete_scale
90-
#' @param type One of the following:
91-
#' * A character vector of color codes. The codes are used for a 'manual' color
92-
#' scale as long as the number of codes exceeds the number of data levels
93-
#' (if there are more levels than codes, [scale_colour_hue()]/[scale_fill_hue()]
94-
#' are used to construct the default scale). If this is a named vector, then the color values
95-
#' will be matched to levels based on the names of the vectors. Data values that
96-
#' don't match will be set as `na.value`.
97-
#' * A list of character vectors of color codes. The minimum length vector that exceeds the
98-
#' number of data levels is chosen for the color scaling. This is useful if you
99-
#' want to change the color palette based on the number of levels.
100-
#' * A function that returns a discrete colour/fill scale (e.g., [scale_fill_hue()],
101-
#' [scale_fill_brewer()], etc).
102-
#' @export
103-
#' @seealso
104-
#' The `r link_book("discrete colour scales section", "scales-colour#sec-colour-discrete")`
105-
#' @examples
106-
#' # Template function for creating densities grouped by a variable
107-
#' cty_by_var <- function(var) {
108-
#' ggplot(mpg, aes(cty, colour = factor({{var}}), fill = factor({{var}}))) +
109-
#' geom_density(alpha = 0.2)
110-
#' }
111-
#'
112-
#' # The default, scale_fill_hue(), is not colour-blind safe
113-
#' cty_by_var(class)
114-
#'
115-
#' # (Temporarily) set the default to Okabe-Ito (which is colour-blind safe)
116-
#' okabe <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
117-
#' withr::with_options(
118-
#' list(ggplot2.discrete.fill = okabe),
119-
#' print(cty_by_var(class))
120-
#' )
121-
#'
122-
#' # Define a collection of palettes to alter the default based on number of levels to encode
123-
#' discrete_palettes <- list(
124-
#' c("skyblue", "orange"),
125-
#' RColorBrewer::brewer.pal(3, "Set2"),
126-
#' RColorBrewer::brewer.pal(6, "Accent")
127-
#' )
128-
#' withr::with_options(
129-
#' list(ggplot2.discrete.fill = discrete_palettes), {
130-
#' # 1st palette is used when there 1-2 levels (e.g., year)
131-
#' print(cty_by_var(year))
132-
#' # 2nd palette is used when there are 3 levels
133-
#' print(cty_by_var(drv))
134-
#' # 3rd palette is used when there are 4-6 levels
135-
#' print(cty_by_var(fl))
136-
#' })
137-
#'
138-
scale_colour_discrete <- function(..., aesthetics = "colour", na.value = "grey50",
139-
type = getOption("ggplot2.discrete.colour")) {
140-
if (!is.null(type)) {
141-
scale <- scale_backward_compatibility(
142-
..., na.value = na.value, scale = type,
143-
aesthetic = "colour", type = "discrete"
144-
)
145-
return(scale)
146-
}
147-
discrete_scale(
148-
aesthetics, palette = NULL, na.value = na.value,
149-
...
150-
)
151-
}
152-
153-
#' @rdname scale_colour_discrete
154-
#' @export
155-
scale_fill_discrete <- function(..., aesthetics = "fill", na.value = "grey50",
156-
type = getOption("ggplot2.discrete.fill")) {
157-
if (!is.null(type)) {
158-
scale <- scale_backward_compatibility(
159-
..., na.value = na.value, scale = type,
160-
aesthetic = "fill", type = "discrete"
161-
)
162-
return(scale)
163-
}
164-
discrete_scale(
165-
aesthetics, palette = NULL, na.value = na.value,
166-
...
167-
)
168-
}
169-
17081
scale_colour_qualitative <- function(name = waiver(), ..., type = NULL,
17182
h = c(0, 360) + 15, c = 100, l = 65,
17283
h.start = 0, direction = 1,

0 commit comments

Comments
 (0)