Skip to content

Commit bd917cf

Browse files
committed
Move more responsibility to Guides$draw()
1 parent e50017b commit bd917cf

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

R/guides-.R

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -459,29 +459,10 @@ Guides <- ggproto(
459459
},
460460

461461
# 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) {
464463
if (length(guides) == 0) {
465464
return(zeroGrob())
466465
}
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-
}
485466

486467
default_position <- theme$legend.position %||% "right"
487468
if (length(default_position) == 2) {
@@ -490,24 +471,40 @@ Guides <- ggproto(
490471
if (default_position == "none") {
491472
return(zeroGrob())
492473
}
493-
494474
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)
498476
)
499477
positions <- factor(positions, levels = c(.trbl, "inside"))
500478

501479
theme$legend.key.width <- theme$legend.key.width %||% theme$legend.key.size
502480
theme$legend.key.height <- theme$legend.key.height %||% theme$legend.key.size
503481

482+
directions <- rep("vertical", length(positions))
483+
directions[positions %in% c("top", "bottom")] <- "horizontal"
484+
504485
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)
510493
)
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)
511508

512509
# Set spacing
513510
theme$legend.spacing <- theme$legend.spacing %||% unit(0.5, "lines")
@@ -516,7 +513,7 @@ Guides <- ggproto(
516513

517514
Map(
518515
grobs = grobs,
519-
position = levels(positions),
516+
position = names(grobs),
520517
self$package_box,
521518
MoreArgs = list(theme = theme)
522519
)

0 commit comments

Comments
 (0)