Add plot_layout(guides="collect") for composition-wide legend collection#1063
Merged
Conversation
`guide` and `guides` each expose `_bind_source(plot)` and `_bind_owner(owner)` instead of a single `setup` that greedily binds layers/mapping/scales (per source plot) and theme/figure (per renderer) at once. Lift `_assemble_guides` and the figure-attach + `targets.legends` write out of the `guides` class into module-level free functions that take theme/figure/targets as explicit parameters. Add a `LegendOwner` Protocol so a future composition-level renderer can satisfy the same binding contract a `ggplot` does today. Pure refactor; no public-API change; no behaviour change.
Adds `guides="collect" | "keep" | None` to `plot_layout`, modelled on patchwork. `"collect"` hoists guides from descendant plots into a single shared legend at the composition level; `"keep"` blocks propagation through a subtree; `None` is transparent. Both `ggplot.guides` and `Compose.guides` are now `guides()` instances; `guides.draw()` dispatches on its `_owner` to either the per-plot path or the cross-plot collect path. `guide_legend.merge` unions `_layer_parameters` so a merged guide overlays glyphs from every contributing plot. `composition_*_space` reserves room for the collected legend, and `set_legends_position` works for both plot and composition owners via a new `owner` property on the side-spaces classes. `test_nested_collect` is marked `xfail` — nested guide owners render correctly but the top-level layout engine only reserves space for the root composition's legend, so the nested legend isn't positioned. Tracked separately.
`guide_area()` (a `plot_spacer` subclass) marks a cell in a composition that should hold the legend produced by `plot_layout(guides="collect")`. It must be a direct child of the collecting composition — nested guide_areas belong to their own sub-grid and are not reached by an outer collector. When no guide_area is eligible, collected guides fall back to side placement.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Patchwork-style guide collection across composed plots. Adding plot_layout(guides="collect") to a composition gathers the legends from its descendants, dedupes by hash, and draws a single set at that level instead of one per plot.