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)