ggplot2 is a data visualization package for the statistical programming language R. Created by Hadley Wickham in 2005, ggplot2 is an implementation of Leland Wilkinson's Grammar of Graphics—a general scheme for data visualization which breaks up graphs into semantic components such as scales and layers.
Published on April 03, 2021
ggplot2 facet panel plots
3 min READ
Subplot creates a figure and a grid of subplots with a single call, while providing reasonable control over how the individual plots are created. In this markdown I’m going to introduce you to the panel plotting system in R.
# loading neccessary packages
library(datasets)
library(ggplot2)
As you all may know one can plots functions using the base R plotting system. One of the major functions we use in doing the task is the par function.
We can check more about the par function using ?
?par
Amoung all of the arguments the one that is useful for us is the mfrow
. Let’s use it and create a simple bi-panel plot.
## loading the cars data
library(datasets)
attach(mtcars)
par(mfrow=c(2,1), mar=c(2,8,2,4))
plot(wt,mpg, main="Scatterplot of Weight vs. Miles per Gallon")
plot(wt,disp, main="Scatterplot of Weight vs Displacement")
par(mfrow=c(1,2), mar=c(4,1,2,2))
hist(wt, main="Histogram of wt", xlab="weight(lbs)")
boxplot(wt, main="Boxplot of wt", xlab="weight(lbs)")
With the par( ) function, you can include the option mfrow=c(nrows, ncols) to create a matrix of nrows x ncols plots that are filled in by row. mfcol=c(nrows, ncols) fills in the matrix by columns.
The layout( ) function has the form layout(mat) where mat is a matrix object specifying the location of the N figures to plot. simple plot could be like this.
layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE))
hist(wt)
hist(mpg)
hist(disp)
With all this I’m leaving it to you to experiment with base R systems and come up with some special plots. One example could be
par(fig=c(0, .8, 0, .8), new=TRUE)
plot(mtcars$wt, mtcars$mpg, xlab="Car Weight", ylab="Miles per Gallon")
par(fig=c(0, 1, .55, 1), new=TRUE)
boxplot(mtcars$wt, horizontal=TRUE, axes=FALSE)
par(fig=c(0.65,1,0,0.8),new=TRUE)
boxplot(mtcars$mpg, axes=FALSE)
mtext("Enhanced Scatterplot", side=3, outer=TRUE, line=-3)
The facet approach divides a plot into a m x n sub-panels where each panel shows a different subset of the data.
There are two main functions for faceting : - facet_grid() - facet_wrap()
Let’s first create a basic plot using ggplot2
attach(ToothGrowth)
ToothGrowth$dose <- as.factor(ToothGrowth$dose) # making sure it is categorical
g <- ggplot(ToothGrowth , aes(x=dose, y=len, group=dose)) # storing for future use
bxplt <- g + geom_boxplot(aes(fill=dose))
bxplt
Now lets create 2 panels using the facet grid
bxplt + facet_grid(supp ~ .)
bxplt + facet_grid(. ~ supp)
Adding another variable to facet
bxplt + facet_grid(supp ~ dose)
bxplt + facet_grid(dose ~ supp)
By default, all the panels have the same scales (scales=“fixed”). They can be made independent, by setting scales to free, free_x, or free_y.
bxplt + facet_grid(dose ~ supp, scales='free')
The argument labeller can be used to control the labels of the panels
bxplt + facet_grid(supp ~ dose, labeller=label_both)
Now we will use the facet_wrap function to create the plots
bxplt + facet_wrap(~ dose, ncol=3)
bxplt + facet_wrap(~ supp, nrow=2)
As we can see above, facet_wrap returns a symmetrical matrix of plots for the number of levels of variable .
facet_grid is most useful when you have two discrete variables, and all combinations of the variables exist in the data whereas facet-wrap is used when a single variable with many levels and want to arrange the plots in a more space efficient manner.
I will end this with this plot that I made a few days back when I was working with the famous titanic data.
you can find out the process and more such plots here: Exploring the Training data