Facets (ggplot2)

Problem

You want to do split up your data by one or more variables and plot the subsets of data together.

Solution

Sample data

We will use the tips dataset from the reshape2 package.

  1. library(reshape2)
  2. # Look at first few rows
  3. head(tips)
  4. #> total_bill tip sex smoker day time size
  5. #> 1 16.99 1.01 Female No Sun Dinner 2
  6. #> 2 10.34 1.66 Male No Sun Dinner 3
  7. #> 3 21.01 3.50 Male No Sun Dinner 3
  8. #> 4 23.68 3.31 Male No Sun Dinner 2
  9. #> 5 24.59 3.61 Female No Sun Dinner 4
  10. #> 6 25.29 4.71 Male No Sun Dinner 4

This is a scatterplot of the tip percentage by total bill size.

  1. library(ggplot2)
  2. sp <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1)
  3. sp

plot of chunk unnamed-chunk-3

facet_grid

The data can be split up by one or two variables that vary on the horizontal and/or vertical direction.

This is done by giving a formula to facet_grid(), of the form vertical ~ horizontal.

  1. # Divide by levels of "sex", in the vertical direction
  2. sp + facet_grid(sex ~ .)

plot of chunk unnamed-chunk-4

  1. # Divide by levels of "sex", in the horizontal direction
  2. sp + facet_grid(. ~ sex)

plot of chunk unnamed-chunk-5

  1. # Divide with "sex" vertical, "day" horizontal
  2. sp + facet_grid(sex ~ day)

plot of chunk unnamed-chunk-6

facet_wrap

Instead of faceting with a variable in the horizontal or vertical direction, facets can be placed next to each other, wrapping with a certain number of columns or rows. The label for each plot will be at the top of the plot.

  1. # Divide by day, going horizontally and wrapping with 2 columns
  2. sp + facet_wrap( ~ day, ncol=2)

plot of chunk unnamed-chunk-7

Modifying facet label appearance

  1. sp + facet_grid(sex ~ day) +
  2. theme(strip.text.x = element_text(size=8, angle=75),
  3. strip.text.y = element_text(size=12, face="bold"),
  4. strip.background = element_rect(colour="red", fill="#CCCCFF"))

plot of chunk unnamed-chunk-8

Modifying facet label text

There are a few different ways of modifying facet labels. The simplest way is to provide a named vector that maps original names to new names. To map the levels of sex from Female==>Women, and Male==>Men:

  1. labels <- c(Female = "Women", Male = "Men")
  2. sp + facet_grid(. ~ sex, labeller=labeller(sex = labels))

Another way is to modify the data frame so that the data contains the desired labels:

  1. tips2 <- tips
  2. levels(tips2$sex)[levels(tips2$sex)=="Female"] <- "Women"
  3. levels(tips2$sex)[levels(tips2$sex)=="Male"] <- "Men"
  4. head(tips2, 3)
  5. #> total_bill tip sex smoker day time size
  6. #> 1 16.99 1.01 Women No Sun Dinner 2
  7. #> 2 10.34 1.66 Men No Sun Dinner 3
  8. #> 3 21.01 3.50 Men No Sun Dinner 3
  9. # Both of these will give the same output:
  10. sp2 <- ggplot(tips2, aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1)
  11. sp2 + facet_grid(. ~ sex)

Both of these will give the same result:

plot of chunk unnamed-chunk-11

labeller() can use any function that takes a character vector as input and returns a character vector as output. For example, the capitalize function from the Hmisc package will capitalize the first letters of strings. We can also define our own custom functions, like this one, which reverses strings:

  1. # Reverse each strings in a character vector
  2. reverse <- function(strings) {
  3. strings <- strsplit(strings, "")
  4. vapply(strings, function(x) {
  5. paste(rev(x), collapse = "")
  6. }, FUN.VALUE = character(1))
  7. }
  8. sp + facet_grid(. ~ sex, labeller=labeller(sex = reverse))

plot of chunk unnamed-chunk-12

Free scales

Normally, the axis scales on each graph are fixed, which means that they have the same size and range. They can be made independent, by setting scales to free, free_x, or free_y.

  1. # A histogram of bill sizes
  2. hp <- ggplot(tips, aes(x=total_bill)) + geom_histogram(binwidth=2,colour="white")
  3. # Histogram of total_bill, divided by sex and smoker
  4. hp + facet_grid(sex ~ smoker)
  5. # Same as above, with scales="free_y"
  6. hp + facet_grid(sex ~ smoker, scales="free_y")
  7. # With panels that have the same scaling, but different range (and therefore different physical sizes)
  8. hp + facet_grid(sex ~ smoker, scales="free", space="free")

plot of chunk unnamed-chunk-13plot of chunk unnamed-chunk-13plot of chunk unnamed-chunk-13