diff --git a/NEWS.md b/NEWS.md index f7a93a88e4..9a8a8fc713 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # ggplot2 (development version) +* Strip padding in `facet_grid()` is now only in effect if `strip.placement = "outside"` + _and_ an axis is present between the strip and the panel (@thomasp85, #4610) + * Aesthetics of length 1 are now recycled to 0 if the length of the data is 0 (@thomasp85, #4588) diff --git a/R/facet-grid-.r b/R/facet-grid-.r index c5ec246a70..a31ba9bb2f 100644 --- a/R/facet-grid-.r +++ b/R/facet-grid-.r @@ -356,10 +356,14 @@ FacetGrid <- ggproto("FacetGrid", Facet, theme$panel.spacing.y %||% theme$panel.spacing) # Add axes - panel_table <- gtable_add_rows(panel_table, max_height(axes$x$top), 0) - panel_table <- gtable_add_rows(panel_table, max_height(axes$x$bottom), -1) - panel_table <- gtable_add_cols(panel_table, max_width(axes$y$left), 0) - panel_table <- gtable_add_cols(panel_table, max_width(axes$y$right), -1) + axis_height_top <- max_height(axes$x$top) + axis_height_bottom <- max_height(axes$x$bottom) + axis_width_left <- max_width(axes$y$left) + axis_width_right <- max_width(axes$y$right) + panel_table <- gtable_add_rows(panel_table, axis_height_top, 0) + panel_table <- gtable_add_rows(panel_table, axis_height_bottom, -1) + panel_table <- gtable_add_cols(panel_table, axis_width_left, 0) + panel_table <- gtable_add_cols(panel_table, axis_width_right, -1) panel_pos_col <- panel_cols(panel_table) panel_pos_rows <- panel_rows(panel_table) @@ -377,7 +381,7 @@ FacetGrid <- ggproto("FacetGrid", Facet, panel_pos_col <- panel_cols(panel_table) if (switch_x) { if (!is.null(strips$x$bottom)) { - if (inside_x) { + if (inside_x || as.numeric(axis_height_bottom) == 0) { panel_table <- gtable_add_rows(panel_table, max_height(strips$x$bottom), -2) 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) } else { @@ -388,7 +392,7 @@ FacetGrid <- ggproto("FacetGrid", Facet, } } else { if (!is.null(strips$x$top)) { - if (inside_x) { + if (inside_x || as.numeric(axis_height_top) == 0) { panel_table <- gtable_add_rows(panel_table, max_height(strips$x$top), 1) 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) } else { @@ -401,7 +405,7 @@ FacetGrid <- ggproto("FacetGrid", Facet, panel_pos_rows <- panel_rows(panel_table) if (switch_y) { if (!is.null(strips$y$left)) { - if (inside_y) { + if (inside_y || as.numeric(axis_width_left) == 0) { panel_table <- gtable_add_cols(panel_table, max_width(strips$y$left), 1) 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) } else { @@ -412,7 +416,7 @@ FacetGrid <- ggproto("FacetGrid", Facet, } } else { if (!is.null(strips$y$right)) { - if (inside_y) { + if (inside_y || as.numeric(axis_width_right) == 0) { panel_table <- gtable_add_cols(panel_table, max_width(strips$y$right), -2) 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) } else { diff --git a/tests/testthat/test-facet-strips.r b/tests/testthat/test-facet-strips.r index 4536eb73d7..269fea8e62 100644 --- a/tests/testthat/test-facet-strips.r +++ b/tests/testthat/test-facet-strips.r @@ -131,6 +131,32 @@ test_that("strips can be removed", { expect_true(all(sapply(strip_grobs, inherits, 'zeroGrob'))) }) +test_that("strips can be removed", { + dat <- data_frame(a = rep(LETTERS[1:10], 10), x = rnorm(100), y = rnorm(100)) + g <- ggplot(dat, aes(x = x, y = y)) + + geom_point() + + facet_wrap(~a) + + theme(strip.background = element_blank(), strip.text = element_blank()) + g_grobs <- ggplotGrob(g) + strip_grobs <- g_grobs$grobs[grepl('strip-', g_grobs$layout$name)] + expect_true(all(sapply(strip_grobs, inherits, 'zeroGrob'))) +}) + +test_that("padding is only added if axis is present", { + p <- ggplot(data = mpg, aes(x = displ, y = hwy)) + + facet_grid(. ~ drv) + + theme( + strip.placement = "outside", + strip.switch.pad.grid = unit(10, "mm") + ) + pg <- ggplotGrob(p) + expect_equal(length(pg$heights), 13) + + pg <- ggplotGrob(p + scale_x_continuous(position = "top")) + expect_equal(length(pg$heights), 14) + expect_equal(as.character(pg$heights[7]), "1cm") +}) + test_that("y strip labels are rotated when strips are switched", { switched <- p + facet_grid(am ~ cyl, switch = "both")