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

Label accessor #6078

Merged
merged 5 commits into from
Sep 23, 2024
Merged

Label accessor #6078

merged 5 commits into from
Sep 23, 2024

Conversation

teunbrand
Copy link
Collaborator

@teunbrand teunbrand commented Sep 5, 2024

This PR aims to fix #6008.

Briefly, this ameliorates the fallout of #5879 by providing a convenient accessor for completed labels that people can access.
The get_labs() function just returns all known labels for a plot.

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

p <- ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = drv, shape = drv))

# Note that colour/shape share a legend
str(get_labs(p))
#> List of 7
#>  $ colour: chr "drv"
#>  $ shape : chr "drv"
#>  $ x.sec : NULL
#>  $ x     : chr "displ"
#>  $ y     : chr "hwy"
#>  $ y.sec : NULL
#>  $ alt   : chr ""

Reverse dependencies that test for the label can then declare the following in their test helpers, and use this instead of trying to access the values directly.

get_labs <- if ("get_labs" %in% getNamespaceExports("ggplot2")) {
  ggplot2::get_labs
} else {
  function(plot) plot$labels
}

Created on 2024-09-05 with reprex v2.1.1

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 171664b into tidyverse:main Sep 23, 2024
13 checks passed
@teunbrand teunbrand deleted the get_labels branch September 23, 2024 09:16
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.

Regressions related to #5879
2 participants