@@ -440,7 +440,7 @@ labeller <- function(..., .rows = NULL, .cols = NULL,
440
440
labellers <- lapply(dots , as_labeller )
441
441
} else {
442
442
margin_labeller <- as_labeller(margin_labeller , default = .default ,
443
- multi_line = .multi_line )
443
+ multi_line = .multi_line )
444
444
445
445
# Check that variable-specific labellers do not overlap with
446
446
# margin-wide labeller
@@ -501,29 +501,30 @@ build_strip <- function(label_df, labeller, theme, horizontal) {
501
501
labels <- do.call(" cbind" , labels )
502
502
503
503
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 )
507
507
grobs_top <- assemble_strips(grobs_top , theme , horizontal , clip = " on" )
508
508
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 )
512
512
grobs_bottom <- assemble_strips(grobs_bottom , theme , horizontal , clip = " on" )
513
513
514
514
list (
515
515
top = grobs_top ,
516
516
bottom = grobs_bottom
517
517
)
518
518
} 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 )
522
522
grobs_left <- assemble_strips(grobs_left , theme , horizontal , clip = " on" )
523
523
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 )
527
528
grobs_right <- assemble_strips(grobs_right , theme , horizontal , clip = " on" )
528
529
529
530
list (
@@ -549,7 +550,7 @@ assemble_strips <- function(grobs, theme, horizontal = TRUE, clip) {
549
550
if (length(grobs ) == 0 || is.zero(grobs [[1 ]])) return (grobs )
550
551
551
552
# Add margins to non-titleGrobs so they behave eqivalently
552
- grobs <- lapply(grobs , function (g ) {
553
+ grobs [] <- lapply(grobs , function (g ) {
553
554
if (inherits(g , " titleGrob" )) return (g )
554
555
add_margins(gList(g ), grobHeight(g ), grobWidth(g ), margin_x = TRUE , margin_y = TRUE )
555
556
})
@@ -561,7 +562,7 @@ assemble_strips <- function(grobs, theme, horizontal = TRUE, clip) {
561
562
height <- unit(1 , " null" )
562
563
width <- max_width(lapply(grobs , function (x ) x $ widths [2 ]))
563
564
}
564
- grobs <- lapply(grobs , function (x ) {
565
+ grobs [] <- lapply(grobs , function (x ) {
565
566
# Avoid unit subset assignment to support R 3.2
566
567
x $ widths <- unit.c(x $ widths [1 ], width , x $ widths [c(- 1 , - 2 )])
567
568
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) {
579
580
background <- element_render(theme , background )
580
581
581
582
# 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 )
586
593
})
587
594
}
588
595
0 commit comments