Debian Codegraph @

R/scale_divergingx.R

raw
1 #' HCL-Based Discrete Flexible Diverging Scales for ggplot2
2 #'
3 #' Discrete ggplot2 color scales using the color palettes generated by \code{\link{divergingx_hcl}}.
4 #'
5 #' Available CARTO palettes: ArmyRose, Earth, Fall, Geyser, TealRose, Temps, Tropic.
6 #'
7 #' Available ColorBrewer.org palettes: Spectral, PuOr, RdYlGn, RdYlBu, RdGy,
8 #' BrBG, PiYG, PRGn, RdBu.
9 #'
10 #' If both a valid palette name and palette parameters are provided then the provided palette parameters overwrite the parameters in the
11 #' named palette. This enables easy customization of named palettes.
12 #'
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 palette The name of the palette to be used.
15 #' @param rev If \code{TRUE}, reverses the order of the colors in the color scale.
16 #' @param h1,h2,h3,c1,c2,c3,l1,l2,l3,p1,p2,p3,p4,cmax1,cmax2 Parameters to customize the scale. See \code{\link{divergingx_hcl}} for details.
17 #' @param nmax Maximum number of different colors the palette should contain. If not provided, is calculated automatically
18 #' from the data.
19 #' @param order Numeric vector listing the order in which the colors should be used. Default is \code{1:nmax}.
20 #' @param aesthetics The ggplot2 aesthetics to which this scale should be applied.
21 #' @param ... common discrete scale parameters: \code{name}, \code{breaks}, \code{labels}, \code{na.value}, \code{limits} and \code{guide}. See
22 #' \code{\link[ggplot2]{discrete_scale}} for more details.
23 #' @examples
24 #' library("ggplot2")
25 #'
26 #' # default color scale
27 #' ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
28 #' geom_point() + theme_minimal() +
29 #' scale_color_discrete_divergingx()
30 #'
31 #' # color scale "Tropic"
32 #' ggplot(iris, aes(Sepal.Length, fill = Species)) +
33 #' geom_density(alpha = 0.7) + theme_classic() +
34 #' scale_fill_discrete_divergingx(palette = "Tropic", rev = TRUE)
35 #'
36 #' # use `nmax` and `order` to skip some colors
37 #' ggplot(iris, aes(Sepal.Length, fill = Species)) +
38 #' geom_density(alpha = 0.7) + theme_classic() +
39 #' scale_fill_discrete_divergingx(palette = "Tropic", nmax = 5, order = c(1, 4, 5))
40 #' @importFrom stats na.omit
41 #' @export
42 scale_colour_discrete_divergingx <- function(palette = "Geyser", c1 = NULL, c2 = NULL, c3 = NULL,
43 l1 = NULL, l2 = NULL, l3 = NULL, h1 = NULL, h2 = NULL,
44 h3 = NULL, p1 = NULL, p2 = NULL, p3 = NULL, p4 = NULL,
45 cmax1 = NULL, cmax2 = NULL, alpha = 1, rev = FALSE, nmax = NULL, order = NULL,
46 aesthetics = "colour", ...)
47 {
48 # arguments we want to hand off to function divergingx_hcl only if explicitly provided
49 hcl_args <- c("c1", "c2", "c3", "l1", "l2", "l3", "h1", "h2", "h3", "p1", "p2",
50 "p3", "p4", "cmax1", "cmax2")
51
52 # match hcl_args to args provided
53 args <- as.list(match.call())
54 args[[1]] <- NULL # remove the function call
55 args <- args[na.omit(match(hcl_args, names(args)))] # remove other args
56 for(n in names(args)) args[[n]] <- get(n) # force evaluation of args
57
58 pal <- function(n) {
59 if (is.null(nmax)) nmax <- n
60 if (is.null(order)) order <- 1:n
61
62 if (n > nmax) {
63 warning("Insufficient values in scale_colour_discrete_diverging. ", n, " needed but only ",
64 nmax, " provided.", call. = FALSE)
65 }
66 # set the remaining arguments and call qualitative_hcl
67 args <- c(args, list(palette = palette, n = nmax, alpha = alpha, rev = rev))
68 do.call(divergingx_hcl, args, envir = parent.frame())[order]
69 }
70 ggplot2::discrete_scale(aesthetics, "manual", pal, ...)
71 }
72
73 #' @rdname scale_colour_discrete_divergingx
74 #' @export
75 scale_color_discrete_divergingx <- scale_colour_discrete_divergingx
76
77 #' @rdname scale_colour_discrete_divergingx
78 #' @export
79 scale_fill_discrete_divergingx <- function(..., aesthetics = "fill")
80 {
81 args <- as.list(match.call())
82 args[[1]] <- NULL # remove the function call
83 if (is.null(args[["aesthetics"]])) args$aesthetics <- "fill"
84 do.call(scale_colour_discrete_divergingx, args, envir = parent.frame())
85 }
86
87
88
89 #' HCL-Based Continuous Flexible Diverging Scales for ggplot2
90 #'
91 #' Continuous ggplot2 color scales using the color palettes generated by \code{\link{divergingx_hcl}}.
92 #'
93 #' Available CARTO palettes: ArmyRose, Earth, Fall, Geyser, TealRose, Temps, Tropic.
94 #'
95 #' Available ColorBrewer.org palettes: Spectral, PuOr, RdYlGn, RdYlBu, RdGy,
96 #' BrBG, PiYG, PRGn, RdBu.
97 #'
98 #' If both a valid palette name and palette parameters are provided then the provided palette parameters overwrite the parameters in the
99 #' named palette. This enables easy customization of named palettes.
100 #'
101 #' @inheritParams scale_colour_discrete_divergingx
102 #' @param h1,h2,h3,c1,c2,c3,l1,l2,l3,p1,p2,p3,p4,cmax1,cmax2 Parameters to customize the scale. See \code{\link{divergingx_hcl}} for details.
103 #' @param mid Data value that should be mapped to the mid-point of the diverging color scale.
104 #' @param na.value Color to be used for missing data points.
105 #' @param guide Type of legend. Use \code{"colourbar"} for continuous color bar.
106 #' @param n_interp Number of discrete colors that should be used to interpolate the continuous color scale.
107 #' For diverging scales, it is important to use an odd number to capture the color at the midpoint.
108 #' @param ... common continuous scale parameters: `name`, `breaks`, `labels`, and `limits`. See
109 #' \code{\link[ggplot2]{continuous_scale}} for more details.
110 #' @examples
111 #' library("ggplot2")
112 #'
113 #' # volcano plot (difference from mean height)
114 #' nx = 87
115 #' ny = 61
116 #' df <- data.frame(diff = c(volcano) - mean(volcano), x = rep(1:nx, ny), y = rep(1:ny, each = nx))
117 #' ggplot(df, aes(x, y, fill=diff)) +
118 #' geom_raster() + scale_fill_continuous_divergingx(palette = "Fall", rev = TRUE) +
119 #' coord_fixed(expand = FALSE)
120 #'
121 #'
122 #' # adapted from stackoverflow: https://stackoverflow.com/a/20127706/4975218
123 #'
124 #' # generate dataset and base plot
125 #' set.seed(100)
126 #' df <- data.frame(country = LETTERS, V = runif(26, -40, 40))
127 #' df$country = factor(LETTERS, LETTERS[order(df$V)]) # reorder factors
128 #' gg <- ggplot(df, aes(x = country, y = V, fill = V)) +
129 #' geom_bar(stat = "identity") +
130 #' labs(y = "Under/over valuation in %", x = "Country") +
131 #' coord_flip() + theme_minimal()
132 #'
133 #' # plot with diverging scale "Geyser"
134 #' gg + scale_fill_continuous_divergingx(palette = "Geyser")
135 #'
136 #' # plot with diverging scale "ArmyRose"
137 #' gg + scale_fill_continuous_divergingx(palette = "ArmyRose")
138 #' @importFrom stats na.omit
139 #' @export
140 scale_colour_continuous_divergingx <- function(palette = "Geyser", c1 = NULL, c2 = NULL, c3 = NULL,
141 l1 = NULL, l2 = NULL, l3 = NULL, h1 = NULL, h2 = NULL,
142 h3 = NULL, p1 = NULL, p2 = NULL, p3 = NULL, p4 = NULL,
143 cmax1 = NULL, cmax2 = NULL, alpha = 1, rev = FALSE, mid = 0, na.value = "grey50",
144 guide = "colourbar", n_interp = 11, aesthetics = "colour", ...)
145 {
146 # arguments we want to hand off to function divergingx_hcl only if explicitly provided
147 hcl_args <- c("c1", "c2", "c3", "l1", "l2", "l3", "h1", "h2", "h3", "p1", "p2",
148 "p3", "p4", "cmax1", "cmax2")
149
150 # match hcl_args to args provided
151 args <- as.list(match.call())
152 args[[1]] <- NULL # remove the function call
153 args <- args[na.omit(match(hcl_args, names(args)))] # remove other args
154
155 # set the remaining arguments and call qualitative_hcl
156 args <- c(args, list(palette = palette, n = n_interp, alpha = alpha, rev = rev))
157 colors <- do.call(divergingx_hcl, args, envir = parent.frame())
158
159 # diverging scale
160 rescaler = mid_rescaler(mid)
161 ggplot2::continuous_scale(aesthetics, "continuous_divergingx",
162 scales::gradient_n_pal(colors, values = NULL),
163 na.value = na.value, guide = guide,
164 rescaler = rescaler, ...)
165 }
166
167 #' @rdname scale_colour_continuous_divergingx
168 #' @export
169 scale_color_continuous_divergingx <- scale_colour_continuous_divergingx
170
171 #' @rdname scale_colour_continuous_divergingx
172 #' @export
173 scale_fill_continuous_divergingx <- function(..., aesthetics = "fill")
174 {
175 args <- as.list(match.call())
176 args[[1]] <- NULL # remove the function call
177 if (is.null(args[["aesthetics"]])) args$aesthetics <- "fill"
178 do.call(scale_colour_continuous_divergingx, args, envir = parent.frame())
179 }
180
181 #' HCL-Based Binned Flexible Diverging Scales for ggplot2
182 #'
183 #' Binned ggplot2 color scales using the color palettes generated by \code{\link{divergingx_hcl}}.
184 #'
185 #' Available CARTO palettes: ArmyRose, Earth, Fall, Geyser, TealRose, Temps, Tropic.
186 #'
187 #' Available ColorBrewer.org palettes: Spectral, PuOr, RdYlGn, RdYlBu, RdGy,
188 #' BrBG, PiYG, PRGn, RdBu.
189 #'
190 #' If both a valid palette name and palette parameters are provided then the provided palette parameters overwrite the parameters in the
191 #' named palette. This enables easy customization of named palettes.
192 #'
193 #' @inheritParams scale_colour_discrete_divergingx
194 #' @param h1,h2,h3,c1,c2,c3,l1,l2,l3,p1,p2,p3,p4,cmax1,cmax2 Parameters to customize the scale. See \code{\link{divergingx_hcl}} for details.
195 #' @param mid Data value that should be mapped to the mid-point of the diverging color scale.
196 #' @param na.value Color to be used for missing data points.
197 #' @param guide Type of legend. Use \code{"coloursteps"} for color bar with discrete steps.
198 #' @param n_interp Number of discrete colors that should be used to interpolate the binned color scale.
199 #' For diverging scales, it is important to use an odd number to capture the color at the midpoint.
200 #' @param ... common binned scale parameters: `name`, `breaks`, `labels`, and `limits`. See
201 #' \code{\link[ggplot2]{binned_scale}} for more details.
202 #' @examples
203 #' library("ggplot2")
204 #'
205 #' # volcano plot (difference from mean height)
206 #' nx = 87
207 #' ny = 61
208 #' df <- data.frame(diff = c(volcano) - mean(volcano), x = rep(1:nx, ny), y = rep(1:ny, each = nx))
209 #' ggplot(df, aes(x, y, fill=diff)) +
210 #' geom_raster() + scale_fill_binned_divergingx(palette = "Fall", rev = TRUE) +
211 #' coord_fixed(expand = FALSE)
212 #'
213 #'
214 #' # adapted from stackoverflow: https://stackoverflow.com/a/20127706/4975218
215 #'
216 #' # generate dataset and base plot
217 #' set.seed(100)
218 #' df <- data.frame(country = LETTERS, V = runif(26, -40, 40))
219 #' df$country = factor(LETTERS, LETTERS[order(df$V)]) # reorder factors
220 #' gg <- ggplot(df, aes(x = country, y = V, fill = V)) +
221 #' geom_bar(stat = "identity") +
222 #' labs(y = "Under/over valuation in %", x = "Country") +
223 #' coord_flip() + theme_minimal()
224 #'
225 #' # plot with diverging scale "Geyser"
226 #' gg + scale_fill_binned_divergingx(palette = "Geyser", n.breaks = 6)
227 #' @importFrom stats na.omit
228 #' @export
229 scale_colour_binned_divergingx <- function(palette = "Geyser", c1 = NULL, c2 = NULL, c3 = NULL,
230 l1 = NULL, l2 = NULL, l3 = NULL, h1 = NULL, h2 = NULL,
231 h3 = NULL, p1 = NULL, p2 = NULL, p3 = NULL, p4 = NULL,
232 cmax1 = NULL, cmax2 = NULL, alpha = 1, rev = FALSE, mid = 0, na.value = "grey50",
233 guide = "coloursteps", n_interp = 11, aesthetics = "colour", ...)
234 {
235 # arguments we want to hand off to function divergingx_hcl only if explicitly provided
236 hcl_args <- c("c1", "c2", "c3", "l1", "l2", "l3", "h1", "h2", "h3", "p1", "p2",
237 "p3", "p4", "cmax1", "cmax2")
238
239 # match hcl_args to args provided
240 args <- as.list(match.call())
241 args[[1]] <- NULL # remove the function call
242 args <- args[na.omit(match(hcl_args, names(args)))] # remove other args
243
244 # set the remaining arguments and call qualitative_hcl
245 args <- c(args, list(palette = palette, n = n_interp, alpha = alpha, rev = rev))
246 colors <- do.call(divergingx_hcl, args, envir = parent.frame())
247
248 # diverging scale
249 rescaler = mid_rescaler(mid)
250 ggplot2::binned_scale(
251 aesthetics, "binned_divergingx",
252 scales::gradient_n_pal(colors, values = NULL),
253 na.value = na.value, guide = guide,
254 rescaler = rescaler, ...
255 )
256 }
257
258 #' @rdname scale_colour_binned_divergingx
259 #' @export
260 scale_color_binned_divergingx <- scale_colour_binned_divergingx
261
262 #' @rdname scale_colour_binned_divergingx
263 #' @export
264 scale_fill_binned_divergingx <- function(..., aesthetics = "fill")
265 {
266 args <- as.list(match.call())
267 args[[1]] <- NULL # remove the function call
268 if (is.null(args[["aesthetics"]])) args$aesthetics <- "fill"
269 do.call(scale_colour_binned_divergingx, args, envir = parent.frame())
270 }