Skip to content

Commit 1160768

Browse files
committed
Ensure aesthetics/scales are parallel
1 parent a722ba6 commit 1160768

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

R/guides-.R

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,19 @@ Guides <- ggproto(
301301
horizontal = c("center", "top")
302302
)
303303

304-
# Setup and train on scales
304+
# Extract the non-position scales
305305
scales <- scales$non_position_scales()$scales
306306
if (length(scales) == 0) {
307307
return(no_guides)
308308
}
309-
guides <- self$setup(scales)
309+
310+
# Ensure a 1:1 mapping between aesthetics and scales
311+
aesthetics <- lapply(scales, `[[`, "aesthetics")
312+
scales <- rep.int(scales, lengths(aesthetics))
313+
aesthetics <- unlist(aesthetics, FALSE, FALSE)
314+
315+
# Setup and train scales
316+
guides <- self$setup(scales, aesthetics = aesthetics)
310317
guides$train(scales, theme$legend.direction, labels)
311318
if (length(guides$guides) == 0) {
312319
return(no_guides)
@@ -343,28 +350,16 @@ Guides <- ggproto(
343350
default = self$missing,
344351
missing = self$missing
345352
) {
346-
347-
if (is.null(aesthetics)) {
348-
# Aesthetics from scale, as in non-position guides
349-
aesthetics <- lapply(scales, `[[`, "aesthetics")
350-
scale_idx <- rep(seq_along(scales), lengths(aesthetics))
351-
aesthetics <- unlist(aesthetics, FALSE, FALSE)
352-
} else {
353-
# Scale based on aesthetics, as in position guides
354-
scale_idx <- seq_along(scales)[match(aesthetics, names(scales))]
355-
}
356-
357353
guides <- self$guides
358354

359355
# For every aesthetic-scale combination, find and validate guide
360-
new_guides <- lapply(seq_along(scale_idx), function(i) {
361-
idx <- scale_idx[i]
356+
new_guides <- lapply(seq_along(scales), function(idx) {
362357

363358
# Find guide for aesthetic-scale combination
364359
# Hierarchy is in the order:
365360
# plot + guides(XXX) + scale_ZZZ(guide = XXX) > default(i.e., legend)
366361
guide <- resolve_guide(
367-
aesthetic = aesthetics[i],
362+
aesthetic = aesthetics[idx],
368363
scale = scales[[idx]],
369364
guides = guides,
370365
default = default,

0 commit comments

Comments
 (0)