-
Notifications
You must be signed in to change notification settings - Fork 2.1k
allow empty facet specs #3162
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
allow empty facet specs #3162
Conversation
I think this is good in principle. Not entirely sure about the |
Oh, nice idea. Thanks. But, we cannot use an empty string as a variable name, so it seems I need some tweaks. list("" = "")
#> Error: attempt to use zero-length variable name |
And the label and the variable name should be the same, I assume. So maybe |
I forgot |
Sounds good to me. |
Two small corrections:
We can, and it seems to work with the current code of ggplot2. l <- setNames(list(""), "")
names(l)
#> [1] "" Created on 2019-02-25 by the reprex package (v0.2.1.9000)
Anyway I'll use |
Is this a typo of Lines 278 to 280 in 9df0a07
|
@lionel- @thomasp85 I think the proposal about empty facet specs is almost fixed, though I need to add more test cases before asking for revewing. I'd appreciate if you give some feedbacks and concerns :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The approach looks good to me!
R/facet-grid-.r
Outdated
free = free, space_free = space_free, labeller = labeller, | ||
as.table = as.table, switch = switch, drop = drop) | ||
) | ||
} | ||
|
||
# returns a list containing exactly two quosures `rows` and `cols` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here it's not clear whether you're mentioning two single quosure or two quosure lists. I would use "quosure lists" or "lists of quosures" instead of just the plural "quosures".
Also we generally capitalise comments. No full stop, unless there are more than one sentence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Actually I'm struggling about the wording here...
Also we generally capitalise comments. No full stop, unless there are more than one sentence.
I see.
Note that, I gave up testing strip labels ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
R/facet-.r
Outdated
compact_facets <- function(x) { | ||
x <- rlang::flatten_if(x, rlang::is_list) | ||
null <- vapply(x, rlang::quo_is_null, logical(1)) | ||
rlang::as_quosures(x[!null]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be new_quosures()
. The casting form converts formulas to quosures etc, and at this point we should have standardised the inputs already.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. Thanks!
@thomasp85 Do you have any comments? Since this PR is mostly about facet specs and I don't think the change is big, I expect this doesn't get in the way of #3062. But, if you have any concerns, please tell me. If there's no major concern, I'll merge. |
Will merge this after the release of 3.2. |
Thanks! |
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/ |
Fix #3070, fix #2986.
Summary
This PR compacts facet specs automatically (#2986) and allow empty specs (#3070).
I admit this makes the implementation of
FacetGrid
andFacetWrap
a bit complicated in that it needs some special treatments for the case of 0-variable. But, on the other hand, accepting empty specs makes the process of building facet specs simpler and clearer. So, I believe this change is worth making.Major changes (visible to users)
NULL
, treat it as an empty quosures object (quos()
).NULL
s in it.facet_wrap()
generates a single plot with a strip. A string(all)
is used for the strip label.facet_grid()
generates a single plot without a strip.Internal changes
To make the major changes, I want to define the internal facet specs more strictly as following:
FacetWrap
requires a quosures object (e.g.quos(foo, bar)
)FacetGrid
requires a list of two quosures objects,rows
andcols
(e.g.list(rows = quos(foo), cols = quos(bar))
)Accordingly, the corresponding functions below return these specs.
grid_as_facet_list()
wrap_as_facet_list()
(new)Under these functions,
as_facet_list()
is responsible for translating a spec (e.g. a quosures object, a formula, and a character) to a list of quosures. It's also responsible for the basic validation (e.g. raise an error when the spec is anuneval
object), so the upper functions should always use this for the input even it's just a simple quosures object.Note that the results of
as_facet_list()
are not compacted; compacting these specs is basically the role of the upper functions,grid_as_facet_list()
andwrap_as_facet_list()
. For convenience, a new internal functioncompact_facets()
can be used as a short cut of flattening and compacting the spec.Minor changes
facet_wrap()
andfacet_grid()
now properly rejectsaes()
Example usages
Created on 2019-03-03 by the reprex package (v0.2.1)