Skip to content

Commit 790fb2e

Browse files
committed
merge guide legends depend on legend.position.inside only
1 parent 0a69e9f commit 790fb2e

File tree

3 files changed

+14
-23
lines changed

3 files changed

+14
-23
lines changed

R/guide-colorbar.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,7 @@ GuideColourbar <- ggproto(
225225
ticks = "legend.ticks",
226226
ticks_length = "legend.ticks.length",
227227
frame = "legend.frame",
228-
inside_position = "legend.position.inside",
229-
inside_justification = "legend.justification.inside"
228+
inside_position = "legend.position.inside"
230229
),
231230

232231
extract_key = function(scale, aesthetic, ...) {

R/guide-legend.R

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,7 @@ GuideLegend <- ggproto(
181181
text_position = "legend.text.position",
182182
title_position = "legend.title.position",
183183
byrow = "legend.byrow",
184-
inside_position = "legend.position.inside",
185-
inside_justification = "legend.justification.inside"
184+
inside_position = "legend.position.inside"
186185
),
187186

188187
extract_params = function(scale, params,
@@ -344,14 +343,7 @@ GuideLegend <- ggproto(
344343
)
345344
)
346345
elements$text <- calc_element("legend.text", add_theme(theme, text))
347-
ans <- Guide$setup_elements(params, elements, theme)
348-
ans$inside_justification <- .subset2(
349-
theme, "legend.justification.inside"
350-
) %||% .subset2(ans, "inside_position")
351-
ans$inside_justification <- valid.just(.subset2(
352-
ans, "inside_justification"
353-
))
354-
ans
346+
Guide$setup_elements(params, elements, theme)
355347
},
356348

357349
override_elements = function(params, elements, theme) {
@@ -571,12 +563,12 @@ GuideLegend <- ggproto(
571563
t = 1, r = -1, b = -1, l = 1, z = -Inf
572564
)
573565
}
574-
# attach the `position` and `justification` for the inside legends
566+
567+
# for inside guide legends, we also save the position values
568+
# in this way, we can identify legends with same position
569+
# and then merge them into same guide box in `Guides$draw()`
575570
if (identical(.subset2(params, "position"), "inside")) {
576571
attr(gt, "inside_position") <- .subset2(elements, "inside_position")
577-
attr(gt, "inside_justification") <- .subset2(
578-
elements, "inside_justification"
579-
)
580572
}
581573
gt
582574
}

R/guides-.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -531,13 +531,12 @@ Guides <- ggproto(
531531
theme = theme, position = positions[i],
532532
direction = directions[i], params = params[[i]]
533533
)
534+
# we'll merge inside legends with same coordinate into same guide box
535+
# here, we define the groups of the inside legends
534536
if (identical(positions[i], "inside")) {
535537
positions[i] <- paste(
536538
"inside",
537539
paste(attr(.subset2(grobs, i), "inside_position"), collapse = "_"),
538-
paste(attr(.subset2(grobs, i), "inside_justification"),
539-
collapse = "_"
540-
),
541540
sep = "_"
542541
)
543542
}
@@ -548,6 +547,9 @@ Guides <- ggproto(
548547
levels = c(.trbl, unique(positions[startsWith(positions, "inside")]))
549548
)
550549
keep <- !vapply(grobs, is.zero, logical(1), USE.NAMES = FALSE)
550+
551+
# we grouped the legends by the positions
552+
# for inside legends, they'll be splitted by the actual inside coordinate
551553
split(grobs[keep], positions[keep])
552554
},
553555

@@ -586,10 +588,8 @@ Guides <- ggproto(
586588
global_just <- paste0("legend.justification.", position)
587589
global_just <- valid.just(calc_element(global_just, theme))
588590
if (startsWith(position, "inside")) {
589-
# The position of inside legends are set by their justification
590-
global_just <- attr(.subset2(grobs, 1L), "inside_justification") %||%
591-
# fallback to original method of ggplot2 <=3.3.5
592-
global_just
591+
# for inside guide legends, the position was attached in
592+
# each grob of the input grobs (which should share the same position)
593593
inside_position <- attr(.subset2(grobs, 1L), "inside_position") %||%
594594
# fallback to original method of ggplot2 <=3.3.5
595595
.subset2(theme, "legend.position.inside") %||% global_just

0 commit comments

Comments
 (0)