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

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

Open
billdenney opened this issue Mar 24, 2024 · 1 comment

Comments

@billdenney
Copy link

billdenney commented Mar 24, 2024

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
Copy link
Author

My thoughts on an interface would be:

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

plot is the gg object.

  • If plot is not a gg object, raise an error.
  • If plot does not use facet_wrap_paginate() or facet_grid_paginate(), it would return NULL, equivalent to `n_pages().
    • Alternative thoughts could be:
      • If it uses facet_wrap() or facet_grid(), it could do the same extraction where page is always set to 1 in the data, and the columns are still extracted.
      • And, if it does not use facets, it could return data.frame(page = 1).
  • Check if one of the faceting variables is named page, and if so, choose a different name for the page column with a warning.
    • Alternatively, use the name .page so that it is very unlikely to have a collision, and if there is a collision, add more dots to the beginning of .page until there is no collision (so ..page would be tested next). This would also have a warning.

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

No branches or pull requests

1 participant