2

How can I change the (all) label given by facet_grid to the strip text if margins = ...?

Here is an example:

ggplot(mtcars, aes(mpg, wt)) + geom_point() +
    facet_grid(am ~ cyl, margins = "cyl")

How can I make (all) say FOO

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • [Here](http://stackoverflow.com/questions/2893005/how-to-change-strip-text-labels-in-ggplot-with-facet-and-margin-true) and [here](http://stackoverflow.com/questions/3472980/ggplot-how-to-change-facet-labels) are solutions that could help. – Didzis Elferts Oct 14 '13 at 15:09
  • This "(all)" labels seems to be hard-coded, and not even in ggplot2 but in `reshape2::add_margins`. It might be worth opening an issue in github, if it's not already been done. – baptiste Oct 14 '13 at 15:15
  • I'm voting to close as this is a duplicate but I believe that title is terrible and didn't yield a hit for me. – Tyler Rinker Oct 14 '13 at 17:21

1 Answers1

4

You can use the labeller parameter of facet_grid(). This is a function that takes two arguments, the variable and value. You can define your own:

facet_labels <- function(variable, value) {
  labels <- as.character(value)
  labels[labels == '(all)'] <- 'FOO'
  return (labels)
}

ggplot(mtcars, aes(mpg, wt)) + geom_point() +
  facet_grid(am ~ cyl, margins = "cyl", labeller = facet_labels)
Ciarán Tobin
  • 7,306
  • 1
  • 29
  • 45