1818# ' @param scales Should scales be fixed (`"fixed"`, the default),
1919# ' free (`"free"`), or free in one dimension (`"free_x"`,
2020# ' `"free_y"`)?
21+ # ' @param space If `"fixed"` (default), all panels have the same size and
22+ # ' the number of rows and columns in the layout can be arbitrary. If
23+ # ' `"free_x"`, panels have widths proportional to the length of the x-scale,
24+ # ' but the layout is constrained to one row. If `"free_y"`, panels have
25+ # ' heights proportional to the length of the y-scale, but the layout is
26+ # ' constrained to one column.
2127# ' @param strip.position By default, the labels are displayed on the top of
2228# ' the plot. Using `strip.position` it is possible to place the labels on
2329# ' either of the four sides by setting \code{strip.position = c("top",
109115# ' geom_point() +
110116# ' facet_wrap(vars(class), dir = "tr")
111117facet_wrap <- function (facets , nrow = NULL , ncol = NULL , scales = " fixed" ,
112- shrink = TRUE , labeller = " label_value" , as.table = TRUE ,
113- switch = deprecated(), drop = TRUE , dir = " h " ,
114- strip.position = ' top' , axes = " margins" ,
118+ space = " fixed " , shrink = TRUE , labeller = " label_value" ,
119+ as.table = TRUE , switch = deprecated(), drop = TRUE ,
120+ dir = " h " , strip.position = ' top' , axes = " margins" ,
115121 axis.labels = " all" ) {
116122 scales <- arg_match0(scales %|| % " fixed" , c(" fixed" , " free_x" , " free_y" , " free" ))
117123 dir <- arg_match0(dir , c(" h" , " v" , " lt" , " tl" , " lb" , " bl" , " rt" , " tr" , " rb" , " br" ))
@@ -128,6 +134,30 @@ facet_wrap <- function(facets, nrow = NULL, ncol = NULL, scales = "fixed",
128134 y = any(scales %in% c(" free_y" , " free" ))
129135 )
130136
137+ # We cannot have free space in both directions
138+ space <- arg_match0(space , c(" free_x" , " free_y" , " fixed" ))
139+ space_free <- list (x = space == " free_x" , y = space == " free_y" )
140+ if (space_free $ x ) {
141+ if ((nrow %|| % 1 ) != 1 || ! is.null(ncol )) {
142+ cli :: cli_warn(
143+ " Cannot use {.code space = \" free_x\" } with custom \\
144+ {.arg nrow} or {.arg ncol}."
145+ )
146+ }
147+ ncol <- NULL
148+ nrow <- 1L
149+ }
150+ if (space_free $ y ) {
151+ if ((ncol %|| % 1 ) != 1 || ! is.null(nrow )) {
152+ cli :: cli_warn(
153+ " Cannot use {.code space= \" free_y\" } with custom \\
154+ {.arg nrow} or {.arg ncol}."
155+ )
156+ }
157+ ncol <- 1L
158+ nrow <- NULL
159+ }
160+
131161 # If scales are free, always draw the axes
132162 draw_axes <- arg_match0(axes , c(" margins" , " all_x" , " all_y" , " all" ))
133163 draw_axes <- list (
@@ -174,6 +204,7 @@ facet_wrap <- function(facets, nrow = NULL, ncol = NULL, scales = "fixed",
174204 drop = drop ,
175205 ncol = ncol ,
176206 nrow = nrow ,
207+ space_free = space_free ,
177208 labeller = labeller ,
178209 dir = dir ,
179210 draw_axes = draw_axes ,
0 commit comments