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 1 commit
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
28 changes: 15 additions & 13 deletions R/guide-colorbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,21 @@ 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",
inside_justification = "legend.justification.inside"
),

extract_key = function(scale, aesthetic, ...) {
Expand Down
44 changes: 30 additions & 14 deletions R/guide-legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,20 @@ 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",
inside_justification = "legend.justification.inside"
),

extract_params = function(scale, params,
Expand Down Expand Up @@ -342,7 +344,14 @@ GuideLegend <- ggproto(
)
)
elements$text <- calc_element("legend.text", add_theme(theme, text))
Guide$setup_elements(params, elements, theme)
ans <- Guide$setup_elements(params, elements, theme)
ans$inside_justification <- .subset2(
theme, "legend.justification.inside"
) %||% .subset2(ans, "inside_position")
ans$inside_justification <- valid.just(.subset2(
ans, "inside_justification"
))
ans
},

override_elements = function(params, elements, theme) {
Expand Down Expand Up @@ -559,7 +568,14 @@ 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
)
}
# attach the `position` and `justification` for the inside legends
if (identical(.subset2(params, "position"), "inside")) {
attr(gt, "inside_position") <- .subset2(elements, "inside_position")
attr(gt, "inside_justification") <- .subset2(
elements, "inside_justification"
)
}
gt
Expand Down
52 changes: 37 additions & 15 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,26 @@ 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]]
)
if (identical(positions[i], "inside")) {
positions[i] <- paste(
"inside",
paste(attr(.subset2(grobs, i), "inside_position"), collapse = "_"),
paste(attr(.subset2(grobs, i), "inside_justification"),
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)
split(grobs[keep], positions[keep])
},

Expand All @@ -546,8 +560,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 @@ -569,16 +585,22 @@ Guides <- ggproto(
# 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") {
if (startsWith(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 {
global_just <- attr(.subset2(grobs, 1L), "inside_justification") %||%
# fallback to original method of ggplot2 <=3.3.5
global_just
inside_position <- attr(.subset2(grobs, 1L), "inside_position") %||%
# fallback to original method of ggplot2 <=3.3.5
.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 {
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 +642,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 +680,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
14 changes: 8 additions & 6 deletions R/plot-build.R
Original file line number Diff line number Diff line change
Expand Up @@ -515,12 +515,14 @@ 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"))
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 = "-")
)
}
table
}

Expand Down
Loading