I'm not sure how to do this with a labeller function, but another option is to create a grouping variable that combines all three of your categorical variables into a single variable that can be used for faceting. Here's an example using the built-in mtcars
data frame and the dplyr
package for creating the new grouping variable on the fly. Following that is an update with a function that allows dynamic choice of from one to three faceting variables.
library(dplyr)
ggplot(mtcars %>% mutate(group = paste(cyl,am,vs, sep="-")),
aes(wt,mpg)) +
geom_point() +
facet_wrap(~group)
UPDATE: Regarding the comment about flexibility, the code below is a function that allows the user to enter the desired data frame and variable names, including dynamically choosing to facet on one, two, or three columns.
library(dplyr)
library(lazyeval)
mygg = function(dat, v1, v2, f1, f2=NA, f3=NA) {
dat = dat %>%
mutate_(group =
if (is.na(f2)) {
f1
} else if (is.na(f3)) {
interp(~paste(f1,f2, sep='-'), f1=as.name(f1), f2=as.name(f2))
} else {
interp(~paste(f1,f2,f3,sep='-'), f1=as.name(f1), f2=as.name(f2), f3=as.name(f3))
})
ggplot(dat, aes_string(v1,v2)) +
geom_point() +
facet_wrap(~group)
}
Now let's try out the function:
library(vcd) # For Arthitis data frame
mygg(Arthritis, "ID","Age","Sex","Treatment","Improved")
mygg(mtcars, "wt","mpg","cyl","am")
mygg(iris, "Petal.Width","Petal.Length","Species")