@@ -504,28 +504,37 @@ Guides <- ggproto(
504
504
}
505
505
506
506
# prepare the position of inside legends
507
+ default_inside_just <- valid.just(
508
+ calc_element(" legend.justification.inside" , theme )
509
+ )
507
510
default_inside_position <- calc_element(
508
511
" legend.position.inside" , theme
509
- ) % || % valid.just(calc_element( " legend.justification.inside " , theme ))
510
- inside_positions <- vector(" list" , length(positions ))
512
+ )
513
+ inside_justs <- inside_positions <- vector(" list" , length(positions ))
511
514
512
515
# we grouped the legends by the positions, for inside legends, they'll be
513
516
# splitted by the actual inside coordinate
514
517
groups <- positions
515
518
for (i in seq_along(positions )) {
516
519
if (identical(positions [i ], " inside" )) {
517
- # the actual inside position can be set in each guide by `theme`
518
- # argument
520
+ # the actual inside position and justification can be set in each guide
521
+ # by `theme` argument
522
+ inside_justs [[i ]] <- valid.just(calc_element(
523
+ " legend.justification.inside" , params [[i ]]$ theme
524
+ )) %|| % default_inside_just
519
525
inside_positions [[i ]] <- calc_element(
520
526
" legend.position.inside" , params [[i ]]$ theme
521
- ) %|| % default_inside_position
522
- groups [i ] <- paste0(" inside_" ,
523
- paste(inside_positions [[i ]], collapse = " _" )
527
+ ) %|| % default_inside_position %|| % inside_justs [[i ]]
528
+ groups [i ] <- paste(" inside" ,
529
+ paste(inside_positions [[i ]], collapse = " _" ),
530
+ paste(inside_justs [[i ]], collapse = " _" ),
531
+ sep = " _"
524
532
)
525
533
}
526
534
}
527
535
positions <- positions [keep ]
528
536
inside_positions <- inside_positions [keep ]
537
+ inside_justs <- inside_justs [keep ]
529
538
groups <- groups [keep ]
530
539
531
540
# we group the guide legends
@@ -539,6 +548,7 @@ Guides <- ggproto(
539
548
first_indice <- lapply(indices , `[[` , 1L )
540
549
positions <- vec_chop(positions , indices = first_indice )
541
550
inside_positions <- vec_chop(inside_positions , indices = first_indice )
551
+ inside_justs <- vec_chop(inside_justs , indices = first_indice )
542
552
543
553
# Set spacing
544
554
theme $ legend.spacing <- theme $ legend.spacing %|| % unit(0.5 , " lines" )
@@ -549,6 +559,7 @@ Guides <- ggproto(
549
559
grobs = grobs ,
550
560
position = positions ,
551
561
inside_position = inside_positions ,
562
+ inside_just = inside_justs ,
552
563
self $ package_box ,
553
564
MoreArgs = list (theme = theme )
554
565
)
@@ -584,7 +595,8 @@ Guides <- ggproto(
584
595
585
596
# here, we put `inside_position` in the last, so that it won't break current
586
597
# implement of patchwork
587
- package_box = function (grobs , position , theme , inside_position = NULL ) {
598
+ package_box = function (grobs , position , theme ,
599
+ inside_position = NULL , inside_just = NULL ) {
588
600
if (is.zero(grobs ) || length(grobs ) == 0 ) {
589
601
return (zeroGrob())
590
602
}
@@ -612,17 +624,20 @@ Guides <- ggproto(
612
624
stretch_x <- any(unlist(lapply(widths , unitType )) == " null" )
613
625
stretch_y <- any(unlist(lapply(heights , unitType )) == " null" )
614
626
615
- # Global justification of the complete legend box
616
- global_just <- paste0(" legend.justification." , position )
617
- global_just <- valid.just(calc_element(global_just , theme ))
618
-
619
627
if (position == " inside" ) {
620
- # The position of inside legends are set by their justification
621
- inside_just <- theme $ legend.position.inside %|| % global_just
622
- global_xjust <- inside_just [1 ]
623
- global_yjust <- inside_just [2 ]
624
- global_margin <- margin()
625
- # # for backward compatibility, no `inside_position` input
628
+ # for backward compatibility, no `inside_just` input
629
+ if (is.null(inside_just ) ||
630
+ # `inside_just` is a list of length one
631
+ is.null(inside_just <- inside_just [[1L ]])) {
632
+ global_just <- valid.just(
633
+ calc_element(" legend.justification.inside" , theme )
634
+ )
635
+ } else {
636
+ global_just <- inside_just
637
+ }
638
+ global_xjust <- global_just [1 ]
639
+ global_yjust <- global_just [2 ]
640
+ # for backward compatibility, no `inside_position` input
626
641
if (is.null(inside_position ) ||
627
642
# `inside_position` is a list of length one
628
643
is.null(inside_position <- inside_position [[1L ]])) {
@@ -632,9 +647,13 @@ Guides <- ggproto(
632
647
x <- inside_position [1L ]
633
648
y <- inside_position [2L ]
634
649
}
650
+ global_margin <- margin()
635
651
} else {
636
- x <- global_xjust <- global_just [1 ]
637
- y <- global_yjust <- global_just [2 ]
652
+ # Global justification of the complete legend box
653
+ global_just <- paste0(" legend.justification." , position )
654
+ global_just <- valid.just(calc_element(global_just , theme ))
655
+ x <- global_xjust <- global_just [1 ]
656
+ y <- global_yjust <- global_just [2 ]
638
657
# Legends to the side of the plot need a margin for justification
639
658
# relative to the plot panel
640
659
global_margin <- margin(
0 commit comments