Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Allow parsing expressions in facet_wrap #656

wants to merge 3 commits into from

6 participants

wch commented

Fixes #25.

This should have visual tests before it's merged.

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)

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.

wch commented

See here:

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.

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.

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.


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.


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



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.


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 hadley closed this


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.


@hadley what was the problem with this labeller exactly? last time I tried it it seemed to work fine. If it works in most situations, maybe it'd still be nice to have it with informative error messages when it doesn't work.


@lionel- I don't remember. If you want to do a fresh PR now is a good time.

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 with 18 additions and 5 deletions.
  1. +18 −5 R/facet-wrap.r
23 R/facet-wrap.r
@@ -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
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 <-$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.