-
Notifications
You must be signed in to change notification settings - Fork 18
/
new-scale.R
292 lines (236 loc) · 8.92 KB
/
new-scale.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#' Adds a new scale to a plot
#'
#' Creates a new scale "slot". Geoms added to a plot after this function will
#' use a new scale definition.
#'
#' @param new_aes A string with the name of the aesthetic for which a new scale
#' will be created.
#'
#' @details
#' `new_scale_color()`, `new_scale_colour()` and `new_scale_fill()` are just
#' aliases to `new_scale("color")`, etc...
#'
#' @examples
#' library(ggplot2)
#'
#' # Equivalent to melt(volcano), but we don't want to depend on reshape2
#' topography <- expand.grid(x = 1:nrow(volcano),
#' y = 1:ncol(volcano))
#' topography$z <- c(volcano)
#'
#' # point measurements of something at a few locations
#' measurements <- data.frame(x = runif(30, 1, 80),
#' y = runif(30, 1, 60),
#' thing = rnorm(30))
#'
#' ggplot(mapping = aes(x, y)) +
#' geom_contour(data = topography, aes(z = z, color = stat(level))) +
#' # Color scale for topography
#' scale_color_viridis_c(option = "D") +
#' # geoms below will use another color scale
#' new_scale_color() +
#' geom_point(data = measurements, size = 3, aes(color = thing)) +
#' # Color scale applied to geoms added after new_scale_color()
#' scale_color_viridis_c(option = "A")
#'
#' @export
new_scale <- function(new_aes) {
structure(ggplot2::standardise_aes_names(new_aes), class = "new_aes")
}
#' @export
#' @rdname new_scale
new_scale_fill <- function() {
new_scale("fill")
}
#' @export
#' @rdname new_scale
new_scale_color <- function() {
new_scale("colour")
}
#' @export
#' @rdname new_scale
new_scale_colour <- function() {
new_scale("colour")
}
#' @export
#' @importFrom ggplot2 ggplot_add
ggplot_add.new_aes <- function(object, plot, object_name) {
# To add default scales (I need to build the whole plot because they might be computed aesthetics)
if (is.null(plot$scales$get_scales(object))) {
plot$scales <- ggplot2::ggplot_build(plot)$plot$scales
}
# Global aes
old_aes <- names(plot$mapping)[remove_new(names(plot$mapping)) %in% object]
new_aes <- paste0(old_aes, "_new")
names(plot$mapping)[names(plot$mapping) == old_aes] <- new_aes
plot$layers <- bump_aes_layers(plot$layers, new_aes = object)
plot$scales$scales <- bump_aes_scales(plot$scales$scales, new_aes = object)
plot$labels <- bump_aes_labels(plot$labels, new_aes = object)
plot$guides <- bump_aes_guides(plot$guides, new_aes = object)
plot
}
bump_aes_guides <- function(guides, new_aes) {
original_aes <- new_aes
if (inherits(guides, "Guides")) {
to_change <- remove_new(names(guides$guides)) == original_aes
if (any(to_change)) {
names(guides$guides)[to_change] <- paste0(names(guides$guides), "_new")
}
} else {
to_change <- remove_new(names(guides)) == original_aes
if (any(to_change)) {
names(guides)[to_change] <- paste0(names(guides), "_new")
}
}
return(guides)
}
bump_aes_layers <- function(layers, new_aes) {
lapply(layers, bump_aes_layer, new_aes = new_aes)
}
bump_aes_layer <- function(layer, new_aes) {
original_aes <- new_aes
new_layer <- ggplot2::ggproto(NULL, layer)
# Get explicit mapping
old_aes <- names(new_layer$mapping)[remove_new(names(new_layer$mapping)) %in% new_aes]
# If not explicit, get the default
if (length(old_aes) == 0) {
old_aes <- names(new_layer$stat$default_aes)[remove_new(names(new_layer$stat$default_aes)) %in% new_aes]
if (length(old_aes) == 0) {
old_aes <- names(new_layer$geom$default_aes)[remove_new(names(new_layer$geom$default_aes)) %in% new_aes]
}
}
# Return unchanged layer if it doens't use this aes
if (length(old_aes) == 0) {
return(new_layer)
}
new_aes <- paste0(old_aes, "_new")
old_geom <- new_layer$geom
old_handle_na <- old_geom$handle_na
new_handle_na <- function(self, data, params) {
colnames(data)[colnames(data) %in% new_aes] <- original_aes
old_handle_na(data, params)
}
new_geom <- ggplot2::ggproto(paste0("New", class(old_geom)[1]), old_geom,
handle_na = new_handle_na)
new_geom$default_aes <- change_name(new_geom$default_aes, old_aes, new_aes)
new_geom$non_missing_aes <- change_name(new_geom$non_missing_aes, old_aes, new_aes)
new_geom$required_aes <- change_name(new_geom$required_aes, old_aes, new_aes)
new_geom$optional_aes <- change_name(new_geom$optional_aes, old_aes, new_aes)
draw_key <- new_geom$draw_key
new_draw_key <- function(data, params, size) {
colnames(data)[colnames(data) == new_aes] <- original_aes
draw_key(data, params, size)
}
new_geom$draw_key <- new_draw_key
new_layer$geom <- new_geom
old_stat <- new_layer$stat
new_handle_na <- function(self, data, params) {
colnames(data)[colnames(data) %in% new_aes] <- original_aes
ggplot2::ggproto_parent(self$super(), self)$handle_na(data, params)
}
new_setup_data <- function(self, data, scales, ...) {
# After setup data, I need to go back to the new aes names, otherwise
# scales are not applied.
colnames(data)[colnames(data) %in% new_aes] <- original_aes
data <- ggplot2::ggproto_parent(self$super(), self)$setup_data(data, scales, ...)
colnames(data)[colnames(data) %in% original_aes] <- new_aes
data
}
if (!is.null(old_stat$is_new)) {
parent <- old_stat$super()
} else {
parent <- ggplot2::ggproto(NULL, old_stat)
}
new_stat <- ggplot2::ggproto(paste0("New", class(old_stat)[1]), parent,
setup_data = new_setup_data,
handle_na = new_handle_na,
is_new = TRUE)
new_stat$default_aes <- change_name(new_stat$default_aes, old_aes, new_aes)
new_stat$non_missing_aes <- change_name(new_stat$non_missing_aes, old_aes, new_aes)
new_stat$required_aes <- change_name(new_stat$required_aes, old_aes, new_aes)
new_stat$optional_aes <- change_name(new_stat$optional_aes, old_aes, new_aes)
new_layer$stat <- new_stat
# Make implicit mapping explicit.
# This fixes https://github.com/eliocamp/ggnewscale/issues/45 but it feels
# wrong. I don't understand why implicit mapping breaks when adding more than
# one extra scale.
if (is.null(new_layer$mapping[[old_aes]])) {
new_layer$mapping[[old_aes]] <- new_stat$default_aes[[new_aes]]
}
new_layer$mapping <- change_name(new_layer$mapping, old_aes, new_aes)
new_layer$aes_params <- change_name(new_layer$aes_params, old_aes, new_aes)
# Restore custom attributes
attributes_old <- attributes(layer)
attributes_new <- attributes(new_layer)
attributes_replace <- attributes_old[setdiff(names(attributes_old), names(attributes_new))]
attributes(new_layer)[names(attributes_replace)] <- attributes_replace
new_layer
}
bump_aes_scales <- function(scales, new_aes) {
lapply(scales, bump_aes_scale, new_aes = new_aes)
}
#' @importFrom ggplot2 guide_colourbar guide_colorbar guide_legend
bump_aes_scale <- function(scale, new_aes) {
old_aes <- scale$aesthetics[remove_new(scale$aesthetics) %in% new_aes]
if (length(old_aes) != 0) {
new_aes <- paste0(old_aes, "_new")
scale$aesthetics[scale$aesthetics %in% old_aes] <- new_aes
if (is.character(scale$guide)) {
no_guide <- isTRUE(scale$guide == "none")
} else {
no_guide <- isFALSE(scale$guide) ||
isTRUE(inherits(scale$guide, c("guide_none", "GuideNone")))
}
if (!no_guide) {
if (is.character(scale$guide)) {
scale$guide <- get(paste0("guide_", scale$guide), mode = "function")()
}
if (inherits(scale$guide, "Guide")) {
# Make clone of guie
old <- scale$guide
new <- ggplot2::ggproto(NULL, old)
# Change available aesthetics
new$available_aes <- change_name(new$available_aes, old_aes, new_aes)
new$available_aes[new$available_aes %in% old_aes] <- new_aes
# Update aesthetic override
if (!is.null(new$params$override.aes)) {
new$params$override.aes <- change_name(new$params$override.aes, old_aes, new_aes)
}
# Re-assign updated guide
scale$guide <- new
} else {
scale$guide$available_aes[scale$guide$available_aes %in% old_aes] <- new_aes
if (!is.null(scale$guide$override.aes)) {
names(scale$guide$override.aes)[names(scale$guide$override.aes) == old_aes] <- new_aes
}
}
}
}
scale
}
bump_aes_labels <- function(labels, new_aes) {
old_aes <- names(labels)[remove_new(names(labels)) %in% new_aes]
new_aes <- paste0(old_aes, "_new")
names(labels)[names(labels) %in% old_aes] <- new_aes
labels
}
change_name <- function(list, old, new) {
UseMethod("change_name")
}
change_name.character <- function(list, old, new) {
list[list %in% old] <- new
list
}
change_name.default <- function(list, old, new) {
nam <- names(list)
nam[nam %in% old] <- new
names(list) <- nam
list
}
change_name.NULL <- function(list, old, new) {
NULL
}
remove_new <- function(aes) {
gsub("(_new)*", "", aes, fixed = FALSE)
# stringi::stri_replace_all(aes, "", regex = "(_new)*")
}