ggalt: Make a Lollipop Plot to Compare Categories in ggplot2
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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 geom_lollipop()
function.
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.
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 group_by()
and summarize()
.
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 [ggalt
Tutorial]
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.
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.
Onto the tutorial.
Load the Libraries and Data
First, run this code to:
- Load Libraries: Load
ggalt
,tidyquant
, andtidyverse
. - Import Data: We’re using the
mpg
dataset that comes withggplot2
.
Here’s the 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 ggplot
and dplyr
functions.
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:
-
group_by()
: To group our dataset by vehicle class -
summarize()
: To calculate the average highway fuel economy by vehicle class
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 geom_lollipop()
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.colour
and point.size
to our liking.
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.
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.
Summary
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. ?
My Struggles with Learning Data Science
It took me a long time to learn data science. 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. Coding is tough, data science is tough, and connecting it all with the business is tough.
If you feel like this, you’re not alone.
The good news is that, after years of learning, I was able to become a highly-rated business consultant working with Fortune 500 clients and my career advanced rapidly. More than that, I was able to help others in the community by developing open source software that has been downloaded over 1,000,000 times, and I found a real passion for coding.
In fact, that’s the driving reason that I created Business Science to help people like you and me that are struggling to learn data science for business (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.
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.