Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Allow parsing expressions in facet_wrap #656

Closed
wants to merge 3 commits into from

6 participants

Winston Chang Luciano Selzer Hadley Wickham Christopher Small tiflo Lionel Henry
Winston Chang
Collaborator
wch commented

Fixes #25.

This should have visual tests before it's merged.

Winston Chang
Collaborator
wch commented

Some useful examples:

m <- mpg
levels(m$drv) <- paste("Y[", 1:3, "]", sep="")
p <- ggplot(m, aes(x = displ, y = cty)) + geom_point()
p + facet_wrap(~drv, labeller = label_parsed)
p + facet_wrap(~drv+fl, labeller = label_parsed)
p + facet_wrap(~drv)
p + facet_wrap(~drv+fl)
p + facet_wrap(~drv+fl, labeller = label_both)
Luciano Selzer

That doesn't work with multiple factors because "," is not a possible plotmath expression. I fixed it but I'm not sure how submit it.

Winston Chang
Collaborator
wch commented

See here:
https://github.com/hadley/ggplot2/wiki/Developing-ggplot2-using-github

Although it may be a bit more complicated since you're making a modification of my branch, instead of Hadley's master branch. If that ends up being too much trouble, you can just cut/paste the patch here.

Winston Chang
Collaborator
wch commented

I think its better to use an expression list, which separates the expressions with commas - see my latest commit.

It will render the example you provided, with commas:

p + facet_wrap(~drv+fl, labeller = label_parsed)

It might be better to add a new labeler function to scales that makes a list automatically.

Winston Chang
Collaborator
wch commented

The problem is in train_position, where it loops over calls to scale_apply, for each layer. In scale_apply, it calls scale_train.discrete, which uses the drop parameter of the scale object.

I think the way to do this is to first loop over all the layers with scale_train, then do the drop afterward. But doing that correctly could be tricky.

Hadley Wickham
Owner

The problem with this approach is that it doesn't allow for other labeller functions. To really fix this we need a more flexible specification of labeller functions - they should take a matrix (or other richer data structure) as an argument. But this will mean that we need to rewrite the existing labeller functions and hence is an API breaking change, which means we need to push it off to a future version.

Christopher Small

This is making me sad... +1 for future version feature request.

Thanks!

tiflo

Same here. This doesn't seem to have been fixed. writing a (variable-specific) labeler function every time I'd like to relabel facets seems tedious. Sorry, if this has been fixed and I missed it. cheers.

Hadley Wickham
Owner

This sounds like a great feature, but unfortunately we don't currently have the development bandwidth to support it. If you'd like to submit a pull request that implements this feature, please follow the instructions in the development vignette.

Hadley Wickham hadley closed this
Lionel Henry

Hello,

I tried Winston's patch and it seems fully functional, on par with facet_grid's labeller.
Can I ask what was wrong? If there is a problem I'd like to try to solve it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 4, 2012
  1. Winston Chang

    Allow parsing expressions in facet_wrap

    wch authored
    Fixes #25
Commits on Sep 5, 2012
  1. Luciano Selzer Winston Chang

    Fixed bug with label_parsed and multiple facets factors

    lselzer authored wch committed
  2. Winston Chang
This page is out of date. Refresh to see the latest.
Showing with 18 additions and 5 deletions.
  1. +18 −5 R/facet-wrap.r
23 R/facet-wrap.r
View
@@ -52,7 +52,9 @@
#' p + geom_point(data = transform(cyl6, cyl = NULL), colour = "red") +
#' facet_wrap(~ cyl)
#' }
-facet_wrap <- function(facets, nrow = NULL, ncol = NULL, scales = "fixed", shrink = TRUE, as.table = TRUE, drop = TRUE) {
+facet_wrap <- function(facets, nrow = NULL, ncol = NULL, scales = "fixed",
+ shrink = TRUE, labeller = "label_value", as.table = TRUE, drop = TRUE) {
+
scales <- match.arg(scales, c("fixed", "free_x", "free_y", "free"))
free <- list(
x = any(scales %in% c("free_x", "free")),
@@ -62,11 +64,12 @@ facet_wrap <- function(facets, nrow = NULL, ncol = NULL, scales = "fixed", shrin
facet(
facets = as.quoted(facets), free = free, shrink = shrink,
as.table = as.table, drop = drop,
- ncol = ncol, nrow = nrow,
+ ncol = ncol, nrow = nrow, labeller = labeller,
subclass = "wrap"
)
}
+
#' @S3method facet_train_layout wrap
facet_train_layout.wrap <- function(facet, data) {
panels <- layout_wrap(data, facet$facets, facet$nrow, facet$ncol,
@@ -199,10 +202,20 @@ facet_panels.wrap <- function(facet, panel, coord, theme, geom_grobs) {
#' @S3method facet_strips wrap
facet_strips.wrap <- function(facet, panel, theme) {
- labels_df <- panel$layout[names(facet$facets)]
- labels_df[] <- llply(labels_df, format, justify = "none")
+ labeller <- match.fun(facet$labeller)
- labels <- apply(labels_df, 1, paste, collapse=", ")
+ labels_df <- panel$layout[names(facet$facets)]
+
+ # If faceting with multiple variables, paste them together
+ labels <- apply(labels_df, 1, paste, collapse = ", ")
+ if (identical(labeller, label_parsed)) {
+ labels <- paste("list(", labels, ")", sep = "")
+ }
+
+ varnames <- paste(names(labels_df), collapse=", ")
+
+ # Run the labeller function
+ labels <- labeller(varnames, labels)
list(t = llply(labels, ggstrip, theme = theme))
}
Something went wrong with that request. Please try again.