Skip to content
This repository

Allow parsing expressions in facet_wrap #656

Closed
wants to merge 3 commits into from

5 participants

Winston Chang Luciano Selzer Hadley Wickham Christopher Small tiflo
Winston Chang
Collaborator

Fixes #25.

This should have visual tests before it's merged.

Winston Chang
Collaborator

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

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

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

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 February 24, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 18 additions and 5 deletions. Show diff stats Hide diff stats

  1. 23  R/facet-wrap.r
23  R/facet-wrap.r
@@ -52,7 +52,9 @@
52 52
 #' p + geom_point(data = transform(cyl6, cyl = NULL), colour = "red") + 
53 53
 #'   facet_wrap(~ cyl)
54 54
 #' }
55  
-facet_wrap <- function(facets, nrow = NULL, ncol = NULL, scales = "fixed", shrink = TRUE, as.table = TRUE, drop = TRUE) {
  55
+facet_wrap <- function(facets, nrow = NULL, ncol = NULL, scales = "fixed",
  56
+  shrink = TRUE, labeller = "label_value", as.table = TRUE, drop = TRUE) {
  57
+
56 58
   scales <- match.arg(scales, c("fixed", "free_x", "free_y", "free"))
57 59
   free <- list(
58 60
     x = any(scales %in% c("free_x", "free")),
@@ -62,11 +64,12 @@ facet_wrap <- function(facets, nrow = NULL, ncol = NULL, scales = "fixed", shrin
62 64
   facet(
63 65
     facets = as.quoted(facets), free = free, shrink = shrink,
64 66
     as.table = as.table, drop = drop,
65  
-    ncol = ncol, nrow = nrow, 
  67
+    ncol = ncol, nrow = nrow, labeller = labeller,
66 68
     subclass = "wrap"
67 69
   )
68 70
 }
69 71
 
  72
+
70 73
 #' @S3method facet_train_layout wrap
71 74
 facet_train_layout.wrap <- function(facet, data) { 
72 75
   panels <- layout_wrap(data, facet$facets, facet$nrow, facet$ncol,
@@ -199,10 +202,20 @@ facet_panels.wrap <- function(facet, panel, coord, theme, geom_grobs) {
199 202
 
200 203
 #' @S3method facet_strips wrap
201 204
 facet_strips.wrap <- function(facet, panel, theme) {
202  
-  labels_df <- panel$layout[names(facet$facets)]
203  
-  labels_df[] <- llply(labels_df, format, justify = "none")
  205
+  labeller <- match.fun(facet$labeller)
204 206
   
205  
-  labels <- apply(labels_df, 1, paste, collapse=", ")
  207
+  labels_df <- panel$layout[names(facet$facets)]
  208
+
  209
+  # If faceting with multiple variables, paste them together
  210
+  labels <- apply(labels_df, 1, paste, collapse = ", ")
  211
+  if (identical(labeller, label_parsed)) {
  212
+    labels <- paste("list(", labels, ")", sep = "")
  213
+  }
  214
+
  215
+  varnames <- paste(names(labels_df), collapse=", ")
  216
+
  217
+  # Run the labeller function
  218
+  labels <- labeller(varnames, labels)
206 219
 
207 220
   list(t = llply(labels, ggstrip, theme = theme))
208 221
 }
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.