Skip to content

Commit fc2dfbc

Browse files
authored
Fix strips from interactions (#3778)
1 parent 40f6751 commit fc2dfbc

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

R/labeller.r

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ labeller <- function(..., .rows = NULL, .cols = NULL,
440440
labellers <- lapply(dots, as_labeller)
441441
} else {
442442
margin_labeller <- as_labeller(margin_labeller, default = .default,
443-
multi_line = .multi_line)
443+
multi_line = .multi_line)
444444

445445
# Check that variable-specific labellers do not overlap with
446446
# margin-wide labeller
@@ -501,29 +501,30 @@ build_strip <- function(label_df, labeller, theme, horizontal) {
501501
labels <- do.call("cbind", labels)
502502

503503
if (horizontal) {
504-
grobs_top <- lapply(labels, element_render, theme = theme,
505-
element = "strip.text.x.top", margin_x = TRUE,
506-
margin_y = TRUE)
504+
grobs_top <- apply(labels, c(1, 2), element_render, theme = theme,
505+
element = "strip.text.x.top", margin_x = TRUE,
506+
margin_y = TRUE)
507507
grobs_top <- assemble_strips(grobs_top, theme, horizontal, clip = "on")
508508

509-
grobs_bottom <- lapply(labels, element_render, theme = theme,
510-
element = "strip.text.x.bottom", margin_x = TRUE,
511-
margin_y = TRUE)
509+
grobs_bottom <- apply(labels, c(1, 2), element_render, theme = theme,
510+
element = "strip.text.x.bottom", margin_x = TRUE,
511+
margin_y = TRUE)
512512
grobs_bottom <- assemble_strips(grobs_bottom, theme, horizontal, clip = "on")
513513

514514
list(
515515
top = grobs_top,
516516
bottom = grobs_bottom
517517
)
518518
} else {
519-
grobs_left <- lapply(labels, element_render, theme = theme,
520-
element = "strip.text.y.left", margin_x = TRUE,
521-
margin_y = TRUE)
519+
grobs_left <- apply(labels, c(1, 2), element_render, theme = theme,
520+
element = "strip.text.y.left", margin_x = TRUE,
521+
margin_y = TRUE)
522522
grobs_left <- assemble_strips(grobs_left, theme, horizontal, clip = "on")
523523

524-
grobs_right <- lapply(labels, element_render, theme = theme,
525-
element = "strip.text.y.right", margin_x = TRUE,
526-
margin_y = TRUE)
524+
grobs_right <- apply(labels[, rev(seq_len(ncol(labels))), drop = FALSE],
525+
c(1, 2), element_render, theme = theme,
526+
element = "strip.text.y.right", margin_x = TRUE,
527+
margin_y = TRUE)
527528
grobs_right <- assemble_strips(grobs_right, theme, horizontal, clip = "on")
528529

529530
list(
@@ -549,7 +550,7 @@ assemble_strips <- function(grobs, theme, horizontal = TRUE, clip) {
549550
if (length(grobs) == 0 || is.zero(grobs[[1]])) return(grobs)
550551

551552
# Add margins to non-titleGrobs so they behave eqivalently
552-
grobs <- lapply(grobs, function(g) {
553+
grobs[] <- lapply(grobs, function(g) {
553554
if (inherits(g, "titleGrob")) return(g)
554555
add_margins(gList(g), grobHeight(g), grobWidth(g), margin_x = TRUE, margin_y = TRUE)
555556
})
@@ -561,7 +562,7 @@ assemble_strips <- function(grobs, theme, horizontal = TRUE, clip) {
561562
height <- unit(1, "null")
562563
width <- max_width(lapply(grobs, function(x) x$widths[2]))
563564
}
564-
grobs <- lapply(grobs, function(x) {
565+
grobs[] <- lapply(grobs, function(x) {
565566
# Avoid unit subset assignment to support R 3.2
566567
x$widths <- unit.c(x$widths[1], width, x$widths[c(-1, -2)])
567568
x$heights <- unit.c(x$heights[1], height, x$heights[c(-1, -2)])
@@ -579,10 +580,16 @@ assemble_strips <- function(grobs, theme, horizontal = TRUE, clip) {
579580
background <- element_render(theme, background)
580581

581582
# Put text on a strip
582-
lapply(grobs, function(x) {
583-
strip <- ggname("strip", gTree(children = gList(background, x)))
584-
strip_table <- gtable(width, height, name = "strip")
585-
gtable_add_grob(strip_table, strip, 1, 1, clip = clip)
583+
grobs[] <- lapply(grobs, function(x) {
584+
ggname("strip", gTree(children = gList(background, x)))
585+
})
586+
apply(grobs, 1, function(x) {
587+
if (horizontal) {
588+
mat <- matrix(x, ncol = 1)
589+
} else {
590+
mat <- matrix(x, nrow = 1)
591+
}
592+
gtable_matrix("strip", mat, rep(width, ncol(mat)), rep(height, nrow(mat)), clip = clip)
586593
})
587594
}
588595

0 commit comments

Comments
 (0)