59
59
# ' variables for which margins are to be created.
60
60
# ' @param facets `r lifecycle::badge("deprecated")` Please use `rows`
61
61
# ' and `cols` instead.
62
+ # ' @param axes Determines which axes will be drawn. When `"margins"`
63
+ # ' (default), axes will be drawn at the exterior margins. `"all_x"` and
64
+ # ' `"all_y"` will draw the respective axes at the interior panels too, whereas
65
+ # ' `"all"` will draw all axes at all panels.
66
+ # ' @param axis.labels Determines whether to draw labels for interior axes when
67
+ # ' the `axes` argument is not `"margins"`. When `"all"` (default), all
68
+ # ' interior axes get labels. When `"margins"`, only the exterior axes get
69
+ # ' labels and the interior axes get none. When `"all_x"` or `"all_y"`, only
70
+ # ' draws the labels at the interior axes in the x- or y-direction
71
+ # ' respectively.
62
72
# ' @export
63
73
# ' @examples
64
74
# ' p <- ggplot(mpg, aes(displ, cty)) + geom_point()
79
89
# ' facet_grid(cols = vars(cyl)) +
80
90
# ' geom_point(data = df, colour = "red", size = 2)
81
91
# '
92
+ # ' # When scales are constant, duplicated axes can be shown with
93
+ # ' # or without labels
94
+ # ' ggplot(mpg, aes(cty, hwy)) +
95
+ # ' geom_point() +
96
+ # ' facet_grid(year ~ drv, axes = "all", axis.labels = "all_x")
97
+ # '
82
98
# ' # Free scales -------------------------------------------------------
83
99
# ' # You can also choose whether the scales should be constant
84
100
# ' # across all panels (the default), or whether they should be allowed
@@ -112,6 +128,7 @@ facet_grid <- function(rows = NULL, cols = NULL, scales = "fixed",
112
128
space = " fixed" , shrink = TRUE ,
113
129
labeller = " label_value" , as.table = TRUE ,
114
130
switch = NULL , drop = TRUE , margins = FALSE ,
131
+ axes = " margins" , axis.labels = " all" ,
115
132
facets = deprecated()) {
116
133
# `facets` is deprecated and renamed to `rows`
117
134
if (lifecycle :: is_present(facets )) {
@@ -137,6 +154,20 @@ facet_grid <- function(rows = NULL, cols = NULL, scales = "fixed",
137
154
y = any(space %in% c(" free_y" , " free" ))
138
155
)
139
156
157
+ draw_axes <- arg_match0(axes , c(" margins" , " all_x" , " all_y" , " all" ))
158
+ draw_axes <- list (
159
+ x = any(draw_axes %in% c(" all_x" , " all" )),
160
+ y = any(draw_axes %in% c(" all_y" , " all" ))
161
+ )
162
+
163
+ # Omitting labels is special-cased internally, so even when no internal axes
164
+ # are to be drawn, register as labelled.
165
+ axis_labels <- arg_match0(axis.labels , c(" margins" , " all_x" , " all_y" , " all" ))
166
+ axis_labels <- list (
167
+ x = ! draw_axes $ x || any(axis_labels %in% c(" all_x" , " all" )),
168
+ y = ! draw_axes $ y || any(axis_labels %in% c(" all_y" , " all" ))
169
+ )
170
+
140
171
if (! is.null(switch )) {
141
172
arg_match0(switch , c(" both" , " x" , " y" ))
142
173
}
@@ -150,7 +181,8 @@ facet_grid <- function(rows = NULL, cols = NULL, scales = "fixed",
150
181
shrink = shrink ,
151
182
params = list (rows = facets_list $ rows , cols = facets_list $ cols , margins = margins ,
152
183
free = free , space_free = space_free , labeller = labeller ,
153
- as.table = as.table , switch = switch , drop = drop )
184
+ as.table = as.table , switch = switch , drop = drop ,
185
+ draw_axes = draw_axes , axis_labels = axis_labels )
154
186
)
155
187
}
156
188
@@ -306,8 +338,22 @@ FacetGrid <- ggproto("FacetGrid", Facet,
306
338
cli :: cli_abort(" {.fn {snake_class(coord)}} doesn't support free scales." )
307
339
}
308
340
309
- cols <- which(layout $ ROW == 1 )
310
- rows <- which(layout $ COL == 1 )
341
+ if (! params $ axis_labels $ x ) {
342
+ cols <- seq_len(nrow(layout ))
343
+ x_axis_order <- as.integer(layout $ PANEL [order(layout $ ROW , layout $ COL )])
344
+ } else {
345
+ cols <- which(layout $ ROW == 1 )
346
+ x_axis_order <- layout $ COL
347
+ }
348
+ if (! params $ axis_labels $ y ) {
349
+ rows <- seq_len(nrow(layout ))
350
+ y_axis_order <- as.integer(layout $ PANEL [order(layout $ ROW , layout $ COL )])
351
+ } else {
352
+ rows <- which(layout $ COL == 1 )
353
+ y_axis_order <- layout $ ROW
354
+ }
355
+
356
+ ranges <- censor_labels(ranges , layout , params $ axis_labels )
311
357
axes <- render_axes(ranges [cols ], ranges [rows ], coord , theme , transpose = TRUE )
312
358
313
359
col_vars <- unique0(layout [names(params $ cols )])
@@ -334,7 +380,8 @@ FacetGrid <- ggproto("FacetGrid", Facet,
334
380
}
335
381
ncol <- max(layout $ COL )
336
382
nrow <- max(layout $ ROW )
337
- panel_table <- matrix (panels , nrow = nrow , ncol = ncol , byrow = TRUE )
383
+ mtx <- function (x ) matrix (x , nrow = nrow , ncol = ncol , byrow = TRUE )
384
+ panel_table <- mtx(panels )
338
385
339
386
# @kohske
340
387
# Now size of each panel is calculated using PANEL$ranges, which is given by
@@ -358,7 +405,7 @@ FacetGrid <- ggproto("FacetGrid", Facet,
358
405
}
359
406
360
407
panel_table <- gtable_matrix(" layout" , panel_table ,
361
- panel_widths , panel_heights , respect = respect , clip = coord $ clip , z = matrix ( 1 , ncol = ncol , nrow = nrow ))
408
+ panel_widths , panel_heights , respect = respect , clip = coord $ clip , z = mtx( 1 ))
362
409
panel_table $ layout $ name <- paste0(' panel-' , rep(seq_len(nrow ), ncol ), ' -' , rep(seq_len(ncol ), each = nrow ))
363
410
364
411
panel_table <- gtable_add_col_space(panel_table ,
@@ -367,17 +414,27 @@ FacetGrid <- ggproto("FacetGrid", Facet,
367
414
theme $ panel.spacing.y %|| % theme $ panel.spacing )
368
415
369
416
# Add axes
370
- panel_table <- gtable_add_rows(panel_table , max_height(axes $ x $ top ), 0 )
371
- panel_table <- gtable_add_rows(panel_table , max_height(axes $ x $ bottom ), - 1 )
372
- panel_table <- gtable_add_cols(panel_table , max_width(axes $ y $ left ), 0 )
373
- panel_table <- gtable_add_cols(panel_table , max_width(axes $ y $ right ), - 1 )
374
- panel_pos_col <- panel_cols(panel_table )
375
- panel_pos_rows <- panel_rows(panel_table )
417
+ if (params $ draw_axes $ x ) {
418
+ axes $ x <- lapply(axes $ x , function (x ) mtx(x [x_axis_order ]))
419
+ panel_table <- weave_axes(panel_table , axes $ x )$ panels
420
+ } else {
421
+ panel_table <- gtable_add_rows(panel_table , max_height(axes $ x $ top ), 0 )
422
+ panel_table <- gtable_add_rows(panel_table , max_height(axes $ x $ bottom ), - 1 )
423
+ panel_pos_col <- panel_cols(panel_table )
424
+ panel_table <- gtable_add_grob(panel_table , axes $ x $ top , 1 , panel_pos_col $ l , clip = " off" , name = paste0(" axis-t-" , seq_along(axes $ x $ top )), z = 3 )
425
+ panel_table <- gtable_add_grob(panel_table , axes $ x $ bottom , - 1 , panel_pos_col $ l , clip = " off" , name = paste0(" axis-b-" , seq_along(axes $ x $ bottom )), z = 3 )
426
+ }
376
427
377
- panel_table <- gtable_add_grob(panel_table , axes $ x $ top , 1 , panel_pos_col $ l , clip = " off" , name = paste0(" axis-t-" , seq_along(axes $ x $ top )), z = 3 )
378
- panel_table <- gtable_add_grob(panel_table , axes $ x $ bottom , - 1 , panel_pos_col $ l , clip = " off" , name = paste0(" axis-b-" , seq_along(axes $ x $ bottom )), z = 3 )
379
- panel_table <- gtable_add_grob(panel_table , axes $ y $ left , panel_pos_rows $ t , 1 , clip = " off" , name = paste0(" axis-l-" , seq_along(axes $ y $ left )), z = 3 )
380
- panel_table <- gtable_add_grob(panel_table , axes $ y $ right , panel_pos_rows $ t , - 1 , clip = " off" , name = paste0(" axis-r-" , seq_along(axes $ y $ right )), z = 3 )
428
+ if (params $ draw_axes $ y ) {
429
+ axes $ y <- lapply(axes $ y , function (y ) mtx(y [y_axis_order ]))
430
+ panel_table <- weave_axes(panel_table , axes $ y )$ panels
431
+ } else {
432
+ panel_table <- gtable_add_cols(panel_table , max_width(axes $ y $ left ), 0 )
433
+ panel_table <- gtable_add_cols(panel_table , max_width(axes $ y $ right ), - 1 )
434
+ panel_pos_rows <- panel_rows(panel_table )
435
+ panel_table <- gtable_add_grob(panel_table , axes $ y $ left , panel_pos_rows $ t , 1 , clip = " off" , name = paste0(" axis-l-" , seq_along(axes $ y $ left )), z = 3 )
436
+ panel_table <- gtable_add_grob(panel_table , axes $ y $ right , panel_pos_rows $ t , - 1 , clip = " off" , name = paste0(" axis-r-" , seq_along(axes $ y $ right )), z = 3 )
437
+ }
381
438
382
439
# Add strips
383
440
switch_x <- ! is.null(params $ switch ) && params $ switch %in% c(" both" , " x" )
0 commit comments