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

WIP: Extracting guide data #5096

Closed
wants to merge 6 commits into from
Closed

Conversation

teunbrand
Copy link
Collaborator

This PR aims to address issues such as #5004.

Briefly:
It provides a new function, guide_data() to programmatically access values and labels (internally called 'keys') of axes and legends.

Less brief:
Currently, the way to reconstruct, for example axis tick positions, is to rummage around in ggplot2's structures. As laid out by Thomas here, querying data structures is a gray area. Providing a convenient accessor to the guide information is nice for two reasons. 1) It provides a user with a reliable way to extract the information and 2), as Thomas mentions in the blog, it leaves the developers free to change internal structures without worrying too much that a downstream dependency breaks due to internal rearrangements. It should, in theory, prevent surprises such as #5063, #5029 or #3436 (comment).

WIP:

  • I had to refactor the ticks code for CoordSf a bit, by capturing the logic in a function that I could reuse in guide_data(). However, should the graticule-to-ticks logic be a ggproto method of CoordSf or should it live as a separate function?
  • Is guide_data() a good name? Are there better ones? I'll take suggestions.
  • Any other feedback is welcome too!

The examples reprex'ed:

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

# A typical plot
p <- ggplot(mtcars) +
  aes(mpg, disp, colour = drat, size = drat) +
  geom_point() +
  facet_wrap(vars(cyl), scales = "free_x")

# Getting guide data for position aesthetic in particular panel
guide_data(p, "x", i = 1, j = 2)
#> [[1]]
#>           x .value .label y
#> 1 0.0959596     18     18 0
#> 2 0.3484848     19     19 0
#> 3 0.6010101     20     20 0
#> 4 0.8535354     21     21 0

# Getting guide data for a legend
guide_data(p, "size")
#> [[1]]
#>       size .label
#> 1 2.662822    3.0
#> 2 3.919819    3.5
#> 3 4.779645    4.0
#> 4 5.477285    4.5

# If guides are merged, `guide_data()` reports merged guide data
guide_data(p + guides(colour = "legend"), "size")
#> [[1]]
#>    colour .label     size
#> 1 #1A3855    3.0 2.662822
#> 2 #28557C    3.5 3.919819
#> 3 #3874A5    4.0 4.779645
#> 4 #4894D0    4.5 5.477285

Created on 2022-12-08 by the reprex package (v2.0.1)

@teunbrand teunbrand mentioned this pull request Jun 26, 2023
@teunbrand
Copy link
Collaborator Author

Closing this PR in favour of #5337.

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.

None yet

1 participant