-
Notifications
You must be signed in to change notification settings - Fork 32
/
facet_nested.R
312 lines (289 loc) · 9.97 KB
/
facet_nested.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# Main function -----------------------------------------------------------
#' @title Layout panels in a grid with nested strips
#'
#' @description `facet_nested()` forms a matrix of panels defined by row
#' and column faceting variables and nests grouped facets.
#'
#' @inheritParams facet_grid2
#' @param nest_line a theme element, either `element_blank()` or inheriting
#' from [ggplot2::element_line()]. This element inherits from
#' the [`ggh4x.facet.nestline`][theme_extensions] element in the theme.
#' @param resect a `unit` vector of length 1, indicating how much the
#' nesting line should be shortened.
#' @param strip An object created by a call to a strip function, such as
#' [ggh4x::strip_nested()].
#' @param bleed `r lifecycle::badge("deprecated")` the `bleed` argument has
#' moved to the `strip_nested()` function.
#'
#' @details This function inherits the capabilities of
#' [ggh4x::facet_grid2()].
#'
#' Unlike `facet_grid()`, this function only automatically expands
#' missing variables when they have no variables in that direction, to allow
#' for unnested variables. It still requires at least one layer to have all
#' faceting variables.
#'
#' Hierarchies are inferred from the order of variables supplied to
#' `rows` or `cols`. The first variable is interpreted to be the
#' outermost variable, while the last variable is interpreted to be the
#' innermost variable. They display order is always such that the outermost
#' variable is placed the furthest away from the panels. For more information
#' about the nesting of strips, please visit the documentation of
#' [ggh4x::strip_nested()].
#'
#' @export
#'
#' @return A *FacetNested* ggproto object that can be added to a plot.
#' @family facetting functions
#' @seealso See [ggh4x::strip_nested()] for nested strips. See
#' [ggplot2::facet_grid()] for descriptions of the original
#' arguments. See [grid::unit()] for the construction of a
#' `unit` vector.
#'
#' @examples
#' # A standard plot
#' p <- ggplot(mtcars, aes(mpg, wt)) +
#' geom_point()
#'
#' # Similar to `facet_grid2(..., strip = strip_nested())`
#' p + facet_nested(~ vs + cyl)
#'
#' # The nest line inherits from the global theme
#' p + facet_nested(~ cyl + vs, nest_line = element_line(colour = "red")) +
#' theme(ggh4x.facet.nestline = element_line(linetype = 3))
facet_nested <- function(
rows = NULL,
cols = NULL,
scales = "fixed",
space = "fixed",
axes = "margins",
remove_labels = "none",
independent = "none",
shrink = TRUE,
labeller = "label_value",
as.table = TRUE,
switch = NULL,
drop = TRUE,
margins = FALSE,
nest_line = element_blank(),
resect = unit(0, "mm"),
strip = strip_nested(),
bleed = NULL
) {
strip <- assert_strip(strip)
if (!is.null(bleed)) {
lifecycle::deprecate_warn(
when = "0.2.0",
what = "facet_nested(bleed)",
details = paste0("The `bleed` argument should be set in the ",
"`strip_nested()` function instead.")
)
strip$params$bleed <- isTRUE(bleed)
}
# Convert logical to elements for backward compatibility
if (isTRUE(nest_line)) {
nest_line <- element_line()
}
if (isFALSE(nest_line)) {
nest_line <- element_blank()
}
if (!inherits(nest_line, c("element_line", "element_blank"))) {
abort(paste0("The `nest_line` argument must be an 'element_blank' or ",
"inherit from an 'element_line'."))
}
params <- list(
nest_line = nest_line,
resect = resect
)
new_grid_facets(
rows, cols,
scales, space, axes, remove_labels, independent,
shrink, labeller, as.table, switch,
drop, margins, strip,
params = params,
super = FacetNested
)
}
# ggproto -----------------------------------------------------------------
#' @usage NULL
#' @format NULL
#' @export
#' @rdname ggh4x_extensions
FacetNested <- ggproto(
"FacetNested", FacetGrid2,
map_data = function(data, layout, params) {
# Handle empty data
if (empty(data)) {
return(cbind(data, PANEL = integer(0)))
}
# Setup variables
rows <- params$rows
cols <- params$cols
vars <- c(names(rows), names(cols))
if (length(vars) == 0) {
data$PANEL <- layout$PANEL
return(data)
}
margin_vars <- list(intersect(names(rows), names(data)),
intersect(names(cols), names(data)))
# Add variables
data <- reshape_add_margins(data, margin_vars, params$margins)
facet_vals <- eval_facets(c(rows, cols), data, params$.possible_columns)
# Only set as missing if it has no variable in that direction
missing_facets <- character(0)
if (!any(names(rows) %in% names(facet_vals))){
missing_facets <- c(missing_facets,
setdiff(names(rows), names(facet_vals)))
}
if (!any(names(cols) %in% names(facet_vals))){
missing_facets <- c(missing_facets,
setdiff(names(cols), names(facet_vals)))
}
# Fill in missing values
if (length(missing_facets) > 0) {
to_add <- unique(layout[missing_facets])
data_rep <- rep.int(1:nrow(data), nrow(to_add))
facet_rep <- rep(1:nrow(to_add), each = nrow(data))
data <- data[data_rep, , drop = FALSE]
rownames(data) <- NULL
facet_vals <- cbind(facet_vals[data_rep, , drop = FALSE],
to_add[facet_rep, , drop = FALSE])
rownames(facet_vals) <- NULL
}
# Match columns to facets
if (nrow(facet_vals) == 0) {
data$PANEL <- -1
} else {
facet_vals[] <- lapply(facet_vals[], as.factor)
facet_vals[] <- lapply(facet_vals[], addNA, ifany = TRUE)
layout[] <- lapply(layout[], as.factor)
keys <- join_keys(facet_vals, layout,
by = vars[vars %in% names(facet_vals)])
data$PANEL <- layout$PANEL[match(keys$x, keys$y)]
}
data
},
vars_combine = function(
data, env = emptyenv(), vars = NULL, drop = TRUE
) {
if (length(vars) == 0) {
return(new_data_frame())
}
possible_columns <- unique0(unlist(lapply(data, names)))
values <- lapply(data, eval_facets, facets = vars,
possible_columns = possible_columns)
values <- values[lengths(values) > 0]
has_all <- unlist(lapply(values, length)) == length(vars)
if (!any(has_all)) {
missing <- lapply(values, function(x) setdiff(names(vars), names(x)))
missing_vars <- paste0(
c("Plot", paste0("Layer ", seq_len(length(data) - 1))),
" is missing {.var ", missing[seq_along(data)], "}"
)
names(missing_vars) <- rep("x", length(data))
cli::cli_abort(c(paste0(
"At least one layer must contain all faceting variables: ",
"{.var {names(vars)}}"
), missing_vars))
}
base <- unique0(vec_rbind(!!!values[has_all]))
if (!drop) {
base <- unique_combs(base)
}
for (value in values[!has_all]) {
if (empty(value))
next
old <- base[setdiff(names(base), names(value))]
new <- unique(value[intersect(names(base), names(value))])
if (drop) {
new <- unique_combs(new)
}
# This is different than vanilla ggplot2
old[setdiff(names(base), names(value))] <- rep("", nrow(old))
base <- rbind(base, df.grid(old, new))
}
if (empty(base)) {
stop("Facetting variables must have at least one value",
call. = FALSE)
}
base
},
finish_panels = function(self, panels, layout, params, theme) {
add_nest_indicator(panels, params, theme)
}
)
# Helper functions -----------------------------------------------
add_nest_indicator <- function(panels, params, theme) {
# Convert nest line to proper element
nest_line <- params$nest_line
if (is.null(nest_line) || isFALSE(nest_line) ||
inherits(nest_line, "element_blank")) {
return(panels)
}
nest_line <- inherit_element(
nest_line, calc_element("ggh4x.facet.nestline", theme)
)
# Find strips
layout <- panels$layout
layout$index <- seq_len(nrow(layout))
is_strip <- grepl("^strip-", layout$name)
layout <- layout[is_strip, ]
active <- unit(c(0, 1), "npc") + c(1, -1) * params$resect
h_strip <- layout[layout$l != layout$r,]
if (nrow(h_strip) > 0) {
index <- h_strip$index
is_secondary <- any(grepl("^strip-b", h_strip$name))
passive <- rep(as.numeric(is_secondary), 2)
# Draw the line
indicator <- element_grob(nest_line, x = active, y = passive)
# Add the line to the strip-grob
panels$grobs[index] <- lapply(
panels$grobs[index],
function(gt) {
with(gt$layout, gtable_add_grob(
gt, indicator, t = t, l = l, r = r, b = b, z = z,
name = "nester", clip = "off"
))
}
)
# These offsets ensure that the strip-grob with the nest line is on top
# of the lower-layer strip-grobs
offset <- vapply(panels$grobs[index], function(grob) {
grob$layout[["t"]][1]
}, numeric(1))
if (!is_secondary) {
nlevels <- dim(panels$grobs[[index[1]]])[1]
offset <- nlevels - offset
}
panels$layout$z[index] <- panels$layout$z[index] + offset
}
v_strip <- layout[layout$t != layout$b,]
if (nrow(v_strip) > 0) {
index <- v_strip$index
is_secondary <- any(grepl("^strip-r", v_strip$name))
passive <- rep(as.numeric(!is_secondary), 2)
# Draw the line
indicator <- element_grob(nest_line, x = passive, y = active)
# Add the line to the strip grob
panels$grobs[index] <- lapply(
panels$grobs[index],
function(gt) {
with(gt$layout, gtable_add_grob(
gt, indicator, t = t, l = l, r = r, b = b, z = z,
name = "nester", clip = "off"
))
}
)
# These offsets ensure that the strip-grob with the nest line is on top
# of the lower-layer strip-grobs
offset <- vapply(panels$grobs[index], function(grob) {
grob$layout[["l"]][1]
}, numeric(1))
if (!is_secondary) {
nlevels <- dim(panels$grobs[[index[1]]])[2]
offset <- nlevels - offset
}
panels$layout$z[index] <- panels$layout$z[index] + offset
}
return(panels)
}