@@ -66,10 +66,9 @@ scale_fill_hue <- function(..., h = c(0, 360) + 15, c = 100, l = 65, h.start = 0
66
66
67
67
# ' Discrete colour scales
68
68
# '
69
- # ' Colour scales for discrete data default to the values of the `ggplot2.discrete.fill`
70
- # ' and `ggplot2.discrete.colour` options. By default these scales attempt to use
71
- # ' a colour-blind safe (or a custom) palette, but if the number of levels is
72
- # ' large, they fallback to [scale_fill_hue()]/[scale_colour_hue()].
69
+ # ' The default discrete colour scale. Defaults to [scale_fill_hue()]/[scale_fill_brewer()]
70
+ # ' unless `type` (which defaults to the `ggplot2.discrete.fill`/`ggplot2.discrete.colour` options)
71
+ # ' is specified.
73
72
# '
74
73
# ' @param ... Additional parameters passed on to the scale type,
75
74
# ' @param type One of the following:
@@ -90,22 +89,32 @@ scale_fill_hue <- function(..., h = c(0, 360) + 15, c = 100, l = 65, h.start = 0
90
89
# ' ggplot(mpg, aes(cty, colour = factor({{var}}), fill = factor({{var}}))) +
91
90
# ' geom_density(alpha = 0.2)
92
91
# ' }
93
- # ' # The default color scale for three levels
92
+ # '
93
+ # ' # The default, scale_fill_hue(), is not colour-blind safe
94
94
# ' cty_by_var(class)
95
95
# '
96
- # ' # Define custom palettes for when there are 1-2, 3, or 4-6 levels
97
- # ' opts <- options(
98
- # ' ggplot2.discrete.fill = list(
99
- # ' c("skyblue", "orange"),
100
- # ' RColorBrewer::brewer.pal(3, "Set2"),
101
- # ' RColorBrewer::brewer.pal(6, "Accent")
102
- # ' )
96
+ # ' # (Temporarily) set the default to Okabe-Ito (which is colour-blind safe)
97
+ # ' okabe <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
98
+ # ' withr::with_options(
99
+ # ' list(ggplot2.discrete.fill = okabe),
100
+ # ' print(cty_by_var(class))
103
101
# ' )
104
- # ' cty_by_var(year)
105
- # ' cty_by_var(drv)
106
- # ' cty_by_var(fl)
107
- # ' cty_by_var(class)
108
- # ' options(opts)
102
+ # '
103
+ # ' # Define a collection of palettes to alter the default based on number of levels to encode
104
+ # ' discrete_palettes <- list(
105
+ # ' c("skyblue", "orange"),
106
+ # ' RColorBrewer::brewer.pal(3, "Set2"),
107
+ # ' RColorBrewer::brewer.pal(6, "Accent")
108
+ # ' )
109
+ # ' withr::with_options(
110
+ # ' list(ggplot2.discrete.fill = discrete_palettes), {
111
+ # ' # 1st palette is used when there 1-2 levels (e.g., year)
112
+ # ' print(cty_by_var(year))
113
+ # ' # 2nd palette is used when there are 3 levels
114
+ # ' print(cty_by_var(drv))
115
+ # ' # 3rd palette is used when there are 4-6 levels
116
+ # ' print(cty_by_var(fl))
117
+ # ' })
109
118
# '
110
119
scale_colour_discrete <- function (... , type = getOption(" ggplot2.discrete.colour" , getOption(" ggplot2.discrete.fill" ))) {
111
120
# TODO: eventually `type` should default to a set of colour-blind safe color codes (e.g. Okabe-Ito)
0 commit comments