Debian Codegraph @

R/scale_diverging.R

raw
1 #' HCL-Based Discrete Diverging Color Scales for ggplot2
2 #'
3 #' Discrete ggplot2 color scales using the color palettes generated by \code{\link{diverging_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 at the scale endpoints.
10 #' @param cmax Maximum chroma value.
11 #' @param l1 Luminance value at the scale endpoints.
12 #' @param l2 Luminance value at the scale midpoint.
13 #' @param h1 Hue value at the first endpoint.
14 #' @param h2 Hue value at the second endpoint.
15 #' @param p1 Control parameter determining how chroma should vary (1 = linear, 2 = quadratic, etc.).
16 #' @param p2 Control parameter determining how luminance should vary (1 = linear, 2 = quadratic, etc.).
17 #' @param alpha Numeric vector of values in the range \code{[0, 1]} for alpha transparency channel (0 means transparent and 1 means opaque).
18 #' @param rev If \code{TRUE}, reverses the order of the colors in the color scale.
19 #' @param palette The name of the palette to be used. Run \code{hcl_palettes(type = "diverging")} for available options.
20 #' @param nmax Maximum number of different colors the palette should contain. If not provided, is calculated automatically
21 #' from the data.
22 #' @param order Numeric vector listing the order in which the colors should be used. Default is \code{1:nmax}.
23 #' @param aesthetics The ggplot2 aesthetics to which this scale should be applied.
24 #' @param ... common discrete scale parameters: \code{name}, \code{breaks}, \code{labels}, \code{na.value}, \code{limits} and \code{guide}. See
25 #' \code{\link[ggplot2]{discrete_scale}} for more details.
26 #' @examples
27 #' library("ggplot2")
28 #'
29 #' # default colors with slightly darkened midpoint
30 #' ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
31 #' geom_point() + theme_minimal() +
32 #' scale_color_discrete_diverging(l2=75)
33 #'
34 #' # color scale "Green-Orange"
35 #' ggplot(iris, aes(Sepal.Length, fill = Species)) +
36 #' geom_density(alpha = 0.7) + theme_classic() +
37 #' scale_fill_discrete_diverging(palette = "Green-Orange", rev = TRUE)
38 #'
39 #' # use `nmax` and `order` to skip some colors
40 #' ggplot(iris, aes(Sepal.Length, fill = Species)) +
41 #' geom_density(alpha = 0.7) + theme_classic() +
42 #' scale_fill_discrete_diverging(palette = "Green-Orange", nmax = 5, order = c(1, 4, 5))
43 #' @importFrom stats na.omit
44 #' @export
45 scale_colour_discrete_diverging <- function(palette = NULL, c1 = NULL, cmax = NULL, l1 = NULL, l2 = NULL,
46 h1 = NULL, h2 = NULL, p1 = NULL, p2 = NULL, alpha = 1, rev = FALSE,
47 nmax = NULL, order = NULL, aesthetics = "colour", ...)
48 {
49 # arguments we want to hand off to function diverging_hcl only if explicitly provided
50 hcl_args <- c("palette", "c1", "cmax", "l1", "l2", "h1", "h2", "p1", "p2")
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(n = nmax, alpha = alpha, rev = rev))
68 do.call(diverging_hcl, args, envir = parent.frame())[order]
69 }
70 ggplot2::discrete_scale(aesthetics, "manual", pal, ...)
71 }
72
73 #' @rdname scale_colour_discrete_diverging
74 #' @export
75 scale_color_discrete_diverging <- scale_colour_discrete_diverging
76
77 #' @rdname scale_colour_discrete_diverging
78 #' @export
79 scale_fill_discrete_diverging <- 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_diverging, args, envir = parent.frame())
85 }
86
87 #' HCL-Based Continuous Diverging Color Scales for ggplot2
88 #'
89 #' Continuous ggplot2 color scales using the color palettes generated by \code{\link{diverging_hcl}}.
90 #'
91 #'
92 #' If both a valid palette name and palette parameters are provided then the provided palette parameters overwrite the parameters in the
93 #' named palette. This enables easy customization of named palettes.
94 #'
95 #' @inheritParams scale_colour_discrete_diverging
96 #' @param mid Data value that should be mapped to the mid-point of the diverging color scale.
97 #' @param na.value Color to be used for missing data points.
98 #' @param guide Type of legend. Use \code{"colourbar"} for continuous color bar.
99 #' @param n_interp Number of discrete colors that should be used to interpolate the continuous color scale.
100 #' It is important to use an odd number to capture the color at the midpoint.
101 #' @param ... common continuous scale parameters: `name`, `breaks`, `labels`, and `limits`. See
102 #' \code{\link[ggplot2]{continuous_scale}} for more details.
103 #' @examples
104 #' # adapted from stackoverflow: https://stackoverflow.com/a/20127706/4975218
105 #'
106 #' library("ggplot2")
107 #'
108 #' # generate dataset and base plot
109 #' set.seed(100)
110 #' df <- data.frame(country = LETTERS, V = runif(26, -40, 40))
111 #' df$country = factor(LETTERS, LETTERS[order(df$V)]) # reorder factors
112 #' gg <- ggplot(df, aes(x = country, y = V, fill = V)) +
113 #' geom_bar(stat = "identity") +
114 #' labs(y = "Under/over valuation in %", x = "Country") +
115 #' coord_flip() + theme_minimal()
116 #'
117 #' # plot with default diverging scale
118 #' gg + scale_fill_continuous_diverging()
119 #'
120 #' # plot with alternative scale
121 #' gg + scale_fill_continuous_diverging(palette = "Purple-Green")
122 #'
123 #' # plot with modified alternative scale
124 #' gg + scale_fill_continuous_diverging(palette = "Blue-Red 3", l1 = 30, l2 = 100, p1 = .9, p2 = 1.2)
125 #' @importFrom stats na.omit
126 #' @export
127 scale_colour_continuous_diverging <- function(palette = NULL, c1 = NULL, cmax = NULL, l1 = NULL, l2 = NULL,
128 h1 = NULL, h2 = NULL, p1 = NULL, p2 = NULL, alpha = 1, rev = FALSE,
129 mid = 0, na.value = "grey50", guide = "colourbar",
130 n_interp = 11, aesthetics = "colour", ...)
131 {
132 # arguments we want to hand off to function diverging_hcl only if explicitly provided
133 hcl_args <- c("palette", "c1", "cmax", "l1", "l2", "h1", "h2", "p1", "p2")
134
135 # match hcl_args to args provided
136 args <- as.list(match.call())
137 args[[1]] <- NULL # remove the function call
138 args <- args[na.omit(match(hcl_args, names(args)))] # remove other args
139
140 # set the remaining arguments and call qualitative_hcl
141 args <- c(args, list(n = n_interp, alpha = alpha, rev = rev))
142 colors <- do.call(diverging_hcl, args, envir = parent.frame())
143
144 ggplot2::continuous_scale(aesthetics, "continuous_diverging",
145 scales::gradient_n_pal(colors, values = NULL),
146 na.value = na.value, guide = guide,
147 rescaler = mid_rescaler(mid), ...)
148 }
149
150 #' @rdname scale_colour_continuous_diverging
151 #' @export
152 scale_color_continuous_diverging <- scale_colour_continuous_diverging
153
154 #' @rdname scale_colour_continuous_diverging
155 #' @export
156 scale_fill_continuous_diverging <- function(..., aesthetics = "fill")
157 {
158 args <- as.list(match.call())
159 args[[1]] <- NULL # remove the function call
160 if (is.null(args[["aesthetics"]])) args$aesthetics <- "fill"
161 do.call(scale_colour_continuous_diverging, args, envir = parent.frame())
162 }
163
164 #' HCL-Based Binned Diverging Color Scales for ggplot2
165 #'
166 #' Binned ggplot2 color scales using the color palettes generated by \code{\link{diverging_hcl}}.
167 #'
168 #'
169 #' If both a valid palette name and palette parameters are provided then the provided palette parameters overwrite the parameters in the
170 #' named palette. This enables easy customization of named palettes.
171 #'
172 #' @inheritParams scale_colour_discrete_diverging
173 #' @param mid Data value that should be mapped to the mid-point of the diverging color scale.
174 #' @param na.value Color to be used for missing data points.
175 #' @param guide Type of legend. Use \code{"coloursteps"} for color bar with discrete steps.
176 #' @param n_interp Number of discrete colors that should be used to interpolate the binned color scale.
177 #' It is important to use an odd number to capture the color at the midpoint.
178 #' @param ... common continuous scale parameters: `name`, `breaks`, `labels`, and `limits`. See
179 #' \code{\link[ggplot2]{binned_scale}} for more details.
180 #' @examples
181 #' # adapted from stackoverflow: https://stackoverflow.com/a/20127706/4975218
182 #'
183 #' library("ggplot2")
184 #'
185 #' # generate dataset and base plot
186 #' set.seed(100)
187 #' df <- data.frame(country = LETTERS, V = runif(26, -40, 40))
188 #' df$country = factor(LETTERS, LETTERS[order(df$V)]) # reorder factors
189 #' gg <- ggplot(df, aes(x = country, y = V, fill = V)) +
190 #' geom_bar(stat = "identity") +
191 #' labs(y = "Under/over valuation in %", x = "Country") +
192 #' coord_flip() + theme_minimal()
193 #'
194 #' # plot with default diverging scale
195 #' gg + scale_fill_binned_diverging(n.breaks = 6)
196 #'
197 #' # plot with alternative scale
198 #' gg + scale_fill_binned_diverging(palette = "Purple-Green", n.breaks = 6)
199 #' @importFrom stats na.omit
200 #' @export
201 scale_colour_binned_diverging <- function(palette = NULL, c1 = NULL, cmax = NULL, l1 = NULL, l2 = NULL,
202 h1 = NULL, h2 = NULL, p1 = NULL, p2 = NULL, alpha = 1, rev = FALSE,
203 mid = 0, na.value = "grey50", guide = "coloursteps",
204 n_interp = 11, aesthetics = "colour", ...)
205 {
206 # arguments we want to hand off to function diverging_hcl only if explicitly provided
207 hcl_args <- c("palette", "c1", "cmax", "l1", "l2", "h1", "h2", "p1", "p2")
208
209 # match hcl_args to args provided
210 args <- as.list(match.call())
211 args[[1]] <- NULL # remove the function call
212 args <- args[na.omit(match(hcl_args, names(args)))] # remove other args
213
214 # set the remaining arguments and call qualitative_hcl
215 args <- c(args, list(n = n_interp, alpha = alpha, rev = rev))
216 colors <- do.call(diverging_hcl, args, envir = parent.frame())
217
218 ggplot2::binned_scale(
219 aesthetics,
220 "binned_diverging",
221 scales::gradient_n_pal(colors, values = NULL),
222 na.value = na.value, guide = guide,
223 rescaler = mid_rescaler(mid),
224 ...
225 )
226 }
227
228 #' @rdname scale_colour_binned_diverging
229 #' @export
230 scale_color_binned_diverging <- scale_colour_binned_diverging
231
232 #' @rdname scale_colour_binned_diverging
233 #' @export
234 scale_fill_binned_diverging <- function(..., aesthetics = "fill")
235 {
236 args <- as.list(match.call())
237 args[[1]] <- NULL # remove the function call
238 if (is.null(args[["aesthetics"]])) args$aesthetics <- "fill"
239 do.call(scale_colour_binned_diverging, args, envir = parent.frame())
240 }