1 Goal


The goal of this tutorial is to learn how to use the facet grid function on ggplot. The idea is to create a different plot for different values of a categorical variable.


2 Data preparation


# First we load the libraries
library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# In this tutorial we are going to use the Titanic dataset
data("Titanic")
Titanic <- as.data.frame(Titanic)
head(Titanic)
##   Class    Sex   Age Survived Freq
## 1   1st   Male Child       No    0
## 2   2nd   Male Child       No    0
## 3   3rd   Male Child       No   35
## 4  Crew   Male Child       No    0
## 5   1st Female Child       No    0
## 6   2nd Female Child       No    0

3 Facet grid

3.1 Simple plotting


# Let's check witch categorical variables we have in our dataset
str(Titanic)
## 'data.frame':    32 obs. of  5 variables:
##  $ Class   : Factor w/ 4 levels "1st","2nd","3rd",..: 1 2 3 4 1 2 3 4 1 2 ...
##  $ Sex     : Factor w/ 2 levels "Male","Female": 1 1 1 1 2 2 2 2 1 1 ...
##  $ Age     : Factor w/ 2 levels "Child","Adult": 1 1 1 1 1 1 1 1 2 2 ...
##  $ Survived: Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 1 1 1 1 ...
##  $ Freq    : num  0 0 35 0 0 0 17 0 118 154 ...
# We plot the frequency of each class

ggplot() + geom_col(data = Titanic, aes(x = Class, y = Freq))


3.2 Facet grid using one variable


# Facet grid takes the formula vertical ~ horizontal
# We plot using age as variable on vertical
ggplot() + geom_col(data = Titanic, aes(x = Class, y = Freq)) +
  facet_grid(Age ~ .)

# And horizontal, which in this case is more useful

ggplot() + geom_col(data = Titanic, aes(x = Class, y = Freq)) +
  facet_grid(. ~ Age)


3.3 Facet grid using two variables


# As we have seen facet grid takes the from vertical ~ horizontal
# Then we can define a second variable to create a two dimensional grid
# We add the sex as second variable in this case
ggplot() + geom_col(data = Titanic, aes(x = Class, y = Freq)) +
  facet_grid(Sex ~ Age)


3.4 Dodging columns


# We can add color and dodging in the plots 
# Check the geom_col tutorial for further explanation

ggplot() + geom_col(data = Titanic, aes(x = Class, y = Freq, fill = Survived), position = "dodge") +
    facet_grid(Sex ~ Age, scales = "free")


3.5 Switching the strips


# By default the strips with the variables used in facet grid appear on top and on the right
# We can flip the position of the strips 
# Switch takes the values x, y and both

# Switching x strip
ggplot() + geom_col(data = Titanic, aes(x = Class, y = Freq, fill = Survived), position = "dodge") +
    facet_grid(Sex ~ Age, switch = "x")

# Switching y strip
ggplot() + geom_col(data = Titanic, aes(x = Class, y = Freq, fill = Survived), position = "dodge") +
    facet_grid(Sex ~ Age, switch = "y")

# Switching both strips
ggplot() + geom_col(data = Titanic, aes(x = Class, y = Freq, fill = Survived), position = "dodge") +
    facet_grid(Sex ~ Age, switch = "both")


4 Conclusion


In this tutorial we have learnt how to use the facet grid and the different configurations we can use.