Fix behaviour of combine_vars() with more than one facet variable #3302
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes #2810, which describes the unusual failure of
facet_wrap()
when used with layers containing different numbers of facet variables.This took a very long time to track down. In the end,
FacetWrap$compute_layout()
was generating a layout with 12 rows, when there are only four panels computed byFacetWrap$map_data()
. This resulted in someNULL
grobs due to index recycling, which are not valid but don't generate errors until render time (r-lib/gtable#80). This layout is computed bycombine_vars()
, which has incorrect handling when there are different numbers of facet variables in each layer's data.This PR fixes this behaviour (by wrapping the line that generates too many combinations with
unique()
) and adds tests to verify the existing behaviour ofcombine_vars()
. There some odd behaviour when using thedrop = FALSE
argument, notably that NAs are dropped (unless they are factor values), and that missing combinations of multiple facet variables are included (in addition to missing factor levels).This function could probably be rewritten to be clearer, but I didn't do this because I didn't want to break the existing behaviour.