-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Allow faceting by expressions in addition to column names #1722
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
Conversation
Would this allow to use interaction of factors as facets? I often want to do things like library("ggplot2")
ggplot(diamonds) + geom_point(aes(x=depth, y=table)) + facet_wrap(~color:clarity))
ggplot(diamonds) + geom_point(aes(x=depth, y=table)) + facet_wrap(~interaction(color, clarity)) instead of ggplot(diamonds) + geom_point(aes(x=depth, y=table)) + facet_wrap(~color+clarity)) to avoid stacking the two facet labels and save space. The first two do not work, I have to compute the interaction beforehand, put it in a new column and then call ggplot. |
@DanRuderman The environment really needs to come from the plot environment, which means it'll need to be threaded down through as extra arguments to these calls. Do you want to have a go at doing that? @jiho yes! |
@hadley Yes. I look forward to the learning experience. Question is, do you look forward to the teaching experience? ;-) |
OK - I think I got the plot's environment passed around correctly. It would be great to have some tough test cases to make sure it's all working. Would you be able to provide some? I can work on documentation in the next week or so. Am I using the correct version of Roxygen? (5.0.1.9000) |
|
Cool! That would be great. In addition to making things like the above easier, it also makes the syntax more consistent between |
@DanRuderman You'll need a test case like this: df <- data.frame(x = 1:5, y = 1:5, z = 1)
f <- function() {
g <- function(x) x + 2
ggplot(df, aes(x, y)) +
geom_point() +
facet_grid(. ~ g(x))
}
f() |
@@ -219,4 +219,4 @@ Collate: | |||
'zxx.r' | |||
'zzz.r' | |||
VignetteBuilder: knitr | |||
RoxygenNote: 5.0.1 |
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.
Could you use released roxygen please?
@thomasp85 you should be aware of this since it touches the facetting code Now that you've implemented this, I do wonder if it might be better to make a clean switch to using lazyeval for the facetting specification. That will require more changes to how facets are created, but will make the rest of the code simpler because the environment is carried around in the formula. I'm also not super confident about Let me take a bit of a look. |
I'm watching (and commented on the issue about the facet rewrite) I would advice against merging this as it touches code that has been completely rewritten and it would thus lead to a lot of merge conflicts. Even so, the end result of this PR will make it much easier for me to implement it in the new system... |
To convert to lazyeval will require a decent amount of work because @DanRuderman I think that means we probably won't merge this PR, but I really appreciate you showing how easy it is in principle! |
Yes - it is much appreciated and I'll make sure that you'll be mentioned in the NEWS :-) |
@hadley I can give you a heads up when the rewrite is in a state where a conversion to lazyeval might make sense and we can discuss then |
@hadley Sounds good. For the moment I'll tidy things up according to your above recommendations. |
This PR has now been ported to #1633 - Thanks @DanRuderman |
@thomasp85 Bravo! |
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/ |
@hadley I made some simple modifications to the environments that are passed to
quoted_df
that seemed to make expressions work usingI().
I haven't done any of the documenting because I'd like you to have a look first and see what I have overlooked (can't believe it would be this easy...).Example:
ggplot(mtcars) + stat_bin(aes(mpg)) + facet_grid(I(gear+1) ~ I(2*carb))