Debian Codegraph @

R/scale_sequential.R

raw
1 #' HCL-Based Discrete Sequential Color Scales for ggplot2
2 #'
3 #' Discrete ggplot2 color scales using the color palettes generated by \code{\link{sequential_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 #' Compared to \code{\link{sequential_hcl}} the ordering of the colors in the sequential ggplot2 scale
10 #' are reversed by default (i.e., \code{rev = TRUE}) to be more consistent with ggplot2's own scales such as
11 #' \code{\link[ggplot2]{scale_color_brewer}}. For most named palettes this leads to darker and more
12 #' colorful colors for larger values on the scale. This is typically the better default on light/white
13 #' backgrounds.
14 #'
15 #' @param c1 Beginning chroma value.
16 #' @param c2 Ending chroma value.
17 #' @param cmax Maximum chroma value.
18 #' @param l1 Beginning luminance value.
19 #' @param l2 Ending luminance value.
20 #' @param h1 Beginning hue value.
21 #' @param h2 Ending hue value. If set to \code{NA}, generates a single-hue scale.
22 #' @param p1 Control parameter determining how chroma should vary (1 = linear, 2 = quadratic, etc.).
23 #' @param p2 Control parameter determining how luminance should vary (1 = linear, 2 = quadratic, etc.).
24 #' @param alpha Numeric vector of values in the range \code{[0, 1]} for alpha transparency channel (0 means transparent and 1 means opaque).
25 #' @param rev If \code{TRUE} (default), reverses the order of the colors in the color scale (compared to \code{\link{sequential_hcl}}).
26 #' @param palette The name of the palette to be used. Run \code{hcl_palettes(type = "sequential")} for available options.
27 #' @param nmax Maximum number of different colors the palette should contain. If not provided, is calculated automatically
28 #' from the data.
29 #' @param order Numeric vector listing the order in which the colors should be used. Default is \code{1:nmax}.
30 #' @param aesthetics The ggplot2 aesthetics to which this scale should be applied.
31 #' @param ... common discrete scale parameters: \code{name}, \code{breaks}, \code{labels}, \code{na.value}, \code{limits} and \code{guide}. See
32 #' \code{\link[ggplot2]{discrete_scale}} for more details.
33 #' @examples
34 #' library("ggplot2")
35 #'
36 #' # default colors
37 #' ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
38 #' geom_point() + scale_color_discrete_sequential() + theme_classic()
39 #'
40 #' # customization of named palette
41 #' ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
42 #' geom_point() + scale_colour_discrete_sequential(palette = "Reds", nmax = 4, p2 = 1.5) +
43 #' theme_classic()
44 #'
45 #' # color scale "Terrain"
46 #' ggplot(iris, aes(Sepal.Length, fill = Species)) +
47 #' geom_density(alpha = 0.7) + scale_fill_discrete_sequential(palette = "Terrain") + theme_minimal()
48 #' @importFrom stats na.omit
49 #' @export
50 scale_colour_discrete_sequential <- function(palette = NULL, c1 = NULL, c2 = NULL, cmax = NULL, l1 = NULL, l2 = NULL,
51 h1 = NULL, h2 = NULL, p1 = NULL, p2 = NULL, alpha = 1, rev = TRUE,
52 nmax = NULL, order = NULL, aesthetics = "colour", ...)
53 {
54 # arguments we want to hand off to function sequential_hcl only if explicitly provided
55 hcl_args <- c("palette", "c1", "c2", "cmax", "l1", "l2", "h1", "h2", "p1", "p2")
56
57 # match hcl_args to args provided
58 args <- as.list(match.call())
59 args[[1]] <- NULL # remove the function call
60 args <- args[na.omit(match(hcl_args, names(args)))] # remove other args
61 for(n in names(args)) args[[n]] <- get(n) # force evaluation of args
62
63 pal <- function(n) {
64 if (is.null(nmax)) nmax <- n
65 if (is.null(order)) order <- 1:n
66
67 if (n > nmax) {
68 warning("Insufficient values in scale_colour_discrete_sequential. ", n, " needed but only ",
69 nmax, " provided.", call. = FALSE)
70 }
71 # set the remaining arguments and call qualitative_hcl
72 args <- c(args, list(n = nmax, alpha = alpha, rev = rev))
73 do.call(sequential_hcl, args, envir = parent.frame())[order]
74 }
75 ggplot2::discrete_scale(aesthetics, "manual", pal, ...)
76 }
77
78 #' @rdname scale_colour_discrete_sequential
79 #' @export
80 scale_color_discrete_sequential <- scale_colour_discrete_sequential
81
82 #' @rdname scale_colour_discrete_sequential
83 #' @export
84 scale_fill_discrete_sequential <- function(..., aesthetics = "fill")
85 {
86 args <- as.list(match.call())
87 args[[1]] <- NULL # remove the function call
88 if (is.null(args[["aesthetics"]])) args$aesthetics <- "fill"
89 do.call(scale_colour_discrete_sequential, args, envir = parent.frame())
90 }
91
92 #' HCL-Based Continuous Sequential Color Scales for ggplot2
93 #'
94 #' Continuous ggplot2 color scales using the color palettes generated by \code{\link{sequential_hcl}}.
95 #'
96 #'
97 #' If both a valid palette name and palette parameters are provided then the provided palette parameters overwrite the parameters in the
98 #' named palette. This enables easy customization of named palettes.
99 #'
100 #' Compared to \code{\link{sequential_hcl}} the ordering of the colors in the sequential ggplot2 scale
101 #' are reversed by default (i.e., \code{rev = TRUE}) to be more consistent with ggplot2's own scales such as
102 #' \code{\link[ggplot2]{scale_color_brewer}}. For most named palettes this leads to darker and more
103 #' colorful colors for larger values on the scale. This is typically the better default on light/white
104 #' backgrounds.
105 #'
106 #' @inheritParams scale_colour_discrete_sequential
107 #' @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.
108 #' @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.
109 #' @param na.value Color to be used for missing data points.
110 #' @param guide Type of legend. Use \code{"colourbar"} for continuous color bar.
111 #' @param n_interp Number of discrete colors that should be used to interpolate the continuous color scale. 11 will work fine in most cases.
112 #' @param ... common continuous scale parameters: `name`, `breaks`, `labels`, and `limits`. See
113 #' \code{\link[ggplot2]{continuous_scale}} for more details.
114 #' @examples
115 #' library("ggplot2")
116 #'
117 #' # base plot
118 #' gg <- ggplot(iris, aes(x = Species, y = Sepal.Width, color = Sepal.Length)) +
119 #' geom_jitter(width = 0.3) + theme_minimal()
120 #'
121 #' # default settings
122 #' gg + scale_color_continuous_sequential()
123 #'
124 #' # switch palette and overwrite some default values
125 #' gg + scale_color_continuous_sequential(palette = "Reds", l1 = 20, c2 = 70, p1 = 1)
126 #'
127 #' # select a range out of the entire palette
128 #' gg + scale_color_continuous_sequential(palette = "Heat", begin = 0.2, end = 0.8)
129 #'
130 #' # volcano plot
131 #' df <- data.frame(height = c(volcano), x = c(row(volcano)), y = c(col(volcano)))
132 #' ggplot(df, aes(x, y, fill = height)) +
133 #' geom_raster() + scale_fill_continuous_sequential(palette = "Terrain", rev = FALSE) +
134 #' coord_fixed(expand = FALSE)
135 #' @importFrom stats na.omit
136 #' @export
137 scale_colour_continuous_sequential <- function(palette = NULL, c1 = NULL, c2 = NULL, cmax = NULL, l1 = NULL, l2 = NULL,
138 h1 = NULL, h2 = NULL, p1 = NULL, p2 = NULL, alpha = 1, rev = TRUE,
139 begin = 0, end = 1, na.value = "grey50", guide = "colourbar",
140 aesthetics = "colour", n_interp = 11, ...)
141 {
142 # arguments we want to hand off to function sequential_hcl only if explicitly provided
143 hcl_args <- c("palette", "c1", "c2", "cmax", "l1", "l2", "h1", "h2", "p1", "p2")
144
145 # match hcl_args to args provided
146 args <- as.list(match.call())
147 args[[1]] <- NULL # remove the function call
148 args <- args[na.omit(match(hcl_args, names(args)))] # remove other args
149
150 # set the remaining arguments and call qualitative_hcl
151 args <- c(args, list(n = n_interp, alpha = alpha, rev = rev))
152 colors <- do.call(sequential_hcl, args, envir = parent.frame())
153
154 ggplot2::continuous_scale(aesthetics, "continuous_sequential",
155 scales::gradient_n_pal(colors, values = NULL),
156 na.value = na.value, guide = guide,
157 rescaler = to_rescaler(begin, end), ...)
158 }
159
160 #' @rdname scale_colour_continuous_sequential
161 #' @export
162 scale_color_continuous_sequential <- scale_colour_continuous_sequential
163
164 #' @rdname scale_colour_continuous_sequential
165 #' @export
166 scale_fill_continuous_sequential <- function(..., aesthetics = "fill")
167 {
168 args <- as.list(match.call())
169 args[[1]] <- NULL # remove the function call
170 if (is.null(args[["aesthetics"]])) args$aesthetics <- "fill"
171 do.call(scale_colour_continuous_sequential, args, envir = parent.frame())
172 }
173
174
175 #' HCL-Based Binned Sequential Color Scales for ggplot2
176 #'
177 #' Binned ggplot2 color scales using the color palettes generated by \code{\link{sequential_hcl}}.
178 #'
179 #'
180 #' If both a valid palette name and palette parameters are provided then the provided palette parameters overwrite the parameters in the
181 #' named palette. This enables easy customization of named palettes.
182 #'
183 #' Compared to \code{\link{sequential_hcl}} the ordering of the colors in the sequential ggplot2 scale
184 #' are reversed by default (i.e., \code{rev = TRUE}) to be more consistent with ggplot2's own scales such as
185 #' \code{\link[ggplot2]{scale_color_fermenter}}. For most named palettes this leads to darker and more
186 #' colorful colors for larger values on the scale. This is typically the better default on light/white
187 #' backgrounds.
188 #'
189 #' @inheritParams scale_colour_discrete_sequential
190 #' @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.
191 #' @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.
192 #' @param na.value Color to be used for missing data points.
193 #' @param guide Type of legend. Use \code{"coloursteps"} for color bar with discrete steps.
194 #' @param n_interp Number of discrete colors that should be used to interpolate the binned color scale. 11 will work fine in most cases.
195 #' @param ... common binned scale parameters: `name`, `breaks`, `labels`, and `limits`. See
196 #' \code{\link[ggplot2]{binned_scale}} for more details.
197 #' @examples
198 #' library("ggplot2")
199 #'
200 #' # volcano plot
201 #' df <- data.frame(height = c(volcano), x = c(row(volcano)), y = c(col(volcano)))
202 #' ggplot(df, aes(x, y, fill = height)) +
203 #' geom_raster() + scale_fill_binned_sequential(palette = "Terrain", rev = FALSE) +
204 #' coord_fixed(expand = FALSE)
205 #' @importFrom stats na.omit
206 #' @export
207 scale_colour_binned_sequential <- function(palette = NULL, c1 = NULL, c2 = NULL, cmax = NULL, l1 = NULL, l2 = NULL,
208 h1 = NULL, h2 = NULL, p1 = NULL, p2 = NULL, alpha = 1, rev = TRUE,
209 begin = 0, end = 1, na.value = "grey50", guide = "coloursteps",
210 aesthetics = "colour", n_interp = 11, ...)
211 {
212 # arguments we want to hand off to function sequential_hcl only if explicitly provided
213 hcl_args <- c("palette", "c1", "c2", "cmax", "l1", "l2", "h1", "h2", "p1", "p2")
214
215 # match hcl_args to args provided
216 args <- as.list(match.call())
217 args[[1]] <- NULL # remove the function call
218 args <- args[na.omit(match(hcl_args, names(args)))] # remove other args
219
220 # set the remaining arguments and call qualitative_hcl
221 args <- c(args, list(n = n_interp, alpha = alpha, rev = rev))
222 colors <- do.call(sequential_hcl, args, envir = parent.frame())
223
224 ggplot2::binned_scale(
225 aesthetics,
226 "binned_sequential",
227 scales::gradient_n_pal(colors, values = NULL),
228 na.value = na.value,
229 guide = guide,
230 rescaler = to_rescaler(begin, end),
231 ...
232 )
233
234 }
235
236 #' @rdname scale_colour_binned_sequential
237 #' @export
238 scale_color_binned_sequential <- scale_colour_binned_sequential
239
240 #' @rdname scale_colour_binned_sequential
241 #' @export
242 scale_fill_binned_sequential <- function(..., aesthetics = "fill")
243 {
244 args <- as.list(match.call())
245 args[[1]] <- NULL # remove the function call
246 if (is.null(args[["aesthetics"]])) args$aesthetics <- "fill"
247 do.call(scale_colour_binned_sequential, args, envir = parent.frame())
248 }
249
250
251