Repository navigation
Fix polygon legend keys in geom_sf() (#6804) - #6923
Open
josefelipe0036 wants to merge 2 commits into
Open
josefelipe0036 wants to merge 2 commits into
josefelipe0036 wants to merge 2 commits into
Conversation
`show.legend = "polygon"` is a documented value for `geom_sf()`, but
`LayerSf$compute_geom_1()` passed it on verbatim while the rest of the sf
machinery calls polygons "other". `GeomSf$use_defaults()` splits legend
key data by `factor(type, c("point", "line", "other", "collection"))`, so
"polygon" became `NA`, every subset was empty and the key lost all of its
aesthetics: an error in `vec_slice()` on ggplot2 4.0.3, an empty key in
the development version.
Translate "polygon" to the internal "other" type, so that the key matches
the one chosen automatically for polygon data.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
josefelipe0036
marked this pull request as ready for review
November 7, 2026 00:02
teunbrand
approved these changes
Nov 11, 2026
teunbrand
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for the contribution! The logic was right, but I've streamlined it a bit so it falls out naturally from the switch() statement above. We'll wait with merging until an intermediate release is resolved.
This branch has not been deployed
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.
Closes #6804.
Diagnosis
"polygon"is one of the documented values ofgeom_sf(show.legend), butLayerSf$compute_geom_1()stores a charactershow.legendverbatim as the legend type, whereas the rest of the sf machinery calls polygons"other"(which is also what the automatic detection returns).For legend keys,
LayerSf$compute_geom_2()writes that type into thegeometrycolumn, andGeomSf$use_defaults()splits the key data withfactor(type, c("point", "line", "other", "collection"))."polygon"is not one of those levels, so it becomesNA, every subset is empty and the key data loses all of its aesthetics:vctrs::vec_slice()receivesNULL, which is the error reported in the issue (backtrace:guide$process_layers()→layer$compute_geom_2()→GeomSf$use_defaults()→vec_slice(data, order(unlist(index)))).linetypefix-up added in 5b928ae turns thatNULLinto an empty list, so there is no error anymore, but the key data that reachesdraw_key()only contains.drawand the legend shows a label without a key.Fix
LayerSf$compute_geom_1()now translates the documented"polygon"into the internal"other"type. Keys drawn withshow.legend = "polygon"are then identical to the polygon keys chosen automatically. The"point","line"and automatic legend types are unaffected.(Normalising in
GeomSf$use_defaults()would also work, but doing it where the legend type is determined keepscomputed_geom_params$legendin the same vocabulary as the automatic detection.)Reprex
Before (main @ b638d65): no key is drawn next to the
alabel.After (this PR): a filled polygon key, the same as with
show.legend = TRUE.Tests
A new test in
test-geom-sf.Rcompares the legend key data ofshow.legend = "polygon"with the automatic polygon key (show.legend = TRUE) for polygon, point and mixed-geometry data. It maps a factor with an unused level and anNA(drop = FALSE, so four keys) and also covers empty layers. It fails on main (3 failures) and passes with this change.Run locally on R 4.6.0 (Windows 11):
devtools::test(): 2979 expectations, 0 failures, 0 errors, 0 warnings.devtools::check(document = TRUE, args = "--no-manual"): 0 errors | 0 warnings | 1 note. The note (checking Rd metadata:Rd files without \alias: 'shared_layer_parameters.Rd') comes fromman/shared_layer_parameters.Rd, which this PR doesn't touch, so it is also present on main.🤖 Generated with Claude Code