Skip to content
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

Let Layer$compute_geom_2() handle legend defaults #5903

Merged
merged 8 commits into from
Jun 4, 2024

Conversation

teunbrand
Copy link
Collaborator

This PR is an amendment to #5834, as it didn't handle the non-standard defaults for geom_sf() legends.

Briefly, this PR let's Layer$compute_geom_2() instead of Geom$use_defaults() handle populating legend keys with defaults.

The legend code was mirroring Layer$compute_geom_2() in many ways anyway, so this shouldn't be much of a stretch.
This ensures that the layer could impose some of its non-aesthetic parameters on the legend keys.
That is needed to resolve the defaults for the correct legend type in geom_sf(), which we'd need for #5833.

In addition the following changes are made:

  • Layer$compute_geom_2() now accepts a custom params object, as legends only pass static aesthetics when they are of length 1.
  • The sf legend detection mechanism has moved from LayerSf$setup_layer() to LayerSf$compute_geom_1(). This allows for more readable code and abolishes the stateful computed_legend_key_type field in LayerSf.
  • There is now a visual test for geom_sf()'s legend types, because we had detected problems in revdepchecks that weren't covered in our own tests.
  • There is now a test for legend keys when a geom does not having matching aesthetics, which we didn't cover before.

Comment on lines -77 to -83

# If `geometry` is not a symbol, `LayerSf$setup_layer()` gives up guessing
# the legend type, and falls back to "polygon"
p <- ggplot_build(
ggplot(d_sf) + geom_sf(aes(geometry = identity(g_point), colour = "a"))
)
expect_identical(p$plot$layers[[1]]$computed_geom_params$legend, "polygon")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The legend type is now correctly determined to be a point type, so this test became vestigial

data$.draw <- keep_key_data(params$key, df, matched_aes, layer$show.legend)
} else {
reps <- rep(1, nrow(params$key))
data <- layer$geom$use_defaults(NULL, layer$aes_params)[reps, ]
Copy link
Collaborator Author

@teunbrand teunbrand May 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The visual test for the non-sf legend is to cover this case, which gets solved by Layer$compute_geom_2() now, but was never triggered in the test suite.

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@teunbrand teunbrand merged commit 6c975e8 into tidyverse:main Jun 4, 2024
11 checks passed
@teunbrand teunbrand deleted the simplify_legend branch June 4, 2024 10:19
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.

2 participants