• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
Finance Train

Finance Train

High Quality tutorials for finance, risk, data science

  • Home
  • Data Science
  • CFA® Exam
  • PRM Exam
  • Tutorials
  • Careers
  • Products
  • Login

Facets for ggplot2 Charts in R (Faceting Layer)

Data Science

This lesson is part 27 of 29 in the course Data Visualization with R

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.

Previous Lesson

‹ stat_summary for Statistical Summary in ggplot2 R

Next Lesson

Coordinates in ggplot2 in R ›

Join Our Facebook Group - Finance, Risk and Data Science

Posts You May Like

How to Improve your Financial Health

CFA® Exam Overview and Guidelines (Updated for 2021)

Changing Themes (Look and Feel) in ggplot2 in R

Coordinates in ggplot2 in R

Facets for ggplot2 Charts in R (Faceting Layer)

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

In this Course

  • 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

Latest Tutorials

    • Data Visualization with R
    • Derivatives with R
    • Machine Learning in Finance Using Python
    • Credit Risk Modelling in R
    • Quantitative Trading Strategies in R
    • Financial Time Series Analysis in R
    • VaR Mapping
    • Option Valuation
    • Financial Reporting Standards
    • Fraud
Facebook Group

Membership

Unlock full access to Finance Train and see the entire library of member-only content and resources.

Subscribe

Footer

Recent Posts

  • How to Improve your Financial Health
  • CFA® Exam Overview and Guidelines (Updated for 2021)
  • Changing Themes (Look and Feel) in ggplot2 in R
  • Coordinates in ggplot2 in R
  • Facets for ggplot2 Charts in R (Faceting Layer)

Products

  • Level I Authority for CFA® Exam
  • CFA Level I Practice Questions
  • CFA Level I Mock Exam
  • Level II Question Bank for CFA® Exam
  • PRM Exam 1 Practice Question Bank
  • All Products

Quick Links

  • Privacy Policy
  • Contact Us

CFA Institute does not endorse, promote or warrant the accuracy or quality of Finance Train. CFA® and Chartered Financial Analyst® are registered trademarks owned by CFA Institute.

Copyright © 2021 Finance Train. All rights reserved.

  • About Us
  • Privacy Policy
  • Contact Us