@@ -459,29 +459,10 @@ Guides <- ggproto(
459
459
},
460
460
461
461
# Loop over every guide, let them draw their grobs
462
- draw = function (self , params = self $ params , guides = self $ guides ,
463
- theme , position ) {
462
+ draw = function (self , theme , params = self $ params , guides = self $ guides ) {
464
463
if (length(guides ) == 0 ) {
465
464
return (zeroGrob())
466
465
}
467
- direction <- switch (
468
- position ,
469
- inside = , left = , right = " vertical" ,
470
- top = , bottom = " horizontal"
471
- )
472
- Map(
473
- function (guide , params ) guide $ draw(theme , position , direction , params ),
474
- guide = guides ,
475
- params = params
476
- )
477
- },
478
-
479
- # Combining multiple guides in a guide box
480
- assemble = function (self , theme ) {
481
-
482
- if (length(self $ guides ) < 1 ) {
483
- return (zeroGrob())
484
- }
485
466
486
467
default_position <- theme $ legend.position %|| % " right"
487
468
if (length(default_position ) == 2 ) {
@@ -490,24 +471,40 @@ Guides <- ggproto(
490
471
if (default_position == " none" ) {
491
472
return (zeroGrob())
492
473
}
493
-
494
474
positions <- vapply(
495
- self $ params ,
496
- function (p ) p $ position %|| % default_position ,
497
- character (1 )
475
+ params , function (p ) p $ position [1 ] %|| % default_position , character (1 )
498
476
)
499
477
positions <- factor (positions , levels = c(.trbl , " inside" ))
500
478
501
479
theme $ legend.key.width <- theme $ legend.key.width %|| % theme $ legend.key.size
502
480
theme $ legend.key.height <- theme $ legend.key.height %|| % theme $ legend.key.size
503
481
482
+ directions <- rep(" vertical" , length(positions ))
483
+ directions [positions %in% c(" top" , " bottom" )] <- " horizontal"
484
+
504
485
grobs <- Map(
505
- params = split(self $ params , positions ),
506
- guides = split(self $ guides , positions ),
507
- position = levels(positions ),
508
- f = self $ draw ,
509
- MoreArgs = list (theme = theme )
486
+ function (guide , params , position , direction ) {
487
+ guide $ draw(theme , position , direction , params )
488
+ },
489
+ guide = guides ,
490
+ params = params ,
491
+ direction = directions ,
492
+ position = as.character(positions )
510
493
)
494
+ split(grobs , positions )
495
+ },
496
+
497
+ # Combining multiple guides in a guide box
498
+ assemble = function (self , theme ) {
499
+
500
+ if (length(self $ guides ) < 1 ) {
501
+ return (zeroGrob())
502
+ }
503
+
504
+ theme $ legend.key.width <- theme $ legend.key.width %|| % theme $ legend.key.size
505
+ theme $ legend.key.height <- theme $ legend.key.height %|| % theme $ legend.key.size
506
+
507
+ grobs <- self $ draw(theme )
511
508
512
509
# Set spacing
513
510
theme $ legend.spacing <- theme $ legend.spacing %|| % unit(0.5 , " lines" )
@@ -516,7 +513,7 @@ Guides <- ggproto(
516
513
517
514
Map(
518
515
grobs = grobs ,
519
- position = levels( positions ),
516
+ position = names( grobs ),
520
517
self $ package_box ,
521
518
MoreArgs = list (theme = theme )
522
519
)
0 commit comments