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
Implement labeller
option for facet_wrap()
#1127
Conversation
We are going to make other API breaking changes in the next release, so it would be ok to consider a less-hacky solution, if you're interested. |
I may have time in July. |
That should work - my crazy summer conference schedule starts on next Wednesday, and I won't be back doing much development until July 20. |
@hadley Would it be ok to have multiple factors (e.g., |
I'll add a parameter to the labellers to specify whether it should be one row or multiple rows. I think multiple rows is more readable but kind of ugly so it'll be nice to have the single row option for publications. Then we can either change the default of wrap to match that of grid. Or we can have the old behaviour by setting the default to this lambda: |
af81805
to
d82966d
Compare
@hadley ready for review. Currently Additionally, I think we should have a new default labeller for both wrap and grid. It would call |
bc4dcd5
to
e515b0c
Compare
ok, I added ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
facet_grid(gear ~ am + cyl) yields which is more readable because you can tell which values belong to which variable. Let me know if you'd prefer no changes in the visual defaults. |
If you look closely there is currently a visible thin line separating the rows of the labels with the default theme. And here is what happens when I think it's not so nice. I could implement the row separation as a newline |
label_both(labels, multi_line) | ||
} | ||
} | ||
class(label_default) <- c("function", "labeller") |
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 just class(label_default) <- "labeller")
(technically function isn't a class, but a type)
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.
@hadley when I remove the "function" class, roxygen stops documenting the usage of these functions, which leads to bad documentation and a CMD CHECK warning. I'll leave the function class for now and file an issue for roxygen.
I'd prefer to make no changes to the default display. I also wonder if it would make sense to document all the labeller functions in one Rd file, so there's a place to document how the labeller function works in more detail. |
Alright, I'll rename Does this also applies to wrap facetting? I.e., currently wrap facets display multiple factors on a single line while grid facets display them on multiple lines. In this PR, they are both displayed consistently as multiple lines but I could pass a lambda |
I think making that consistent is fine - it's fairly unusual to use multiple vars with |
4e9a9d6
to
e453999
Compare
@hadley The labellers are now documented together. Also the last commit proposes another API update for p + facet_grid(. ~ vs + am, labeller = label_bquote(.(x) ^ .(y))) we have p + facet_grid(. ~ vs + am, labeller = label_bquote(.(vs) ^ .(am))) For backward compatibility, it still bounds I'll cancel the commit if you prefer the old behaviour. |
This will break existing custom labellers, right? |
yes, before the labellers took a |
Can you make that more clear in the NEWS? |
#' @section Writing New Labeller Functions: | ||
#' | ||
#' A labeller function accepts a dataframe of labels (character | ||
#' vectors) containing one column for each factor. Muultiple factors |
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.
Muultiple typo
result <- values | ||
# If the facetting margin (i.e. `variable`) was not specified when | ||
# calling labeller, default to use the actual values. | ||
if (is.null(labeller)) { |
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.
This switch seems overly complicated to me. Why not just require that you use a "labeller function", i.e. a function that has arguments labels
and multiline
? (obviously you're just copying the existing structure, but that now seems overly complex)
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.
This is a legacy from before. We could remove the ability to use non-labeller functions that take and return a chr vector if you prefer?
Before the labeller function was checking the arguments to determine which functions are labellers, now I use the S3 class because I thought it was more robust. I can make it check the arguments as before if you think that'd be easier to create custom labellers. But we can't rely on multi_line
, only on 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.
Ah, I guess it's documented and you've already done it, so lets leave it.
Overall, this looks good. I like the unification across wrap and grid, and the code is not much more complicated than previously. Any thoughts on unit testing? |
Can you please merge/rebase and squash and then I'll merge? |
Here you go |
And squash please? |
hmm I can squash into Winston's commit (which is the earliest one) but then he gets credited as the author of the whole patch. What's the proper way to do it? |
Oh I see - don't worry about it then. |
Implement `labeller` option for facet_wrap()
Fixes a regression introduced in PR tidyverse#1127.
Fixes a regression introduced in PR tidyverse#1127.
Fixes a regression introduced in PR tidyverse#1127.
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/ |
so I finally understood what was the concern in #656. I found a somewhat hacky solution (documented in the source code) that should nevertheless work in most cases.