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 numbering columns in facet_grid #1

Open
jimjam-slam opened this issue Jul 21, 2018 · 1 comment
Open

Facet numbering columns in facet_grid #1

jimjam-slam opened this issue Jul 21, 2018 · 1 comment
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@jimjam-slam
Copy link
Owner

jimjam-slam commented Jul 21, 2018

Facet numbering is fairly easy with facet_wrap: each facet gets its own label and each label only covers one facet, so you can just assign numbers to each row in the incoming data frame, labels, and then parse them with glue.

It's not so easy with facet_grid: there are labels for the margins, not individual facets. The labeller gets called separately for each margin; when it does, you get the following attributes on labels:

  • facet: this is "grid" for facet_grid;
  • type: either rows or cols depending on which margin is being worked,
  • row.names: each row is equal to the first cell in the row or column, as far as I can tell.

Here's the problem: if you're processing the row margins, the gaps in the row.names tell you how many columns there are, so you can work out in turn the values of all the cells in the row (assuming that there are no dropped levels! I think this is a somewhat reasonable assumption for facet_grid, though).

But if you're processing the column margins, all you can see is the first row. There's no way to infer how many rows there are, and you therefore can't work out the values of all the cells in the column.

One solution I'd considered is to use the fact that label_glue is a closure to store state between label_glue_inner calls. If the row margins were done first, this info could be passed on to the column margins. And since each call of label_glue has its own state, this shouldn't present a problem if you then go and use another call of it on a different plot (or the same plot again).

But in every test I've done, the column margins are done first, and I don't know if this can be changed. This appears to happen in ggplot2::render_strips. I'll file an issue and see if it can be changed. A less destructive change to ggplot2 might simply be to pass more metadata on to the labeller so that this stuff can be worked out without storing state.

@jimjam-slam
Copy link
Owner Author

Based on the issue I've referenced, I think it's unlikely that facet numbering will work with facet_grid, since there isn't enough information given to the labeller in most cases to infer which panels belong to each column or row. I also understand the argument that a 2-dimensional faceting spec should probably be referenced by a 2-dimensional labelling spec, although I suspect that a lot of journals wouldn't accept that.

I believe that label_glue works with facet_grid if you supply specs for the rows and columns separately, like:

facet_grid(
  width ~ height,
  labeller = labeller(
    rows = label_glue('{.n}. Width: {width}'),
    cols = label_glue('{.L}. height: {height}')))

But I'll verify that before I update the README.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant