Skip to content

Commit 2c1a0d1

Browse files
committed
allow set the inside justification for each legend
1 parent 1de623f commit 2c1a0d1

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

R/guides-.R

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -504,28 +504,37 @@ Guides <- ggproto(
504504
}
505505

506506
# prepare the position of inside legends
507+
default_inside_just <- valid.just(
508+
calc_element("legend.justification.inside", theme)
509+
)
507510
default_inside_position <- calc_element(
508511
"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))
511514

512515
# we grouped the legends by the positions, for inside legends, they'll be
513516
# splitted by the actual inside coordinate
514517
groups <- positions
515518
for (i in seq_along(positions)) {
516519
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
519525
inside_positions[[i]] <- calc_element(
520526
"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 = "_"
524532
)
525533
}
526534
}
527535
positions <- positions[keep]
528536
inside_positions <- inside_positions[keep]
537+
inside_justs <- inside_justs[keep]
529538
groups <- groups[keep]
530539

531540
# we group the guide legends
@@ -539,6 +548,7 @@ Guides <- ggproto(
539548
first_indice <- lapply(indices, `[[`, 1L)
540549
positions <- vec_chop(positions, indices = first_indice)
541550
inside_positions <- vec_chop(inside_positions, indices = first_indice)
551+
inside_justs <- vec_chop(inside_justs, indices = first_indice)
542552

543553
# Set spacing
544554
theme$legend.spacing <- theme$legend.spacing %||% unit(0.5, "lines")
@@ -549,6 +559,7 @@ Guides <- ggproto(
549559
grobs = grobs,
550560
position = positions,
551561
inside_position = inside_positions,
562+
inside_just = inside_justs,
552563
self$package_box,
553564
MoreArgs = list(theme = theme)
554565
)
@@ -584,7 +595,8 @@ Guides <- ggproto(
584595

585596
# here, we put `inside_position` in the last, so that it won't break current
586597
# 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) {
588600
if (is.zero(grobs) || length(grobs) == 0) {
589601
return(zeroGrob())
590602
}
@@ -612,17 +624,20 @@ Guides <- ggproto(
612624
stretch_x <- any(unlist(lapply(widths, unitType)) == "null")
613625
stretch_y <- any(unlist(lapply(heights, unitType)) == "null")
614626

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-
619627
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
626641
if (is.null(inside_position) ||
627642
# `inside_position` is a list of length one
628643
is.null(inside_position <- inside_position[[1L]])) {
@@ -632,9 +647,13 @@ Guides <- ggproto(
632647
x <- inside_position[1L]
633648
y <- inside_position[2L]
634649
}
650+
global_margin <- margin()
635651
} 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]
638657
# Legends to the side of the plot need a margin for justification
639658
# relative to the plot panel
640659
global_margin <- margin(

0 commit comments

Comments
 (0)