Skip to content

Commit 565b501

Browse files
authored
Fix stack and clipping in facets (#1789)
1 parent 03a50ac commit 565b501

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

R/facet-grid-.r

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ FacetGrid <- ggproto("FacetGrid", Facet,
311311
}
312312

313313
panel_table <- gtable_matrix("layout", panel_table,
314-
panel_widths, panel_heights, respect = respect)
314+
panel_widths, panel_heights, respect = respect, clip = "on")
315315
panel_table$layout$name <- paste0('panel-', rep(seq_len(ncol), nrow), '-', rep(seq_len(nrow), each = ncol))
316316

317317
panel_table <- gtable_add_col_space(panel_table,
@@ -327,10 +327,10 @@ FacetGrid <- ggproto("FacetGrid", Facet,
327327
panel_pos_col <- panel_cols(panel_table)
328328
panel_pos_rows <- panel_rows(panel_table)
329329

330-
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)))
331-
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)))
332-
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)))
333-
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)))
330+
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)
331+
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)
332+
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)
333+
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)
334334

335335
# Add strips
336336
switch_x <- !is.null(params$switch) && params$switch %in% c("both", "x")
@@ -343,22 +343,22 @@ FacetGrid <- ggproto("FacetGrid", Facet,
343343
if (!is.null(strips$x$bottom)) {
344344
if (inside_x) {
345345
panel_table <- gtable_add_rows(panel_table, max_height(strips$x$bottom), -2)
346-
panel_table <- gtable_add_grob(panel_table, strips$x$bottom, -2, panel_pos_col$l, clip = "off", name = paste0("strip-b-", seq_along(strips$x$bottom)))
346+
panel_table <- gtable_add_grob(panel_table, strips$x$bottom, -2, panel_pos_col$l, clip = "on", name = paste0("strip-b-", seq_along(strips$x$bottom)), z = 2)
347347
} else {
348348
panel_table <- gtable_add_rows(panel_table, strip_padding, -1)
349349
panel_table <- gtable_add_rows(panel_table, max_height(strips$x$bottom), -1)
350-
panel_table <- gtable_add_grob(panel_table, strips$x$bottom, -1, panel_pos_col$l, clip = "off", name = paste0("strip-b-", seq_along(strips$x$bottom)))
350+
panel_table <- gtable_add_grob(panel_table, strips$x$bottom, -1, panel_pos_col$l, clip = "on", name = paste0("strip-b-", seq_along(strips$x$bottom)), z = 2)
351351
}
352352
}
353353
} else {
354354
if (!is.null(strips$x$top)) {
355355
if (inside_x) {
356356
panel_table <- gtable_add_rows(panel_table, max_height(strips$x$top), 1)
357-
panel_table <- gtable_add_grob(panel_table, strips$x$top, 2, panel_pos_col$l, clip = "off", name = paste0("strip-t-", seq_along(strips$x$top)))
357+
panel_table <- gtable_add_grob(panel_table, strips$x$top, 2, panel_pos_col$l, clip = "on", name = paste0("strip-t-", seq_along(strips$x$top)), z = 2)
358358
} else {
359359
panel_table <- gtable_add_rows(panel_table, strip_padding, 0)
360360
panel_table <- gtable_add_rows(panel_table, max_height(strips$x$top), 0)
361-
panel_table <- gtable_add_grob(panel_table, strips$x$top, 1, panel_pos_col$l, clip = "off", name = paste0("strip-t-", seq_along(strips$x$top)))
361+
panel_table <- gtable_add_grob(panel_table, strips$x$top, 1, panel_pos_col$l, clip = "on", name = paste0("strip-t-", seq_along(strips$x$top)), z = 2)
362362
}
363363
}
364364
}
@@ -367,22 +367,22 @@ FacetGrid <- ggproto("FacetGrid", Facet,
367367
if (!is.null(strips$y$left)) {
368368
if (inside_y) {
369369
panel_table <- gtable_add_cols(panel_table, max_width(strips$y$left), 1)
370-
panel_table <- gtable_add_grob(panel_table, strips$y$left, panel_pos_rows$t, 2, clip = "off", name = paste0("strip-l-", seq_along(strips$y$left)))
370+
panel_table <- gtable_add_grob(panel_table, strips$y$left, panel_pos_rows$t, 2, clip = "on", name = paste0("strip-l-", seq_along(strips$y$left)), z = 2)
371371
} else {
372372
panel_table <- gtable_add_cols(panel_table, strip_padding, 0)
373373
panel_table <- gtable_add_cols(panel_table, max_width(strips$y$left), 0)
374-
panel_table <- gtable_add_grob(panel_table, strips$y$left, panel_pos_rows$t, 1, clip = "off", name = paste0("strip-l-", seq_along(strips$y$left)))
374+
panel_table <- gtable_add_grob(panel_table, strips$y$left, panel_pos_rows$t, 1, clip = "on", name = paste0("strip-l-", seq_along(strips$y$left)), z = 2)
375375
}
376376
}
377377
} else {
378378
if (!is.null(strips$y$right)) {
379379
if (inside_y) {
380380
panel_table <- gtable_add_cols(panel_table, max_width(strips$y$right), -2)
381-
panel_table <- gtable_add_grob(panel_table, strips$y$right, panel_pos_rows$t, -2, clip = "off", name = paste0("strip-r-", seq_along(strips$y$right)))
381+
panel_table <- gtable_add_grob(panel_table, strips$y$right, panel_pos_rows$t, -2, clip = "on", name = paste0("strip-r-", seq_along(strips$y$right)), z = 2)
382382
} else {
383383
panel_table <- gtable_add_cols(panel_table, strip_padding, -1)
384384
panel_table <- gtable_add_cols(panel_table, max_width(strips$y$right), -1)
385-
panel_table <- gtable_add_grob(panel_table, strips$y$right, panel_pos_rows$t, -1, clip = "off", name = paste0("strip-r-", seq_along(strips$y$right)))
385+
panel_table <- gtable_add_grob(panel_table, strips$y$right, panel_pos_rows$t, -1, clip = "on", name = paste0("strip-r-", seq_along(strips$y$right)), z = 2)
386386
}
387387
}
388388
}

