Skip to content

multiple inside guide box with different position #6210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0a69e9f
multiple inside guide box with different position
Yunuuuu Nov 28, 2024
790fb2e
merge guide legends depend on `legend.position.inside` only
Yunuuuu Nov 29, 2024
7e156d0
fix inside legend justification
Yunuuuu Nov 29, 2024
1d7c5b9
no inside guide box if no inside guide legends
Yunuuuu Nov 29, 2024
c6e90ea
add `guide-box-index` when there is no inside legends
Yunuuuu Nov 29, 2024
51889d4
fix inside guide box area
Yunuuuu Nov 29, 2024
b7754c8
manage position in `Guides$assemble()`
Yunuuuu Nov 29, 2024
d7aa7c4
revert
Yunuuuu Nov 29, 2024
a4f1221
revert
Yunuuuu Nov 29, 2024
d1f6833
revert
Yunuuuu Nov 29, 2024
c4882c6
fix inside legend coordinates
Yunuuuu Nov 29, 2024
9854c58
fix test error
Yunuuuu Nov 29, 2024
1de623f
no need to prepare inside legends when empty
Yunuuuu Nov 29, 2024
2c1a0d1
allow set the inside justification for each legend
Yunuuuu Nov 29, 2024
04827f5
test multiple inside legends with different positions
Yunuuuu Nov 29, 2024
92f57f8
fix R CMD check error
Yunuuuu Nov 29, 2024
e381fa3
code notes
Yunuuuu Nov 29, 2024
acbf747
Update R/guides-.R
Yunuuuu Dec 4, 2024
4dd4970
apply suggestion
Yunuuuu Dec 4, 2024
e7cf7de
avoid modify package_box
Yunuuuu Dec 4, 2024
40f13bf
accept snapshot
teunbrand Dec 4, 2024
a634f20
Update R/guides-.R
Yunuuuu Dec 4, 2024
6e41f07
accept the suggestion
Yunuuuu Dec 4, 2024
df64aa2
new bullet
Yunuuuu Dec 4, 2024
e84a4a4
try to linearise logic
teunbrand Dec 4, 2024
20ce959
Merge branch 'main' into Yunuuuu-multiple_inside_legend_box
teunbrand Dec 4, 2024
6c8b526
tweak formatting
teunbrand Dec 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions R/guide-colorbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,20 @@ GuideColourbar <- ggproto(
hashables = exprs(title, key$.label, decor, name),

elements = list(
background = "legend.background",
margin = "legend.margin",
key = "legend.key",
key_height = "legend.key.height",
key_width = "legend.key.width",
text = "legend.text",
theme.title = "legend.title",
text_position = "legend.text.position",
title_position = "legend.title.position",
axis_line = "legend.axis.line",
ticks = "legend.ticks",
ticks_length = "legend.ticks.length",
frame = "legend.frame"
background = "legend.background",
margin = "legend.margin",
key = "legend.key",
key_height = "legend.key.height",
key_width = "legend.key.width",
text = "legend.text",
theme.title = "legend.title",
text_position = "legend.text.position",
title_position = "legend.title.position",
axis_line = "legend.axis.line",
ticks = "legend.ticks",
ticks_length = "legend.ticks.length",
frame = "legend.frame",
inside_position = "legend.position.inside"
),

extract_key = function(scale, aesthetic, ...) {
Expand Down
34 changes: 21 additions & 13 deletions R/guide-legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,19 @@ GuideLegend <- ggproto(
hashables = exprs(title, key$.label, name),

elements = list(
background = "legend.background",
margin = "legend.margin",
key = "legend.key",
key_height = "legend.key.height",
key_width = "legend.key.width",
text = "legend.text",
theme.title = "legend.title",
spacing_x = "legend.key.spacing.x",
spacing_y = "legend.key.spacing.y",
text_position = "legend.text.position",
title_position = "legend.title.position",
byrow = "legend.byrow"
background = "legend.background",
margin = "legend.margin",
key = "legend.key",
key_height = "legend.key.height",
key_width = "legend.key.width",
text = "legend.text",
theme.title = "legend.title",
spacing_x = "legend.key.spacing.x",
spacing_y = "legend.key.spacing.y",
text_position = "legend.text.position",
title_position = "legend.title.position",
byrow = "legend.byrow",
inside_position = "legend.position.inside"
),

extract_params = function(scale, params,
Expand Down Expand Up @@ -559,9 +560,16 @@ GuideLegend <- ggproto(
gt <- gtable_add_grob(
gt, elements$background,
name = "background", clip = "off",
t = 1, r = -1, b = -1, l =1, z = -Inf
t = 1, r = -1, b = -1, l = 1, z = -Inf
)
}

# for inside guide legends, we also save the position values
# in this way, we can identify legends with same position
# and then merge them into same guide box in `Guides$draw()`
if (identical(.subset2(params, "position"), "inside")) {
attr(gt, "inside_position") <- .subset2(elements, "inside_position")
}
gt
}
)
Expand Down
64 changes: 45 additions & 19 deletions R/guides-.R
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,8 @@ Guides <- ggproto(
positions <- vapply(
params,
function(p) p$position[1] %||% default_position,
character(1)
character(1), USE.NAMES = FALSE
)
positions <- factor(positions, levels = c(.trbl, "inside"))

directions <- rep(direction %||% "vertical", length(positions))
if (is.null(direction)) {
Expand All @@ -529,11 +528,28 @@ Guides <- ggproto(
grobs <- vector("list", length(guides))
for (i in seq_along(grobs)) {
grobs[[i]] <- guides[[i]]$draw(
theme = theme, position = as.character(positions[i]),
theme = theme, position = positions[i],
direction = directions[i], params = params[[i]]
)
# we'll merge inside legends with same coordinate into same guide box
# here, we define the groups of the inside legends
if (identical(positions[i], "inside")) {
positions[i] <- paste(
"inside",
paste(attr(.subset2(grobs, i), "inside_position"), collapse = "_"),
sep = "_"
)
}
}
keep <- !vapply(grobs, is.zero, logical(1))

# move inside legends to the last
positions <- factor(positions,
levels = c(.trbl, unique(positions[startsWith(positions, "inside")]))
)
keep <- !vapply(grobs, is.zero, logical(1), USE.NAMES = FALSE)

# we grouped the legends by the positions
# for inside legends, they'll be splitted by the actual inside coordinate
split(grobs[keep], positions[keep])
},

Expand All @@ -546,8 +562,10 @@ Guides <- ggproto(
# Determine default direction
direction <- switch(
position,
inside = , left = , right = "vertical",
top = , bottom = "horizontal"
left = , right = "vertical",
top = , bottom = "horizontal",
# for all inside guide legends
"vertical"
)

# Populate missing theme arguments
Expand All @@ -566,19 +584,27 @@ Guides <- ggproto(
stretch_x <- any(unlist(lapply(widths, unitType)) == "null")
stretch_y <- any(unlist(lapply(heights, unitType)) == "null")

# Global justification of the complete legend box
global_just <- paste0("legend.justification.", position)
global_just <- valid.just(calc_element(global_just, theme))

if (position == "inside") {
# The position of inside legends are set by their justification
inside_position <- theme$legend.position.inside %||% global_just
global_xjust <- inside_position[1]
global_yjust <- inside_position[2]
global_margin <- margin()
} else {
if (startsWith(position, "inside")) {
# Global justification of the complete legend box
global_just <- valid.just(calc_element(
"legend.justification.inside", theme
))
# for inside guide legends, the position was attached in
# each grob of the input grobs (which should share the same position)
inside_position <- attr(.subset2(grobs, 1L), "inside_position") %||%
# fallback to original method of ggplot2 <=3.5.1
.subset2(theme, "legend.position.inside") %||% global_just
global_xjust <- global_just[1]
global_yjust <- global_just[2]
x <- inside_position[1]
y <- inside_position[2]
global_margin <- margin()
} else {
# Global justification of the complete legend box
global_just <- paste0("legend.justification.", position)
global_just <- valid.just(calc_element(global_just, theme))
x <- global_xjust <- global_just[1]
y <- global_yjust <- global_just[2]
# Legends to the side of the plot need a margin for justification
# relative to the plot panel
global_margin <- margin(
Expand Down Expand Up @@ -620,7 +646,7 @@ Guides <- ggproto(

# Set global justification
vp <- viewport(
x = global_xjust, y = global_yjust, just = global_just,
x = x, y = y, just = global_just,
height = max(heights),
width = vp_width
)
Expand Down Expand Up @@ -658,7 +684,7 @@ Guides <- ggproto(

# Set global justification
vp <- viewport(
x = global_xjust, y = global_yjust, just = global_just,
x = x, y = y, just = global_just,
height = vp_height,
width = max(widths)
)
Expand Down
22 changes: 16 additions & 6 deletions R/plot-build.R
Original file line number Diff line number Diff line change
Expand Up @@ -515,12 +515,22 @@ table_add_legends <- function(table, legends, theme) {

# Add manual legend
place <- find_panel(table)
table <- gtable_add_grob(
table, legends$inside, clip = "off",
t = place$t, b = place$b, l = place$l, r = place$r,
name = "guide-box-inside"
)

inside_legends <- .subset(legends, startsWith(names(legends), "inside"))
if (length(inside_legends)) {
for (i in seq_along(inside_legends)) {
table <- gtable_add_grob(
table, .subset2(inside_legends, i), clip = "off",
t = place$t, b = place$b, l = place$l, r = place$r,
name = paste("guide-box-inside", i, sep = "-")
)
}
} else { # to be consistent with original gtable layout
table <- gtable_add_grob(
table, zeroGrob(), clip = "off",
t = place$t, b = place$b, l = place$l, r = place$r,
name = "guide-box-inside"
)
}
table
}

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ test_that("empty guides are dropped", {
guides <- p$plot$guides$draw(theme_gray(), direction = "vertical")

# All guide-boxes should be empty
expect_equal(lengths(guides, use.names = FALSE), rep(0, 5))
expect_equal(lengths(guides, use.names = FALSE), rep(0, 4))
})

test_that("bins can be parsed by guides for all scale types", {
Expand Down
Loading