ggalt: Make a Lollipop Plot to Compare Categories in ggplot2
Written by Matt Dancho
A Lollipop Plot shows the relationship between categories using a dot and a line that connects to a baseline (similar to a Bar Plot). In this short tutorial, we use
ggalt to create a Lollipop Plot with the
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 Lollipop Plot?
The Lollipop Plot is a visualization that shows the comparsion between numberic data and categories in our data. It gets the name because of the Lollipop shape. It’s a great way to show grouped summaries using the classic
dplyr combo of
Lollipop 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 lollipop plot.
Lollipop plots [
This tutorial showcases the awesome power of
ggalt for visualizing lollipop 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.
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
mpg dataset that comes with
Get the code.
mpg dataset. We’ll focus on “hwy” (fuel economy in Miles Per Gallon) and “class” (the standardized vehicle category the vehicle model falls into).
lollipop plot: Using ggplot
Next, we’ll make a lollipop plot that highlights the difference in average highway fuel economy (MPG) by class of vehicle. It helps if you have
dplyr (data wrangling) and
ggplot2 (data visualization) experience.
Pro-Tip 1: Use the R cheat sheet to refer to
Pro-Tip 2: If you need to master R, then I’ll talk about my 5-Course R-Track at the end of the tutorial. It’s a way to up-skill yourself with the data science skills that organizations demand.
Step 1: Prepare the Data
To make a Lollipop Plot with
geom_lollipop(), we need to first get the data into the correct format for the visualization. We’ll use the combination of:
Get the code.
The resulting data is now formatted correctly for the lollipop Plot.
Data wrangling can be a bit tricky. If you need to learn data wrangling with
dplyr (a critical skill), I teach
dplyr in our R for Business Analysis Course. It’s the first course in the 5-Course R-Track.
Now we can make the lollipop plot.
Step 2: Make the Base lollipop Plot with
We start by making a basic lollipop plot with
geom_lollipop(). The trick is to use
horizontal = TRUE to format horizontally and to adjust the
point.size to our liking.
Get the code.
This produces our base plot, which is a lollipop plot of highway fuel economy for each vehicle class.
Data Visualization is a key skill that beginners often struggle with. If you need to learn
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. It’s the first course in the 5-Course R-Track.
Now, we can make the plot awesome with by adding labels and customizing the theme.
Step 3: Customize the
ggplot with labels and theme
It’s a good idea to customize our
ggplot, 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:
geom_label(): I like to add labels to Lollipop Plots that describe key information. This is my personal preference, but I find that it helps business people focus on the important details.
Theme Customization: I use my
tidyquant package for theme customization to match Business Science colors. You may need to adjust for your organization’s theme. Doing this takes your plots from looking amateur to professional.
Refer to the Ultimate R Cheat Sheet and my courses for advanced customization beyond this tutorial.
Get the code.
And here’s the final Lollipop Plot that tells the story of how highway fuel economy varies with the vehicle class. Compact, Subcompact, and Midsize are the most fuel efficient while SUV and Pickup are the least.
Awesome work! The lollipop plot looks amazing.
We learned how to make lollipop plots with
ggalt. But, there’s a lot more to visualization. And, if you are just starting out, then this tutorial was probably difficult. That’s OK because I have a solution.
If you’d like to learn
ggplot2, data visualizations, data wrangling,
shiny apps, and data science for business with R, then read on. 👇
Do You Need Help Becoming A Business Data Scientist Right Now?
YOU know the feeling. Being unhappy with your current job.
Promotions aren’t happening. You’re stuck. Hopeless. Confused…
And you’re praying that the next data science interview will go better than the last 12…
… But you know it won’t. Not unless you take control of your career.
The good news is…
I Can Help You Speed It Up.
I’ve helped 5,897+ students learn data science for business from an elite business consultant’s perspective.
I’ve worked with Fortune 500 companies like S&P Global, Apple, MRM McCann, and more.
And I built a training program that gets my students life-changing data science careers (don’t believe me? see my testimonials here):
6-Figure Data Science Job at CVS Health ($125K)
Senior VP Of Analytics At JP Morgan ($200K)
50%+ Raises & Promotions ($150K)
Lead Data Scientist at Northwestern Mutual ($175K)
2X-ed Salary (From $60K to $120K)
2 Competing ML Job Offers ($150K)
Promotion to Lead Data Scientist ($175K)
Data Scientist Job at Verizon ($125K+)
Data Scientist Job at CitiBank ($100K + Bonus)
Whenever you are ready, here’s how I can help you:
Here’s the system that has gotten aspiring data scientists, career transitioners, and life long learners data science jobs and promotions…
Join My 5-Course R-Track Program
(And Become The Data Scientist You Were Meant To Be...)
P.S. - Samantha landed her NEW Data Science R Developer job at CVS Health (Fortune 500). This could be you.