Skip to content

Add patch_spacing for proportional row heights in categorical legends#206

Open
mtennekes wants to merge 1 commit into
walkerke:mainfrom
mtennekes:patch-1
Open

Add patch_spacing for proportional row heights in categorical legends#206
mtennekes wants to merge 1 commit into
walkerke:mainfrom
mtennekes:patch-1

Conversation

@mtennekes

Copy link
Copy Markdown

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

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)

## 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)
@mtennekes

Copy link
Copy Markdown
Author

Reason for this PR is to get rid of the space between smaller bubbles:

Screenshot 2026-06-14 at 20 56 06

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant