rggplot2symbolsfacetplotmath

How to change facet labels?


I have used the following ggplot command:

ggplot(survey, aes(x = age)) + stat_bin(aes(n = nrow(h3), y = ..count.. / n), binwidth = 10)
  + scale_y_continuous(formatter = "percent", breaks = c(0, 0.1, 0.2))
  + facet_grid(hospital ~ .)
  + theme(panel.background = theme_blank())

to produce

alt text

I'd like to change the facet labels, however, to something shorter (like Hosp 1, Hosp 2...) because they are too long now and look cramped (increasing the height of the graph is not an option, it would take too much space in the document). I looked at the facet_grid help page but cannot figure out how.


Solution

  • Change the underlying factor level names with something like:

    # Using the Iris data
    > i <- iris
    > levels(i$Species)
    [1] "setosa"     "versicolor" "virginica" 
    > levels(i$Species) <- c("S", "Ve", "Vi")
    > ggplot(i, aes(Petal.Length)) + stat_bin() + facet_grid(Species ~ .)