Skip to content

check if axis is present before adding padding to facet_grid #4669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
20 changes: 12 additions & 8 deletions R/facet-grid-.r
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions tests/testthat/test-facet-strips.r
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down