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

Extracting guide data (version 3) #5506

Merged
merged 8 commits into from
Dec 15, 2023
Merged

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #5004 and replaces #5337.

Briefly, it defines the new guide_data() function to programatically access values and labels of axes and legends. More motivation is described in #5096.

Because guides are now build in ggplot_build(), we can extract the keys more cleanly than #5337. That this PR's footprint more focussed and minimal.

Examples (same as #5337):

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2
#> Warning: package 'testthat' was built under R version 4.3.1

# 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)
#>           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")
#>       size .value .label
#> 1 2.662822    3.0    3.0
#> 2 3.919819    3.5    3.5
#> 3 4.779645    4.0    4.0
#> 4 5.477285    4.5    4.5

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

Created on 2023-11-03 with reprex v2.0.2

@teunbrand teunbrand mentioned this pull request Nov 3, 2023
Merge branch 'main' into guide_data3

# Conflicts:
#	NAMESPACE
@teunbrand teunbrand added this to the ggplot2 3.5.0 milestone Dec 14, 2023
@teunbrand
Copy link
Collaborator Author

teunbrand commented Dec 14, 2023

I've renamed the function to get_guide_data() to prevent the confusion that guide_data() might be a guide that can be fed to a scale or guides(): it is not.

R/guides-.R Outdated
Comment on lines 756 to 757
#' @param i,j An integer giving a row (i) and column (j) number of a facet for
#' which to return position guide information.
Copy link
Member

Choose a reason for hiding this comment

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

How does this play with facets from extension packages that might not adhere to a strict grid of panels?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Depends on how the facets handle the layout I suppose. From my own package, facet_manual() has .LEFT, .RIGHT, .TOP and .BOTTOM instead of ROW and COL, so it won't work.

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2
library(ggh4x)
#> 
#> Attaching package: 'ggh4x'
#> 
#> The following object is masked from 'package:ggplot2':
#> 
#>     guide_axis_logticks

ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  facet_manual(
    ~ cyl, design = matrix(c(1,1,NA,NA,NA,2,2,NA,NA,NA,3,3), nrow = 4)
  )

get_guide_data(last_plot(), aesthetic = "x", i = 1, j = 1)
#> NULL

Created on 2023-12-14 with reprex v2.0.2

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should just allow a single index to not force a tabular layout. I know that would require some knowledge about how the facet lays out the panels but this is not something for the standard user anyway

Copy link
Member

Choose a reason for hiding this comment

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

basically use [[ indexing rather than [

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I like that idea, and match PANEL instead of ROW/COL. I think PANEL should be used in most facet extensions, I'd think.

@teunbrand
Copy link
Collaborator Author

I'm assuming that Thomas' approval still holds after implementing his suggestions, so I'll go ahead and merge this in.

@teunbrand teunbrand merged commit 24dfd6e into tidyverse:main Dec 15, 2023
12 checks passed
@teunbrand teunbrand deleted the guide_data3 branch December 15, 2023 09:17
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.

Consider adding x and y tick labels columns to layer_data()
2 participants