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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Label accessor #6078

wants to merge 4 commits into from

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

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
1 participant