thomasp85 / ggforce

Accelerating ggplot2
https://ggforce.data-imaginist.com
Other
916 stars 106 forks source link

Question: Is there a way to determine which facets from the data are on which page with `facet_*_paginate` #322

Open billdenney opened 6 months ago

billdenney commented 6 months ago

Is there any way to extract the facet to page mapping when using facet_wrap_paginate() and facet_grid_paginate()? I'm hoping that there is a function like paginate_data_map() (which I know doesn't exist) that would extract the information of which facet is on which page similar to the example below:

library(tidyverse)
library(ggforce)

mtcars_double <-
  mtcars |>
  crossing(double = 1:2) |>
  mutate(
    cyl = double*cyl
  )

ggplot(mtcars_double, aes(x = disp, y = hp)) +
  geom_point() +
  facet_wrap_paginate(~cyl, nrow = 2, ncol = 2)


# desired output would be a data.frame or tibble that looks something like the
# following with one column for the page and more columns for each of the
data.frame(
  page = c(1, 1, 1, 1, 2),
  cyl = c(4, 6, 8, 12, 16)
)
#>   page cyl
#> 1    1   4
#> 2    1   6
#> 3    1   8
#> 4    1  12
#> 5    2  16

Created on 2024-03-24 with reprex v2.1.0

If it doesn't exist and would be of interest, I could try to make a PR for it with guidance about where to find the information within the gg object.

FYI, this is for use in the ggtibble package so that I can automatically expand the pages into a gglist and ggtibble object there.

billdenney commented 6 months ago

My thoughts on an interface would be:

paginate_data_map <- function(plot) {
###
}

plot is the gg object.