tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.39k stars 2k forks source link

Strip label accessor #5924

Open teunbrand opened 1 month ago

teunbrand commented 1 month ago

This PR aims to fix #4979 and conflicts with #5917.

Briefly, a new method is added to Facet to retrieve strip text after applying the labeller.

The new method is also used in FacetWrap/FacetGrid$draw_panels() to format the strip labels. As the labeller is already applied, the render_strips(labeller) argument has become vestigial. I haven't removed the argument as extensions might still use it.

Rendered example:

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

# Basic plot
p <- ggplot(mpg, aes(displ, hwy)) +
  geom_point()

get_strip_labels(p) # empty facets
#> NULL
get_strip_labels(p + facet_wrap(year ~ cyl))
#> $facets
#>   year cyl
#> 1 1999   4
#> 2 1999   6
#> 3 1999   8
#> 4 2008   4
#> 5 2008   5
#> 6 2008   6
#> 7 2008   8
get_strip_labels(p + facet_grid(year ~ cyl))
#> $cols
#>   cyl
#> 1   4
#> 2   5
#> 3   6
#> 4   8
#> 
#> $rows
#>   year
#> 1 1999
#> 2 2008

Created on 2024-05-31 with reprex v2.1.0