Add patch_spacing for proportional row heights in categorical legends#206
Open
mtennekes wants to merge 1 commit into
Open
Add patch_spacing for proportional row heights in categorical legends#206mtennekes wants to merge 1 commit into
mtennekes wants to merge 1 commit into
Conversation
## Summary
Adds a `patch_spacing` argument to `add_categorical_legend()` (threaded through
`add_legend()`) so that legends with per-item `sizes` can size each row to its
own symbol instead of forcing every row to the largest symbol's height.
For graduated/proportional-symbol legends, the current fixed-height rows leave
small symbols floating in oversized boxes, so the vertical gaps look uneven. The
new `"proportional"` option makes each row's height track its symbol size, which
matches how other mapping libraries (e.g. leaflet) render this kind of legend.
## What changed
- New arg `patch_spacing = c("uniform", "proportional")` on
`add_categorical_legend()` and `add_legend()`.
- `"uniform"` (default): every row uses `max(sizes)` — **identical to current
behaviour**.
- `"proportional"`: each row's patch container height is set to that item's
own size. Container *width* stays at `max(sizes)` so labels remain aligned.
- Only affects categorical legends with varying `sizes`; line patches keep their
existing uniform spacing.
## Backward compatibility
The default is `"uniform"`, so existing output is unchanged. No other arguments
or behaviour are touched.
## Example
```r
library(mapgl)
maplibre() |>
add_categorical_legend(
legend_title = "Population",
values = c("Small", "Medium", "Large", "Huge"),
colors = "#3182bd",
patch_shape = "circle",
sizes = c(8, 16, 32, 48),
patch_spacing = "proportional" # rows hug each symbol
)
```
## Notes
- Found this while adding proportional-symbol (donut) legends to `tmap`'s mapgl
backend, where the constant row height was the only thing keeping the size
legend from matching the plot/leaflet output.
- Docs regenerated with `devtools::document()`; a test for the row-height
behaviour is included.
To be added to NEWS.md:
`add_categorical_legend()` and `add_legend()` gain a `patch_spacing` argument
(`"uniform"`/`"proportional"`). With `"proportional"`, each legend row's height
tracks its own symbol size, giving proportional vertical spacing for
graduated-symbol legends. Default `"uniform"` preserves existing behaviour
(#PR)
Best,
Martijn (with some help from Claude)
Author
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.

Summary
Adds a
patch_spacingargument toadd_categorical_legend()(threaded throughadd_legend()) so that legends with per-itemsizescan size each row to its own symbol instead of forcing every row to the largest symbol's height.For graduated/proportional-symbol legends, the current fixed-height rows leave small symbols floating in oversized boxes, so the vertical gaps look uneven. The new
"proportional"option makes each row's height track its symbol size, which matches how other mapping libraries (e.g. leaflet) render this kind of legend.What changed
patch_spacing = c("uniform", "proportional")onadd_categorical_legend()andadd_legend()."uniform"(default): every row usesmax(sizes)— identical to current behaviour."proportional": each row's patch container height is set to that item's own size. Container width stays atmax(sizes)so labels remain aligned.sizes; line patches keep their existing uniform spacing.Backward compatibility
The default is
"uniform", so existing output is unchanged. No other arguments or behaviour are touched.Example
Notes
tmap's mapgl backend, where the constant row height was the only thing keeping the size legend from matching the plot/leaflet output.devtools::document(); a test for the row-height behaviour is included.To be added to NEWS.md:
add_categorical_legend()andadd_legend()gain apatch_spacingargument(
"uniform"/"proportional"). With"proportional", each legend row's heighttracks its own symbol size, giving proportional vertical spacing for
graduated-symbol legends. Default
"uniform"preserves existing behaviour(#PR)
Best,
Martijn (with some help from Claude)