ggalt: Make a Dumbbell Plot to Visualize Change in ggplot2
Written by Matt Dancho on August 12, 2021
ggalt is a
ggplot2 extension that adds many new ggplot geometries. In this tutorial, we’ll learn how to make dumbbell plots for visualizing change within our data using
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. 👇
Follow along with our Full YouTube Video Tutorial.
Learn how to use
ggalt in our 6-minute YouTube video tutorial.
What is a Dumbbell Plot?
The Dumbbell Plot is a visualization that shows change between two points in our data. It gets the name because of the Dumbbell shape. It’s a great way to show changing data between two points (think start and finish). Here we can see the improvement in vehicle fuel economy over time (between 1999 and 2008). The Dumbbell shows the starting point (MPG in 1999) and the ending point (MPG in 2008).
Dumbbell Plot (We'll make in this tutorial)
We’ll go through a short tutorial to get you up and running with
ggalt to make a dumbbell plot.
Dumbbell plots [
This tutorial showcases the awesome power of
ggalt for visualizing dumbbell plots.
R Package Author Credits
Before we get started, get the R Cheat Sheet
ggalt is great for extending ggplot2 with advanced features. But, you’ll need to learn
ggplot2 to take full advantage. For these 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” hyperlink to “ggplot2”.
Now you’re ready to quickly reference the
ggplot2 cheat sheet. This shows you the core plotting functions available in the ggplot library.
Onto the tutorial.
Load the Libraries and Data
First, run this code to:
- Load Libraries: Load
- Import Data: We’re using the
mpgdataset that comes with
mpg dataset. We’ll focus on “hwy” (fuel economy in Miles Per Gallon), “year” (the vehicle model year), and “model” (the manufacturer’s vehicle description).
Dumbbell plot: Using ggplot
Next, we’ll make a Dumbbell plot that highlights the change in Vehicle Fuel Economy (MPG) for each Model from 1999 to 2008. It helps if you have
dplyr (data wrangling) and
ggplot2 (data visualization) experience.
Pro-Tip 1: Definitely use the cheat sheet to refer to
Pro-Tip 2: I have a course that can help. It’s called R for Business Analysis Course. Check it out if you want to master
Step 1: Prepare the Data
To make a Dumbbell Plot with
geom_dumbbell(), we need to first get the data into the correct format for the visualization. The trick is to use
pivot_wider() to pivot the data using an aggregation (
mean()) to get the vehicle fuel economy (
hwy) into two columns separated by year (1999 vs 2008).
The resulting data is now formatted correctly for the Dumbbell Plot.
Data wrangling can be a bit tricky. If you’d like to learn data wrangling with
dplyr (a critical skill), I teach
dplyr in my R for Business Analysis Course.
Now we can make the dumbbell plot.
Step 2: Make the Base Dumbbell Plot with
We start by making a basic dumbbell plot with
geom_dumbbell(). The trick is to use
xend to specify the start and end points of the dumbbell plot.
This produces our base plot, which is a dumbbell plot of highway fuel economy for each vehicle model.
Data Visualization is a key skill that beginners often struggle with. If you are interested in learning
ggplot2 in-depth, check out our R for Business Analysis Course (DS4B 101-R) that contains over 30-hours of video lessons on learning R for data analysis.
Now, we can make the plot awesome with themes and
Step 3: Customize the
It’s a good idea to adjust our plot
theme(), especially if we are going to present to business stakeholders in a presentation or report (you’ll likely want to match your organization’s colors). We’ll leverage
ggplot for theme customization to match Business Science colors. Refer to the Ultimate R Cheat Sheet and
ggplot2 documentation for more customization.
And here’s the output. We have our final plot that tells the story of how highway fuel economy varies with the vehicle’s number of cylinders and engine displacement volume.
We learned how to make dumbbell plots with
ggalt. But, there’s a lot more to visualization.
It’s critical to learn how to visualize with
ggplot2, which is the premier framework for data visualization in R.
If you’d like to learn
ggplot2, data visualizations, data wrangling, and data science for business with R, then read on. 👇
My Struggles with Learning Data Science
It took me a long time to learn data science. And I made a lot of mistakes as I fumbled through learning R. I specifically had a tough time navigating the ever increasing landscape of tools and packages, trying to pick between R and Python, and getting lost along the way.
If you feel like this, you’re not alone.
In fact, that’s the driving reason that I created Business Science and Business Science University (You can read about my personal journey here).
What I found out is that:
Data Science does not have to be difficult, it just has to be taught smartly
Anyone can learn data science fast provided they are motivated.
How I can help
If you are interested in learning R and the ecosystem of tools at a deeper level, then I have a streamlined program that will get you past your struggles and improve your career in the process.
It’s called the 5-Course R-Track System. It’s an integrated system containing 5 courses that work together on a learning path. Through 5+ projects, you learn everything you need to help your organization: from data science foundations, to advanced machine learning, to web applications and deployment.
The result is that you break through previous struggles, learning from my experience & our community of 2000+ data scientists that are ready to help you succeed.
Ready to take the next step? Then let’s get started.
👇 Top R-Tips Tutorials you might like:
- mmtable2: ggplot2 for tables
- ggdist: Make a Raincloud Plot to Visualize Distribution in ggplot2
- ggside: Plot linear regression with marginal distributions
- DataEditR: Interactive Data Editing in R
- openxlsx: How to Automate Excel in R
- officer: How to Automate PowerPoint in R
- DataExplorer: Fast EDA in R
- esquisse: Interactive ggplot2 builder
- gghalves: Half-plots with ggplot2
- rmarkdown: How to Automate PDF Reporting
- patchwork: How to combine multiple ggplots
- Geospatial Map Visualizations in R
Want these tips every week? Join R-Tips Weekly.