R/facet-wrap.r

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ FacetWrap <- ggproto("FacetWrap", Facet,
231231
empties <- apply(panel_table, c(1,2), function(x) is.zero(x[[1]]))
232232
panel_table <- gtable_matrix("layout", panel_table,
233233
widths = unit(rep(1, ncol), "null"),
234-
heights = unit(rep(aspect_ratio, nrow), "null"), respect = respect)
234+
heights = unit(rep(aspect_ratio, nrow), "null"), respect = respect, clip = "on")
235235
panel_table$layout$name <- paste0('panel-', rep(seq_len(ncol), nrow), '-', rep(seq_len(nrow), each = ncol))
236236

237237
panel_table <- gtable_add_col_space(panel_table,
@@ -285,10 +285,10 @@ FacetWrap <- ggproto("FacetWrap", Facet,
285285
axis_mat_y_right[col_pos] <- col_axes
286286
}
287287
}
288-
panel_table <- weave_tables_row(panel_table, axis_mat_x_top, -1, axis_height_top, "axis-t")
289-
panel_table <- weave_tables_row(panel_table, axis_mat_x_bottom, 0, axis_height_bottom, "axis-b")
290-
panel_table <- weave_tables_col(panel_table, axis_mat_y_left, -1, axis_width_left, "axis-l")
291-
panel_table <- weave_tables_col(panel_table, axis_mat_y_right, 0, axis_width_right, "axis-r")
288+
panel_table <- weave_tables_row(panel_table, axis_mat_x_top, -1, axis_height_top, "axis-t", 3)
289+
panel_table <- weave_tables_row(panel_table, axis_mat_x_bottom, 0, axis_height_bottom, "axis-b", 3)
290+
panel_table <- weave_tables_col(panel_table, axis_mat_y_left, -1, axis_width_left, "axis-l", 3)
291+
panel_table <- weave_tables_col(panel_table, axis_mat_y_right, 0, axis_width_right, "axis-r", 3)
292292

