Splitting Plots with Facets in ggplots

Faceting refers to splitting the data into multiple subsets and then displaying plots for the specific subsets in a panel. Such plots are also called small-multiple plots. The facet approach partitions a plot into a matrix of panels. Each panel shows a different subset of the data.

In our scatter plot we grouped the data into bad and good loans using colors. We can use the faceting concept to split the entire graph into two separate panels one for each, good and bad loans. To do so we will use the facet_grid(. ~ ___) layer. facet_grid forms a matrix of panels defined by row and column faceting variables. facet_grid(x ~ y) will display x*y plots even if some plots are empty.

g <- ggplot(df,aes(x=Duration.of.Credit..in.months.,y=Credit.amount))
g+geom_point()+geom_smooth(se=FALSE)+
  facet_grid(. ~ Loan.Quality)

We split our data only by columns. That's why we specify the rows by a simple. The columns are split by Loan.Quality. Since this variable has only two values, the graph will be split into two columns.

Faceting can be specially useful when we want to bring another variable into our plot. For example in this case, we can now add the Job variable to see how the type of job of the borrower impacts the credit.

g <- ggplot(df,aes(x=Duration.of.Credit..in.months.,y=Credit.amount, color=Job))
g+geom_point()+geom_smooth(se=FALSE,method='lm')+
  facet_grid(. ~ Loan.Quality)

ggplot2 is beautiful and to slice the data in a different way is extremely easy. In the following graph, we exchange the two parameters Loan.Quality and Job.

g <- ggplot(df,aes(x=Duration.of.Credit..in.months.,y=Credit.amount, color=Loan.Quality))
g+geom_point()+geom_smooth(se=FALSE,method='lm')+
  facet_grid(. ~ Job)

Related Downloads

Data Science in Finance: 9-Book Bundle

Data Science in Finance 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)
JOIN 30,000 DATA PROFESSIONALS

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.

Data Science in Finance: 9-Book Bundle

Data Science in Finance 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 comes with PDFs, detailed explanations, step-by-step instructions, data files, and complete downloadable R code for all examples.