Skip to content

Commit 199eb90

Browse files
authored
check if axis is present before adding padding to facet_grid (#4669)
1 parent 65d3bfc commit 199eb90

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# ggplot2 (development version)
22

3+
* Strip padding in `facet_grid()` is now only in effect if `strip.placement = "outside"`
4+
_and_ an axis is present between the strip and the panel (@thomasp85, #4610)
5+
36
* Aesthetics of length 1 are now recycled to 0 if the length of the data is 0
47
(@thomasp85, #4588)
58

R/facet-grid-.r

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,14 @@ FacetGrid <- ggproto("FacetGrid", Facet,
356356
theme$panel.spacing.y %||% theme$panel.spacing)
357357

358358
# Add axes
359-
panel_table <- gtable_add_rows(panel_table, max_height(axes$x$top), 0)
360-
panel_table <- gtable_add_rows(panel_table, max_height(axes$x$bottom), -1)
361-
panel_table <- gtable_add_cols(panel_table, max_width(axes$y$left), 0)
362-
panel_table <- gtable_add_cols(panel_table, max_width(axes$y$right), -1)
359+
axis_height_top <- max_height(axes$x$top)
360+
axis_height_bottom <- max_height(axes$x$bottom)
361+
axis_width_left <- max_width(axes$y$left)
362+
axis_width_right <- max_width(axes$y$right)
363+
panel_table <- gtable_add_rows(panel_table, axis_height_top, 0)
364+
panel_table <- gtable_add_rows(panel_table, axis_height_bottom, -1)
365+
panel_table <- gtable_add_cols(panel_table, axis_width_left, 0)
366+
panel_table <- gtable_add_cols(panel_table, axis_width_right, -1)
363367
panel_pos_col <- panel_cols(panel_table)
364368
panel_pos_rows <- panel_rows(panel_table)
365369

@@ -377,7 +381,7 @@ FacetGrid <- ggproto("FacetGrid", Facet,
377381
panel_pos_col <- panel_cols(panel_table)
378382
if (switch_x) {
379383
if (!is.null(strips$x$bottom)) {
380-
if (inside_x) {
384+
if (inside_x || as.numeric(axis_height_bottom) == 0) {
381385
panel_table <- gtable_add_rows(panel_table, max_height(strips$x$bottom), -2)
382386
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)
383387
} else {
@@ -388,7 +392,7 @@ FacetGrid <- ggproto("FacetGrid", Facet,
388392
}
389393
} else {
390394
if (!is.null(strips$x$top)) {
391-
if (inside_x) {
395+
if (inside_x || as.numeric(axis_height_top) == 0) {
392396
panel_table <- gtable_add_rows(panel_table, max_height(strips$x$top), 1)
393397
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)
394398
} else {
@@ -401,7 +405,7 @@ FacetGrid <- ggproto("FacetGrid", Facet,
401405
panel_pos_rows <- panel_rows(panel_table)
402406
if (switch_y) {
403407
if (!is.null(strips$y$left)) {
404-
if (inside_y) {
408+
if (inside_y || as.numeric(axis_width_left) == 0) {
405409
panel_table <- gtable_add_cols(panel_table, max_width(strips$y$left), 1)
406410
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)
407411
} else {
@@ -412,7 +416,7 @@ FacetGrid <- ggproto("FacetGrid", Facet,
412416
}
413417
} else {
414418
if (!is.null(strips$y$right)) {
415-
if (inside_y) {
419+
if (inside_y || as.numeric(axis_width_right) == 0) {
416420
panel_table <- gtable_add_cols(panel_table, max_width(strips$y$right), -2)
417421
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)
418422
} else {

tests/testthat/test-facet-strips.r

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,32 @@ test_that("strips can be removed", {
131131
expect_true(all(sapply(strip_grobs, inherits, 'zeroGrob')))
132132
})
133133

134+
test_that("strips can be removed", {
135+
dat <- data_frame(a = rep(LETTERS[1:10], 10), x = rnorm(100), y = rnorm(100))
136+
g <- ggplot(dat, aes(x = x, y = y)) +
137+
geom_point() +
138+
facet_wrap(~a) +
139+
theme(strip.background = element_blank(), strip.text = element_blank())
140+
g_grobs <- ggplotGrob(g)
141+
strip_grobs <- g_grobs$grobs[grepl('strip-', g_grobs$layout$name)]
142+
expect_true(all(sapply(strip_grobs, inherits, 'zeroGrob')))
143+
})
144+
145+
test_that("padding is only added if axis is present", {
146+
p <- ggplot(data = mpg, aes(x = displ, y = hwy)) +
147+
facet_grid(. ~ drv) +
148+
theme(
149+
strip.placement = "outside",
150+
strip.switch.pad.grid = unit(10, "mm")
151+
)
152+
pg <- ggplotGrob(p)
153+
expect_equal(length(pg$heights), 13)
154+
155+
pg <- ggplotGrob(p + scale_x_continuous(position = "top"))
156+
expect_equal(length(pg$heights), 14)
157+
expect_equal(as.character(pg$heights[7]), "1cm")
158+
})
159+
134160
test_that("y strip labels are rotated when strips are switched", {
135161
switched <- p + facet_grid(am ~ cyl, switch = "both")
136162

0 commit comments

Comments
 (0)