|
23 | 23 | #' either of the four sides by setting \code{strip.position = c("top",
|
24 | 24 | #' "bottom", "left", "right")}
|
25 | 25 | #' @param dir Direction: either `"h"` for horizontal, the default, or `"v"`,
|
26 |
| -#' for vertical. |
| 26 | +#' for vertical. When `"h"` or `"v"` will be combined with `as.table` to |
| 27 | +#' set final layout. Alternatively, a combination of `"t"` (top) or |
| 28 | +#' `"b"` (bottom) with `"l"` (left) or `"r"` (right) to set a layout directly. |
| 29 | +#' These two letters give the starting position and the first letter gives |
| 30 | +#' the growing direction. For example `"rt"` will place the first panel in |
| 31 | +#' the top-right and starts filling in panels right-to-left. |
27 | 32 | #' @param axes Determines which axes will be drawn in case of fixed scales.
|
28 | 33 | #' When `"margins"` (default), axes will be drawn at the exterior margins.
|
29 | 34 | #' `"all_x"` and `"all_y"` will draw the respective axes at the interior
|
@@ -95,13 +100,29 @@ NULL
|
95 | 100 | #' facet_wrap(vars(variable), scales = "free_y", nrow = 2, strip.position = "top") +
|
96 | 101 | #' theme(strip.background = element_blank(), strip.placement = "outside")
|
97 | 102 | #' }
|
| 103 | +#' |
| 104 | +#' # The two letters determine the starting position, so 'tr' starts |
| 105 | +#' # in the top-right. |
| 106 | +#' # The first letter determines direction, so 'tr' fills top-to-bottom. |
| 107 | +#' # `dir = "tr"` is equivalent to `dir = "v", as.table = FALSE` |
| 108 | +#' ggplot(mpg, aes(displ, hwy)) + |
| 109 | +#' geom_point() + |
| 110 | +#' facet_wrap(vars(class), dir = "tr") |
98 | 111 | facet_wrap <- function(facets, nrow = NULL, ncol = NULL, scales = "fixed",
|
99 | 112 | shrink = TRUE, labeller = "label_value", as.table = TRUE,
|
100 | 113 | switch = deprecated(), drop = TRUE, dir = "h",
|
101 | 114 | strip.position = 'top', axes = "margins",
|
102 | 115 | axis.labels = "all") {
|
103 | 116 | scales <- arg_match0(scales %||% "fixed", c("fixed", "free_x", "free_y", "free"))
|
104 |
| - dir <- arg_match0(dir, c("h", "v")) |
| 117 | + dir <- arg_match0(dir, c("h", "v", "lt", "tl", "lb", "bl", "rt", "tr", "rb", "br")) |
| 118 | + if (nchar(dir) == 1) { |
| 119 | + dir <- base::switch( |
| 120 | + dir, |
| 121 | + h = if (as.table) "lt" else "lb", |
| 122 | + v = if (as.table) "tl" else "tr" |
| 123 | + ) |
| 124 | + } |
| 125 | + |
105 | 126 | free <- list(
|
106 | 127 | x = any(scales %in% c("free_x", "free")),
|
107 | 128 | y = any(scales %in% c("free_y", "free"))
|
@@ -149,7 +170,6 @@ facet_wrap <- function(facets, nrow = NULL, ncol = NULL, scales = "fixed",
|
149 | 170 | params = list(
|
150 | 171 | facets = facets,
|
151 | 172 | free = free,
|
152 |
| - as.table = as.table, |
153 | 173 | strip.position = strip.position,
|
154 | 174 | drop = drop,
|
155 | 175 | ncol = ncol,
|
@@ -189,21 +209,7 @@ FacetWrap <- ggproto("FacetWrap", Facet,
|
189 | 209 | n <- attr(id, "n")
|
190 | 210 |
|
191 | 211 | dims <- wrap_dims(n, params$nrow, params$ncol)
|
192 |
| - layout <- data_frame0( |
193 |
| - PANEL = factor(id, levels = seq_len(n)), |
194 |
| - ROW = if (params$as.table) { |
195 |
| - as.integer((id - 1L) %/% dims[2] + 1L) |
196 |
| - } else { |
197 |
| - as.integer(dims[1] - (id - 1L) %/% dims[2]) |
198 |
| - }, |
199 |
| - COL = as.integer((id - 1L) %% dims[2] + 1L), |
200 |
| - .size = length(id) |
201 |
| - ) |
202 |
| - |
203 |
| - # For vertical direction, flip row and col |
204 |
| - if (identical(params$dir, "v")) { |
205 |
| - layout[c("ROW", "COL")] <- layout[c("COL", "ROW")] |
206 |
| - } |
| 212 | + layout <- wrap_layout(id, dims, params$dir) |
207 | 213 |
|
208 | 214 | panels <- vec_cbind(layout, base)
|
209 | 215 | panels <- panels[order(panels$PANEL), , drop = FALSE]
|
@@ -576,3 +582,31 @@ measure_axes <- function(empty_idx, axis, margin = 1L, shift = 0) {
|
576 | 582 | cm[set_zero] <- 0
|
577 | 583 | unit(apply(cm, margin, max), "cm")
|
578 | 584 | }
|
| 585 | + |
| 586 | +wrap_layout <- function(id, dims, dir) { |
| 587 | + as.table <- TRUE |
| 588 | + n <- attr(id, "n") |
| 589 | + |
| 590 | + ROW <- switch( |
| 591 | + dir, |
| 592 | + lt = , rt = (id - 1L) %/% dims[2] + 1L, |
| 593 | + tl = , tr = (id - 1L) %% dims[1] + 1L, |
| 594 | + lb = , rb = dims[1] - (id - 1L) %/% dims[2], |
| 595 | + bl = , br = dims[1] - (id - 1L) %% dims[1] |
| 596 | + ) |
| 597 | + |
| 598 | + COL <- switch( |
| 599 | + dir, |
| 600 | + lt = , lb = (id - 1L) %% dims[2] + 1L, |
| 601 | + tl = , bl = (id - 1L) %/% dims[1] + 1L, |
| 602 | + rt = , rb = dims[2] - (id - 1L) %% dims[2], |
| 603 | + tr = , br = dims[2] - (id - 1L) %/% dims[1] |
| 604 | + ) |
| 605 | + |
| 606 | + data_frame0( |
| 607 | + PANEL = factor(id, levels = seq_len(n)), |
| 608 | + ROW = as.integer(ROW), |
| 609 | + COL = as.integer(COL), |
| 610 | + .size = length(id) |
| 611 | + ) |
| 612 | +} |
0 commit comments