293293
strip_padding <- convertUnit(theme$strip.switch.pad.wrap, "cm")
294294
strip_name <- paste0("strip-", substr(params$strip.position, 1, 1))
@@ -304,7 +304,7 @@ FacetWrap <- ggproto("FacetWrap", Facet,
304304
strip_pad <- axis_height_bottom
305305
}
306306
strip_height <- unit(apply(strip_mat, 1, max_height), "cm")
307-
panel_table <- weave_tables_row(panel_table, strip_mat, placement, strip_height, strip_name)
307+
panel_table <- weave_tables_row(panel_table, strip_mat, placement, strip_height, strip_name, 2, "on")
308308
if (!inside) {
309309
strip_pad[unclass(strip_pad) != 0] <- strip_padding
310310
panel_table <- weave_tables_row(panel_table, row_shift = placement, row_height = strip_pad)
@@ -320,7 +320,7 @@ FacetWrap <- ggproto("FacetWrap", Facet,
320320
}
321321
strip_pad[unclass(strip_pad) != 0] <- strip_padding
322322
strip_width <- unit(apply(strip_mat, 2, max_width), "cm")
323-
panel_table <- weave_tables_col(panel_table, strip_mat, placement, strip_width, strip_name)
323+
panel_table <- weave_tables_col(panel_table, strip_mat, placement, strip_width, strip_name, 2, "on")
324324
if (!inside) {
325325
strip_pad[unclass(strip_pad) != 0] <- strip_padding
326326
panel_table <- weave_tables_col(panel_table, col_shift = placement, col_width = strip_pad)
@@ -414,26 +414,26 @@ convertInd <- function(row, col, nrow) {
414414
(col - 1) * nrow + row
415415
}
416416

417-
weave_tables_col <- function(table, table2, col_shift, col_width, name) {
417+
weave_tables_col <- function(table, table2, col_shift, col_width, name, z = 1, clip = "off") {
418418
panel_col <- panel_cols(table)$l
419419
panel_row <- panel_rows(table)$t
420420
for (i in rev(seq_along(panel_col))) {
421421
col_ind <- panel_col[i] + col_shift
422422
table <- gtable_add_cols(table, col_width[i], pos = col_ind)
423423
if (!missing(table2)) {
424-
table <- gtable_add_grob(table, table2[, i], t = panel_row, l = col_ind + 1, clip = 'off', name = paste0(name, '-', seq_along(panel_row), '-', i))
424+
table <- gtable_add_grob(table, table2[, i], t = panel_row, l = col_ind + 1, clip = clip, name = paste0(name, "-", seq_along(panel_row), "-", i), z = z)
425425
}
426426
}
427427
table
428428
}
429-
weave_tables_row <- function(table, table2, row_shift, row_height, name) {
429+
weave_tables_row <- function(table, table2, row_shift, row_height, name, z = 1, clip = "off") {
430430
panel_col <- panel_cols(table)$l
431431
panel_row <- panel_rows(table)$t
432432
for (i in rev(seq_along(panel_row))) {
433433
row_ind <- panel_row[i] + row_shift
434434
table <- gtable_add_rows(table, row_height[i], pos = row_ind)
435435
if (!missing(table2)) {
436-
table <- gtable_add_grob(table, table2[i, ], t = row_ind + 1, l = panel_col, clip = 'off', name = paste0(name, '-', seq_along(panel_col), '-', i))
436+
table <- gtable_add_grob(table, table2[i, ], t = row_ind + 1, l = panel_col, clip = clip, name = paste0(name, "-", seq_along(panel_col), "-", i), z = z)
437437
}
438438
}
439439
table

R/labeller.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ build_strip <- function(label_df, labeller, theme, horizontal) {
493493
horizontal = horizontal)
494494
heights <- unit(apply(grobs, 2, max_height), "cm")
495495
grobs <- apply(grobs, 1, function(strips) {
496-
gtable_matrix("strip", matrix(strips, ncol = 1), unit(1, "null"), heights, clip = "off")
496+
gtable_matrix("strip", matrix(strips, ncol = 1), unit(1, "null"), heights, clip = "on")
497497
})
498498
list(
499499
top = grobs,
@@ -505,7 +505,7 @@ build_strip <- function(label_df, labeller, theme, horizontal) {
505505
grobs_right <- grobs_right[, rev(seq_len(ncol(grobs_right))), drop = FALSE]
506506
widths <- unit(apply(grobs_right, 2, max_width), "cm")
507507
grobs_right <- apply(grobs_right, 1, function(strips) {
508-
gtable_matrix("strip", matrix(strips, nrow = 1), widths, unit(1, "null"), clip = "off")
508+
gtable_matrix("strip", matrix(strips, nrow = 1), widths, unit(1, "null"), clip = "on")
509509
})
510510
theme$strip.text.y$angle <- adjust_angle(theme$strip.text.y$angle)
511511
grobs_left <- apply(labels, c(1, 2), ggstrip, theme = theme,

0 commit comments

Comments
 (0)