Debian Codegraph @

R/scale_qualitative.R

raw
1 #' HCL-Based Discrete Qualitative Color Scales for ggplot2
2 #'
3 #' Discrete ggplot2 color scales using the color palettes generated by \code{\link{qualitative_hcl}}.
4 #'
5 #'
6 #' If both a valid palette name and palette parameters are provided then the provided palette parameters overwrite the parameters in the
7 #' named palette. This enables easy customization of named palettes.
8 #'
9 #' @param c1 Chroma value, used for all colors in the scale.
10 #' @param l1 Luminance value, used for all colors in the scale.
11 #' @param h1 Beginning hue value.
12 #' @param h2 Ending hue value.
13 #' @param alpha Numeric vector of values in the range \code{[0, 1]} for alpha transparency channel (0 means transparent and 1 means opaque).
14 #' @param rev If \code{TRUE}, reverses the order of the colors in the color scale.
15 #' @param palette The name of the palette to be used. Run \code{hcl_palettes(type = "qualitative")} for available options.
16 #' @param nmax Maximum number of different colors the palette should contain. If not provided, is calculated automatically
17 #' from the data.
18 #' @param order Numeric vector listing the order in which the colors should be used. Default is \code{1:nmax}.
19 #' @param aesthetics The ggplot2 aesthetics to which this scale should be applied.
20 #' @param ... common discrete scale parameters: \code{name}, \code{breaks}, \code{labels}, \code{na.value}, \code{limits} and \code{guide}. See
21 #' \code{\link[ggplot2]{discrete_scale}} for more details.
22 #' @examples
23 #' library("ggplot2")
24 #'
25 #' # default colors
26 #' ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
27 #' geom_point() + theme_minimal() +
28 #' scale_color_discrete_qualitative()
29 #'
30 #' # color scale "Harmonic"
31 #' ggplot(iris, aes(Sepal.Length, fill = Species)) +
32 #' geom_density(alpha = 0.7) + scale_fill_discrete_qualitative(palette = "Harmonic")
33 #' @importFrom stats na.omit
34 #' @export
35 scale_colour_discrete_qualitative <- function(palette = NULL, c1 = NULL, l1 = NULL, h1 = NULL, h2 = NULL,
36 alpha = 1, rev = FALSE, nmax = NULL, order = NULL, aesthetics = "colour",
37 ...)
38 {
39 # arguments we want to hand off to function qualitative_hcl only if explicitly provided
40 hcl_args <- c("palette", "c1", "l1", "h1", "h2")
41
42 # match hcl_args to args provided
43 args <- as.list(match.call())
44 args[[1]] <- NULL # remove the function call
45 args <- args[na.omit(match(hcl_args, names(args)))] # remove other args
46 for(n in names(args)) args[[n]] <- get(n) # force evaluation of args
47
48 pal <- function(n) {
49 if (is.null(nmax)) nmax <- n
50 if (is.null(order)) order <- 1:n
51
52 if (n > nmax) {
53 warning("Insufficient values in scale_colour_discrete_qualitative. ", n, " needed but only ",
54 nmax, " provided.", call. = FALSE)
55 }
56 # set the remaining arguments and call qualitative_hcl
57 args <- c(args, list(n = nmax, alpha = alpha, rev = rev))
58 do.call(qualitative_hcl, args, envir = parent.frame())[order]
59 }
60 ggplot2::discrete_scale(aesthetics, "manual", pal, ...)
61 }
62
63 #' @rdname scale_colour_discrete_qualitative
64 #' @export
65 scale_color_discrete_qualitative <- scale_colour_discrete_qualitative
66
67 #' @rdname scale_colour_discrete_qualitative
68 #' @export
69 scale_fill_discrete_qualitative <- function(..., aesthetics = "fill")
70 {
71 args <- as.list(match.call())
72 args[[1]] <- NULL # remove the function call
73 if (is.null(args[["aesthetics"]])) args$aesthetics <- "fill"
74 do.call(scale_colour_discrete_qualitative, args, envir = parent.frame())
75 }
76
77 #' HCL-Based Continuous Qualitative Color Scales for ggplot2
78 #'
79 #' Continuous ggplot2 color scales using the color palettes generated by \code{\link{qualitative_hcl}}. These scales are provided
80 #' for completeness. It is not normally a good idea to color a continuous variable using a qualitative scale.
81 #'
82 #' If both a valid palette name and palette parameters are provided then the provided palette parameters overwrite the parameters in the
83 #' named palette. This enables easy customization of named palettes.
84 #'
85 #' @inheritParams scale_colour_discrete_qualitative
86 #' @param begin Number in the range of \code{[0, 1]} indicating to which point in the color scale the smallest data value should be mapped.
87 #' @param end Number in the range of \code{[0, 1]} indicating to which point in the color scale the largest data value should be mapped.
88 #' @param na.value Color to be used for missing data points.
89 #' @param guide Type of legend. Use \code{"colourbar"} for continuous color bar.
90 #' @param n_interp Number of discrete colors that should be used to interpolate the continuous color scale. 11 will work fine in most cases.
91 #' @param ... common continuous scale parameters: `name`, `breaks`, `labels`, and `limits`. See
92 #' \code{\link[ggplot2]{continuous_scale}} for more details.
93 #' @examples
94 #' library("ggplot2")
95 #'
96 #' # none of these examples are necessarily good ideas
97 #' gg <- ggplot(iris, aes(x = Species, y = Sepal.Width, color = Sepal.Length)) +
98 #' geom_jitter(width = 0.3) + theme_minimal()
99 #'
100 #' gg + scale_color_continuous_qualitative(palette = "Warm")
101 #' gg + scale_color_continuous_qualitative(palette = "Cold", l1 = 60)
102 #' gg + scale_color_continuous_qualitative(palette = "Harmonic", rev = TRUE)
103 #'
104 #' nx = 87
105 #' ny = 61
106 #' df <- data.frame(height = c(volcano), x = rep(1:nx, ny), y = rep(1:ny, each = nx))
107 #' ggplot(df, aes(x, y, fill=height)) +
108 #' geom_raster() + scale_fill_continuous_qualitative(palette = "Dark 3") +
109 #' coord_fixed(expand = FALSE)
110 #' @importFrom stats na.omit
111 #' @export
112 scale_colour_continuous_qualitative <- function(palette = NULL, c1 = NULL, l1 = NULL, h1 = NULL, h2 = NULL,
113 alpha = 1, rev = FALSE, begin = 0, end = 1, na.value = "grey50",
114 guide = "colourbar", aesthetics = "colour", n_interp = 11, ...)
115 {
116 # arguments we want to hand off to function qualitative_hcl only if explicitly provided
117 hcl_args <- c("palette", "c1", "l1", "h1", "h2")
118
119 # match hcl_args to args provided
120 args <- as.list(match.call())
121 args[[1]] <- NULL # remove the function call
122 args <- args[na.omit(match(hcl_args, names(args)))] # remove other args
123
124 # set the remaining arguments and call qualitative_hcl
125 args <- c(args, list(n = n_interp, alpha = alpha, rev = rev))
126 colors <- do.call(qualitative_hcl, args, envir = parent.frame())
127
128 ggplot2::continuous_scale(aesthetics, "continuous_qualitative",
129 scales::gradient_n_pal(colors, values = NULL),
130 na.value = na.value, guide = guide,
131 rescaler = to_rescaler(begin, end), ...)
132 }
133
134 #' @rdname scale_colour_continuous_qualitative
135 #' @export
136 scale_color_continuous_qualitative <- scale_colour_continuous_qualitative
137
138 #' @rdname scale_colour_continuous_qualitative
139 #' @export
140 scale_fill_continuous_qualitative <- function(..., aesthetics = "fill")
141 {
142 args <- as.list(match.call())
143 args[[1]] <- NULL # remove the function call
144 if (is.null(args[["aesthetics"]])) args$aesthetics <- "fill"
145 do.call(scale_colour_continuous_qualitative, args, envir = parent.frame())
146 }
147
148 #' HCL-Based Binned Qualitative Color Scales for ggplot2
149 #'
150 #' Binned ggplot2 color scales using the color palettes generated by \code{\link{qualitative_hcl}}. These scales are provided
151 #' for completeness. It is not normally a good idea to color a continuous, binned variable using a qualitative scale.
152 #'
153 #' If both a valid palette name and palette parameters are provided then the provided palette parameters overwrite the parameters in the
154 #' named palette. This enables easy customization of named palettes.
155 #'
156 #' @inheritParams scale_colour_discrete_qualitative
157 #' @param begin Number in the range of \code{[0, 1]} indicating to which point in the color scale the smallest data value should be mapped.
158 #' @param end Number in the range of \code{[0, 1]} indicating to which point in the color scale the largest data value should be mapped.
159 #' @param na.value Color to be used for missing data points.
160 #' @param guide Type of legend. Use \code{"coloursteps"} for color bar with discrete steps.
161 #' @param n_interp Number of discrete colors that should be used to interpolate the binned color scale. 11 will work fine in most cases.
162 #' @param ... common binned scale parameters: `name`, `breaks`, `labels`, and `limits`. See
163 #' \code{\link[ggplot2]{binned_scale}} for more details.
164 #' @examples
165 #' library("ggplot2")
166 #'
167 #' # none of these examples are necessarily good ideas
168 #' gg <- ggplot(iris, aes(x = Species, y = Sepal.Width, color = Sepal.Length)) +
169 #' geom_jitter(width = 0.3) + theme_minimal()
170 #'
171 #' gg + scale_color_binned_qualitative(palette = "Dynamic")
172 #' gg + scale_color_binned_qualitative(palette = "Dark3", l1 = 70)
173 #'
174 #' nx = 87
175 #' ny = 61
176 #' df <- data.frame(height = c(volcano), x = rep(1:nx, ny), y = rep(1:ny, each = nx))
177 #' ggplot(df, aes(x, y, fill=height)) +
178 #' geom_raster() + scale_fill_binned_qualitative(palette = "Dark 3") +
179 #' coord_fixed(expand = FALSE)
180 #' @importFrom stats na.omit
181 #' @export
182 scale_colour_binned_qualitative <- function(palette = NULL, c1 = NULL, l1 = NULL, h1 = NULL, h2 = NULL,
183 alpha = 1, rev = FALSE, begin = 0, end = 1, na.value = "grey50",
184 guide = "coloursteps", aesthetics = "colour", n_interp = 11, ...)
185 {
186 # arguments we want to hand off to function qualitative_hcl only if explicitly provided
187 hcl_args <- c("palette", "c1", "l1", "h1", "h2")
188
189 # match hcl_args to args provided
190 args <- as.list(match.call())
191 args[[1]] <- NULL # remove the function call
192 args <- args[na.omit(match(hcl_args, names(args)))] # remove other args
193
194 # set the remaining arguments and call qualitative_hcl
195 args <- c(args, list(n = n_interp, alpha = alpha, rev = rev))
196 colors <- do.call(qualitative_hcl, args, envir = parent.frame())
197
198 ggplot2::binned_scale(
199 aesthetics, "binned_qualitative",
200 scales::gradient_n_pal(colors, values = NULL),
201 na.value = na.value, guide = guide,
202 rescaler = to_rescaler(begin, end),
203 ...
204 )
205 }
206
207 #' @rdname scale_colour_binned_qualitative
208 #' @export
209 scale_color_binned_qualitative <- scale_colour_binned_qualitative
210
211 #' @rdname scale_colour_binned_qualitative
212 #' @export
213 scale_fill_binned_qualitative <- function(..., aesthetics = "fill")
214 {
215 args <- as.list(match.call())
216 args[[1]] <- NULL # remove the function call
217 if (is.null(args[["aesthetics"]])) args$aesthetics <- "fill"
218 do.call(scale_colour_binned_qualitative, args, envir = parent.frame())
219 }
220