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

Facet strip rendering order and metadata #2769

Closed
jimjam-slam opened this issue Jul 21, 2018 · 6 comments
Closed

Facet strip rendering order and metadata #2769

jimjam-slam opened this issue Jul 21, 2018 · 6 comments

Comments

@jimjam-slam
Copy link

I created a small package to help with facet labelling, and one of the neat things it can do is number or letter facets created with facet_wrap, so that you can refer to them individually in papers and reports.

I'd love to do the same for facet_grid (for example, have labels prefixed with "(a–c)", "(d–f)", etc), but there's a difficult limitation here that I've outlined here. Essentially, the labelling function doesn't get enough information to make this possible with facet_grid, as far as I can tell.

One solution would be to change the order in which facet strips are rendered in ggplot2::render_strips. It currently looks like this:

render_strips <- function(x = NULL, y = NULL, labeller, theme) {
  list(
    x = build_strip(x, labeller, theme, TRUE),
    y = build_strip(y, labeller, theme, FALSE)
  )
}

If rows were done first instead, I could take advantage of the fact that my labelling function is a closure and store the information learned about the number of rows in the outer function, so that the inner function could access it later when doing columns. If you just flipped the two lines around, that would probably be a destructive change, but you could easily call y, then x, then put them in a list.

Another, potentially less destructive, solution would be to simply attach more metadata to the data frame given to the labelling function. If the data frame had a list attached, where each element was a numeric vector of the facet numbers, this would be pretty trivial to implement. It would also catch the corner case where facet levels are dropped in facet_grid due to lack of data (the other solution would have to assume that all facets were present).

Would either of these changes to ggplot2 be feasible? Happy to submit a PR myself if it's an acceptable change.

@clauswilke
Copy link
Member

Irrespective of any implementation issues, I don’t entirely understand how this is meant to work for facet_grid(). For facet_wrap(), you get one strip per facet, so it makes sense to number them. But for facet_grid(), interior facets don’t have strips. So how would you number them? One option I can envision is like in a map, where you use letters for columns and numbers for rows (or vice versa), so facets are uniquely specified by the letter-number combination. But that would be possible with the current code, wouldn’t it?

@jimjam-slam
Copy link
Author

I was thinking something like... let's say we have a 3x3 facet layout:

----------------------------
|   a.   |   b.   |   c.   |
|   d.   |   e.   |   f.   |
|   g.   |   h.   |   i.   |
----------------------------

The column facet labels could be prefixed "(a, d, g)", "(b, e, h)", "(c, f, i)" and the row labels could be prefixed "(a–c)", "(d–f)", "(g–i)". Although, now that I think about it, that might not make sense to the reader if the individual facets aren't also labelled. This might require some more thought on my part 😆

@clauswilke
Copy link
Member

In my mind, a grid is a two-dimensional display and needs two-dimensional labeling, as I described. You could start with that approach and see where it goes.

@jimjam-slam
Copy link
Author

I think you might be right. Thanks for lending me your thoughts, @clauswilke!

@clauswilke
Copy link
Member

I'm closing this issue. Please open a new one if 2D labeling requires changes to the faceting code.

@lock
Copy link

lock bot commented Jan 17, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Jan 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants