ggside: Plot Linear Regression using Marginal Distributions (ggplot2 extension)
Written by Matt Dancho on May 18, 2021
This article is part of R-Tips Weekly, a weekly video tutorial that shows you step-by-step how to do common R coding tasks.
Here are the links to get set up. 👇
What are Marginal Distributions?
And how can I use them to uncover complex relationships?
What are Marginal Distributions?
Marginal Distribution (Density) plots are a way to extend your numeric data with side plots that highlight the density (histogram or boxplots work too).
Marginal Distribution Plots were made popular with the seaborn jointplot() side-panels in Python. These add side plots that highlight distributions.
How do we make them in ggplot2?
Marginal distributions can now be made in R using
ggside, a new ggplot2 extension. You can make linear regression with marginal distributions using histograms, densities, box plots, and more. Bonus - The side panels are super customizable for uncovering complex relationships.
Here are two examples of what you can (and will) do in this tutorial! 👇
Linear Regression with Marginal Distribution (Density) Side-Plots (Top and Left)
Facet-Plot with Marginal Box Plots (Top)
Before we get started, get the Cheat Sheet
ggside is great for making marginal distribution side plots. But, you'll still need to learn how to visualize data with ggplot2. For those topics, I'll use the Ultimate R Cheat Sheet to refer to
ggplot2 code in my workflow.
Download the Ultimate R Cheat Sheet. Then Click the "CS" next to "ggplot2" which opens the Data Visualization with Dplyr Cheat Sheet.
Now you're ready to quickly reference
Load Libraries & Data
The libraries we'll need today are patchwork, ggridges, ggrepel, maps, tidyverse, and lubridate. All packages are available on CRAN and can be installed with
install.packages(). Note - I'm using the development version of
ggside, which is what I recommend in the YouTube Video .
The dataset is the mpg data that comes with ggplot2.
SLinear Regression with Marginal Distribution Plot
Replicating Seaborn's jointdist() plot
We'll start by replicating what you can do in Python's Seaborn jointdist() Plot. We'll accomplish this with
We set up the plot just like a normal ggplot.
Refer to the Ultimate R Cheat Sheet for:
Next we add from ggside:
geom_xsidedensity()- Adds a side density panel (top panel).
geom_ysidedensity()- Adds a side density panel (right panel).
The trick is using the
after_stat(density), which makes an awesome looking marginal density side panel plot. I increased the size of the marginal density panels with the
Loess Regression w/ Marginal Density
We generate the regression plot with marginal distributions (density) to highlight key differences between the automobile classes. We can see:
- Pickup, SUV - Have the lowest Highway Fuel Economy (MPG)
- 2seater, Compact, Midsize, Subcompact - Have the highest Highway Fuel Economy
Need help learning ggplot2?
In the R for Business Analysis (DS4B 101-R) Course , I teach 5-hours just on ggplot2. Learn:
- And advanced customizations: Labeled Heat Maps and Lollipop Charts
Plot 2. Faceted Side-Panels
Next, let's try out some advanced functionality. I want to see how ggside handles faceted plots, which are subplots that vary based on a categorical feature. We'll use the "cyl" column to facet, which is for engine size (number of cylinders).
Faceted Side Panels? No problem.
Awesome! I have included facets by "cyl", which creates four plots based on the engine size. ggside picked up on the facets and has made 4 side-panel plots.
Amazing. ggside just works.
Congrats. You just quickly made two report-quality plots with ggplot2 and ggside. Excellent work.
But it gets better
You've just scratched the surface.
What is the best way to become proficient in data science?
You're probably thinking:
- There's so much to learn.
- My time is precious.
I have good news that will put those doubts behind you.
You can learn data science with my state-of-the-art Full 5 Course R-Track System .
Become the data science expert in your organization.
Get Five of our Premium R Courses that Build Expert-Level Machine Learning Skills, Web Application Skills, & Time Series Skills.
Taking these courses is equivalent to:
- 9-Months of Methodical Code-Based Learning.
- 250+ tool-based MOOC courses.
- Education Comparable to 9-months of University Courses.
- 5 end-to-end projects.
- 5 frameworks.