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 geom_lollipop() function.


SPECIAL ANNOUNCEMENT: AI for Data Scientists Workshop on December 18th

Inside the workshop I’ll share how I built a SQL-Writing Business Intelligence Agent with Generative AI:

Generative AI for Data Scientists

What: GenAI for Data Scientists

When: Wednesday December 18th, 2pm EST

How It Will Help You: Whether you are new to data science or are an expert, Generative AI is changing the game. There’s a ton of hype. But how can Generative AI actually help you become a better data scientist and help you stand out in your career? I’ll show you inside my free Generative AI for Data Scientists workshop.

Price: Does Free sound good?

How To Join: 👉 Register Here


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

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.

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) and “class” (the standardized vehicle category the vehicle model falls into).

MPG Dataset

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

Get the code.

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

Data Wranlging - Pivoted for 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.

Get the code.

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

Lollipo Plot - Basic

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.

Lollipop Plot - Final

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

Need to advance your business data science skills?

I’ve helped 6,107+ 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 the system they are taking:

Here’s the system that has gotten aspiring data scientists, career transitioners, and life long learners data science jobs and promotions…

What They're Doing - 5 Course R-Track

Join My 5-Course R-Track Program Now!
(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.

Success Samantha Got The Job