ggalt: Make a Dumbbell Plot to Visualize Change in ggplot2

[This article was first published on business-science.io, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 geom_dumbbell().

R-Tips Weekly

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. ?

Video Tutorial
Follow along with our Full YouTube Video Tutorial.

Learn how to use ggalt in our 6-minute YouTube video tutorial.

(Click image to play 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

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 [ggalt Tutorial]

This tutorial showcases the awesome power of ggalt for visualizing dumbbell plots.

R Package Author Credits

This tutorial wouldn’t be possible without the excellent work of Bob Rudis, creator of ggalt. Check out the ggalt package here.

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.

Quick Example:

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.

ggplot2 cheat sheet

Onto the tutorial.

Load the Libraries and Data

First, run this code to:

  1. Load Libraries: Load ggalt, tidyquant, and tidyverse.
  2. Import Data: We’re using the mpg dataset that comes with ggplot2.

Get the code.

Here’s the 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).

MPG Dataset

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 ggplot and dplyr functions.

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 dplyr and ggplot2.

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).

Get the code.

The resulting data is now formatted correctly for the Dumbbell Plot.

Data Wranlging - Pivoted for 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 geom_dumbbell()

We start by making a basic dumbbell plot with geom_dumbbell(). The trick is to use x and xend to specify the start and end points of the dumbbell plot.

Get the code.

This produces our base plot, which is a dumbbell plot of highway fuel economy for each vehicle model.

Dumbell Plot - Basic

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 tidyquant.

Step 3: Customize the ggplot theme

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 tidyquant and ggplot for theme customization to match Business Science colors. Refer to the Ultimate R Cheat Sheet and ggplot2 documentation for more customization.

Get the code.

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.

Dumbell Plot - Final

Summary

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:

  1. Data Science does not have to be difficult, it just has to be taught smartly

  2. 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.





To leave a comment for the author, please follow the link and comment on their blog: business-science.io.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)