Skip to content

Commit 940658a

Browse files
authored
Negative tick strips (#5258)
* Test for presence/absence instead of size * Add test
1 parent e219ddb commit 940658a

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

R/facet-grid-.R

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

369369
# Add axes
370-
axis_height_top <- max_height(axes$x$top)
371-
axis_height_bottom <- max_height(axes$x$bottom)
372-
axis_width_left <- max_width(axes$y$left)
373-
axis_width_right <- max_width(axes$y$right)
374-
panel_table <- gtable_add_rows(panel_table, axis_height_top, 0)
375-
panel_table <- gtable_add_rows(panel_table, axis_height_bottom, -1)
376-
panel_table <- gtable_add_cols(panel_table, axis_width_left, 0)
377-
panel_table <- gtable_add_cols(panel_table, axis_width_right, -1)
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)
378374
panel_pos_col <- panel_cols(panel_table)
379375
panel_pos_rows <- panel_rows(panel_table)
380376

@@ -392,7 +388,7 @@ FacetGrid <- ggproto("FacetGrid", Facet,
392388
panel_pos_col <- panel_cols(panel_table)
393389
if (switch_x) {
394390
if (!is.null(strips$x$bottom)) {
395-
if (inside_x || as.numeric(axis_height_bottom) == 0) {
391+
if (inside_x || all(vapply(axes$x$bottom, is.zero, logical(1)))) {
396392
panel_table <- gtable_add_rows(panel_table, max_height(strips$x$bottom), -2)
397393
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)
398394
} else {
@@ -403,7 +399,7 @@ FacetGrid <- ggproto("FacetGrid", Facet,
403399
}
404400
} else {
405401
if (!is.null(strips$x$top)) {
406-
if (inside_x || as.numeric(axis_height_top) == 0) {
402+
if (inside_x || all(vapply(axes$x$top, is.zero, logical(1)))) {
407403
panel_table <- gtable_add_rows(panel_table, max_height(strips$x$top), 1)
408404
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)
409405
} else {
@@ -416,7 +412,7 @@ FacetGrid <- ggproto("FacetGrid", Facet,
416412
panel_pos_rows <- panel_rows(panel_table)
417413
if (switch_y) {
418414
if (!is.null(strips$y$left)) {
419-
if (inside_y || as.numeric(axis_width_left) == 0) {
415+
if (inside_y || all(vapply(axes$y$left, is.zero, logical(1)))) {
420416
panel_table <- gtable_add_cols(panel_table, max_width(strips$y$left), 1)
421417
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)
422418
} else {
@@ -427,7 +423,7 @@ FacetGrid <- ggproto("FacetGrid", Facet,
427423
}
428424
} else {
429425
if (!is.null(strips$y$right)) {
430-
if (inside_y || as.numeric(axis_width_right) == 0) {
426+
if (inside_y || all(vapply(axes$y$right, is.zero, logical(1)))) {
431427
panel_table <- gtable_add_cols(panel_table, max_width(strips$y$right), -2)
432428
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)
433429
} else {

tests/testthat/test-facet-strips.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ test_that("padding is only added if axis is present", {
159159
pg <- ggplotGrob(p + scale_x_continuous(position = "top"))
160160
expect_equal(length(pg$heights), 14)
161161
expect_equal(as.character(pg$heights[7]), "1cm")
162+
163+
# Also add padding with negative ticks and no text (#5251)
164+
pg <- ggplotGrob(
165+
p + scale_x_continuous(labels = NULL, position = "top") +
166+
theme(axis.ticks.length.x.top = unit(-2, "mm"))
167+
)
168+
expect_equal(length(pg$heights), 14)
169+
expect_equal(as.character(pg$heights[7]), "1cm")
162170
})
163171

164172
test_that("y strip labels are rotated when strips are switched", {

0 commit comments

Comments
 (0)