- Overview of Data Visualization
- When to Use Bar Chart, Column Chart, and Area Chart
- What is Line Chart and When to Use It
- What are Pie Chart and Donut Chart and When to Use Them
- How to Read Scatter Chart and Bubble Chart
- What is a Box Plot and How to Read It
- Understanding Japanese Candlestick Charts and OHLC Charts
- Understanding Treemap, Heatmap and Other Map Charts
- Visualization in Data Science
- Graphic Systems in R
- Accessing Built-in Datasets in R
- How to Create a Scatter Plot in R
- Create a Scatter Plot in R with Multiple Groups
- Creating a Bar Chart in R
- Creating a Line Chart in R
- Plotting Multiple Datasets on One Chart in R
- Adding Details and Features to R Plots
- Introduction to ggplot2
- Grammar of Graphics in ggplot
- Data Import and Basic Manipulation in R - German Credit Dataset
- Create ggplot Graph with German Credit Data in R
- Splitting Plots with Facets in ggplots
- ggplot2 - Chart Aesthetics and Position Adjustments in R
- Creating a Line Chart in ggplot 2 in R
- Add a Statistical Layer on Line Chart in ggplot2
- stat_summary for Statistical Summary in ggplot2 R
- Facets for ggplot2 Charts in R (Faceting Layer)
- Coordinates in ggplot2 in R
- Changing Themes (Look and Feel) in ggplot2 in R
Facets for ggplot2 Charts in R (Faceting Layer)
Faceting is a very useful visualization technique that allows you to create multiple versions of the same plot with multiple subsets of the data. This is also called small multiples. ggplot2 allows us to small multiples in two ways: facet_wrap()
and facet_grid()
. The default is facet_null()
which produces a single plot.
We will use German Credit dataset to demonstrate faceting with both these functions.
facet_wrap()
facet_wrap()
creates a plot for every level of a factor passed to it. For example, if we create a scatter plot with Credit Amount on x-axis, Duration of Credit on y-axis, and pass the factor 'Type of Housing' as facet. Then it will create the number of plots equal to the levels in type of Housing with each plot displaying the points only for that level.
g <- ggplot(df,aes(x=Duration.of.Credit..in.months.,y=Credit.amount))
g+geom_point()+geom_smooth()+
facet_wrap(~Type.of.housing)
You can control how the panels are wrapped into a grid with ncol
, nrow
, as.table
and dir
. ncol
and nrow
control how many columns and rows (you only need to set one). as.table
controls whether the facets are laid out like a table (TRUE), with highest values at the bottom-right, or a plot (FALSE), with the highest values at the top-right. dir
controls the direction of wrap: horizontal or vertical.
In the following example, we pass the factor 'Job' for faceting (which as 4 levels). We pass the parameter nrow = 2
to wrap the plots in 2 rows.
g <- ggplot(df,aes(x=Duration.of.Credit..in.months.,y=Credit.amount))
g+geom_point()+geom_smooth()+
facet_wrap(~Job, nrow = 2)
There are two important observations here. One, facets create subsets of the same dataset. Two, the x and y scales of each plot are the same in each facet.
facet_grid()
facet_grid()
allows use to facet using two variables.
. ~ a
spreads the values of a across the columns as shown below:
g <- ggplot(df,aes(x=Duration.of.Credit..in.months.,y=Credit.amount))
g+geom_point()+geom_smooth()+
facet_grid(. ~ Loan.Quality)
b ~ .
spreads the values of b down the rows.
g <- ggplot(df,aes(x=Duration.of.Credit..in.months.,y=Credit.amount))
g+geom_point()+geom_smooth()+
facet_grid(Loan.Quality ~ .)
a ~ b
spreads a across columns and b down rows.
It is recommended to put the variable with the greatest number of levels in the columns, to take advantage of the aspect ratio of your screen.
g <- ggplot(df,aes(x=Duration.of.Credit..in.months.,y=Credit.amount))
g+geom_point()+geom_smooth()+
facet_grid(Type.of.housing ~ Loan.Quality)
Facet Scales
For both the facet functions, you can control whether you want the x and y position scales for each panel to be fixed across all panels (fixed) or allow to vary between panels (free).
scales = "fixed"
: x and y scales are fixed across all panels.scales = "free_x"
: the x scale is free, and the y scale is fixed.scales = "free_y"
: the y scale is free, and the x scale is fixed.scales = "free"
: x and y scales vary across panels.
Usually you will want all facets to have same x and y scales for the purpose of comparability. However, in some cases free scales will make more sense, for example, in a situation where each level is measured on a different scale.
Related Downloads
Data Science in Finance: 9-Book Bundle
Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.
What's Included:
- Getting Started with R
- R Programming for Data Science
- Data Visualization with R
- Financial Time Series Analysis with R
- Quantitative Trading Strategies with R
- Derivatives with R
- Credit Risk Modelling With R
- Python for Data Science
- Machine Learning in Finance using Python
Each book includes PDFs, explanations, instructions, data files, and R code for all examples.
Get the Bundle for $29 (Regular $57)Free Guides - Getting Started with R and Python
Enter your name and email address below and we will email you the guides for R programming and Python.