Skip to content

Commit c151942

Browse files
authored
Make strip clipping optional (#4223)
Fixes #4118
1 parent 37e0094 commit c151942

File tree

7 files changed

+46
-4
lines changed

7 files changed

+46
-4
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+
* `theme()` now has a `strip.clip` argument, that can be set to `"off"` to
4+
prevent the clipping of strip text and background borders (@teunbrand, #4118)
5+
36
* `aes()` now supports the `!!!` operator in its first two arguments
47
(#2675). Thanks to @yutannihilation and @teunbrand for draft
58
implementations.

R/labeller.r

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,18 +498,23 @@ build_strip <- function(label_df, labeller, theme, horizontal) {
498498
ncol <- ncol(labels)
499499
nrow <- nrow(labels)
500500

501+
# Decide strip clipping
502+
clip <- calc_element("strip.clip", theme)[[1]]
503+
clip <- pmatch(clip, c("on", "off", "inherit"), nomatch = 3)
504+
clip <- c("on", "off", "inherit")[clip]
505+
501506
if (horizontal) {
502507
grobs_top <- lapply(labels, element_render, theme = theme,
503508
element = "strip.text.x.top", margin_x = TRUE,
504509
margin_y = TRUE)
505510
grobs_top <- assemble_strips(matrix(grobs_top, ncol = ncol, nrow = nrow),
506-
theme, horizontal, clip = "on")
511+
theme, horizontal, clip = clip)
507512

508513
grobs_bottom <- lapply(labels, element_render, theme = theme,
509514
element = "strip.text.x.bottom", margin_x = TRUE,
510515
margin_y = TRUE)
511516
grobs_bottom <- assemble_strips(matrix(grobs_bottom, ncol = ncol, nrow = nrow),
512-
theme, horizontal, clip = "on")
517+
theme, horizontal, clip = clip)
513518

514519
list(
515520
top = grobs_top,
@@ -520,14 +525,14 @@ build_strip <- function(label_df, labeller, theme, horizontal) {
520525
element = "strip.text.y.left", margin_x = TRUE,
521526
margin_y = TRUE)
522527
grobs_left <- assemble_strips(matrix(grobs_left, ncol = ncol, nrow = nrow),
523-
theme, horizontal, clip = "on")
528+
theme, horizontal, clip = clip)
524529

525530
grobs_right <- lapply(labels[, rev(seq_len(ncol(labels))), drop = FALSE],
526531
element_render, theme = theme,
527532
element = "strip.text.y.right", margin_x = TRUE,
528533
margin_y = TRUE)
529534
grobs_right <- assemble_strips(matrix(grobs_right, ncol = ncol, nrow = nrow),
530-
theme, horizontal, clip = "on")
535+
theme, horizontal, clip = clip)
531536

532537
list(
533538
left = grobs_left,

R/theme-defaults.r

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ theme_grey <- function(base_size = 11, base_family = "",
197197
panel.ontop = FALSE,
198198

199199
strip.background = element_rect(fill = "grey85", colour = NA),
200+
strip.clip = "inherit",
200201
strip.text = element_text(
201202
colour = "grey10",
202203
size = rel(0.8),
@@ -484,6 +485,7 @@ theme_void <- function(base_size = 11, base_family = "",
484485
legend.position = "right",
485486
legend.text = element_text(size = rel(0.8)),
486487
legend.title = element_text(hjust = 0),
488+
strip.clip = "inherit",
487489
strip.text = element_text(size = rel(0.8)),
488490
strip.switch.pad.grid = unit(half_line / 2, "pt"),
489491
strip.switch.pad.wrap = unit(half_line / 2, "pt"),
@@ -609,6 +611,7 @@ theme_test <- function(base_size = 11, base_family = "",
609611
panel.ontop = FALSE,
610612

611613
strip.background = element_rect(fill = "grey85", colour = "grey20"),
614+
strip.clip = "inherit",
612615
strip.text = element_text(
613616
colour = "grey10",
614617
size = rel(0.8),

R/theme-elements.r

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
478478
strip.background = el_def("element_rect", "rect"),
479479
strip.background.x = el_def("element_rect", "strip.background"),
480480
strip.background.y = el_def("element_rect", "strip.background"),
481+
strip.clip = el_def("character"),
481482
strip.text.x = el_def("element_text", "strip.text"),
482483
strip.text.x.top = el_def("element_text", "strip.text.x"),
483484
strip.text.x.bottom = el_def("element_text", "strip.text.x"),

R/theme.r

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@
148148
#' @param strip.placement placement of strip with respect to axes,
149149
#' either "inside" or "outside". Only important when axes and strips are
150150
#' on the same side of the plot.
151+
#' @param strip.clip should strip background edges and strip labels be clipped
152+
#' to the extend of the strip background? Options are `"on"` to clip, `"off"`
153+
#' to disable clipping or `"inherit"` (default) to take the clipping setting
154+
#' from the parent viewport.
151155
#' @param strip.text,strip.text.x,strip.text.y facet labels ([element_text()];
152156
#' inherits from `text`). Horizontal facet labels (`strip.text.x`) & vertical
153157
#' facet labels (`strip.text.y`) inherit from `strip.text` or can be specified
@@ -350,6 +354,7 @@ theme <- function(line,
350354
strip.background,
351355
strip.background.x,
352356
strip.background.y,
357+
strip.clip,
353358
strip.placement,
354359
strip.text,
355360
strip.text.x,

man/theme.Rd

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-facet-strips.r

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,22 @@ test_that("y strip labels are rotated when strips are switched", {
162162

163163
expect_doppelganger("switched facet strips", switched)
164164
})
165+
166+
test_that("strip clipping can be set from the theme", {
167+
labels <- data_frame(var1 = "a")
168+
169+
strip <- render_strips(
170+
labels,
171+
labeller = label_value,
172+
theme = theme_test() + theme(strip.clip = "on")
173+
)
174+
expect_equal(strip$x$top[[1]]$layout$clip, "on")
175+
176+
strip <- render_strips(
177+
labels,
178+
labeller = label_value,
179+
theme = theme_test() + theme(strip.clip = "off")
180+
)
181+
expect_equal(strip$x$top[[1]]$layout$clip, "off")
182+
})
183+

0 commit comments

Comments
 (0)