This code through explores how to create various lollipop plots using the ggplot2 package in R.
Specifically, we’ll explain and demonstrate what a lollipop plot is, as well as the various ways that it can be constructed to make comparisons between categorical variables.
This topic is valuable because it is always nice to know how to create striking visualizations and charts that complement your data. A lollipop chart operates in a similar manner as a bar chart in that it can show comparisons and ranks. However, it’s most helpful when the bars are around the same length. The thin segment with the defined point at the end creates for a less cluttered image than what the thick bars of a bar graph do.
Specifically, you’ll learn how to:
Use the ggplot2 package to create a basic lollipop chart
Make the lollipop chart visually appealing by customizing the aesthetics
Switch from vertical to horizontal orientations
Change the baseline of the lollipop plot to zone in on a specific range
Here, we’ll show how to create a basic lollipop plot using the ggplot2 package. From there, we’ll take on more advanced examples of this plot.
This is based on the extension of the ggplot2 function, created by Hadley Wickham in 2005. This package was established to expand the primary tools that the Core R package provides in order to allow users to create truly dynamic and appealing visualizations. ggplot2 is a part of the tidyverse package.
A basic example shows how to create a lollipop plot based off of categorical data. You first want to ensure that the ggplot2 library is loaded. In this example, we have created categorical variables based off of animals and their corresponding ages. We create the plot, as well as the points and segments that are instrumental to forming a lollipop plot.
#Library
library(ggplot2)
#Data
<- c("Dog", "Cat", "Fish", "Hamster", "Turtle", "Snake")
animals <- c(7, 10, 6, 4, 2, 3)
age
<- data.frame(x=animals, y=age)
data
#Basic Plot
ggplot(data, aes(x=x, y=y)) +
geom_point() +
geom_segment(aes(x=x, xend=x, y=0, yend=y)) +
xlab("Animals") +
ylab("Age")
Our y-values are very similar, but they are easily differentiated in the lollipop plot format.
More specifically, we can begin to play around with the aesthetics of the plot to really make it appealing to the eye. We can differentiate between the stems and the points to make the y-value really pop out.
library(ggplot2)
<- c("Dog", "Cat", "Fish", "Hamster", "Turtle", "Snake")
animals <- c(7, 10, 6, 4, 2, 3)
age
<- data.frame(x=animals, y=age)
data
ggplot(data, aes(x=x, y=y)) +
geom_point(size=5, color="red") +
geom_segment(aes(x=x, xend=x, y=0, yend=y), size=1, color="orchid3", linetype="dotted") +
xlab("Animals") +
ylab("Age")
What’s more, ggplot2 also has the capability to switch the lollipop plot from a vertical orientation to a horizontal orientation. In order to swap the x-axis and y-axis, simply add the “coord_flip()” function to the end.
library(ggplot2)
<- c("Dog", "Cat", "Fish", "Hamster", "Turtle", "Snake")
animals <- c(7, 10, 6, 4, 2, 3)
age
<- data.frame(x=animals, y=age)
data
ggplot(data, aes(x=x, y=y)) +
geom_point(size=5, color="red") +
geom_segment(aes(x=x, xend=x, y=0, yend=y), size=1, color="orchid3", linetype="dotted") +
xlab("Animals") +
ylab("Age") +
coord_flip()
Most notably, we can adjust the baseline to highlight a specific range of data points. Let’s say that we are interested in seeing which animals are older than 5 years old. Simply change the y-value in the geom-segment line from 0 to 5.
library(ggplot2)
<- c("Dog", "Cat", "Fish", "Hamster", "Turtle", "Snake")
animals <- c(7, 10, 6, 4, 2, 3)
age
<- data.frame(x=animals, y=age)
data
ggplot(data, aes(x=x, y=y)) +
geom_point(size=5, color="red") +
geom_segment(aes(x=x, xend=x, y=5, yend=y), size=1, color="orchid3", linetype="dotted") +
xlab("Animals") +
ylab("Age")
library(ggplot2)
<- c("Dog", "Cat", "Fish", "Hamster", "Turtle", "Snake")
animals <- c(7, 10, 6, 4, 2, 3)
age
<- data.frame(x=animals, y=age)
data
ggplot(data, aes(x=x, y=y)) +
geom_point(size=5, color="red") +
geom_segment(aes(x=x, xend=x, y=5, yend=y), size=1, color="orchid3", linetype="dotted") +
xlab("Animals") +
ylab("Age") +
coord_flip()
Learn more about [package, technique, dataset] with the following:
Lollipop Chart Lollipop Chart
Data Visualization with ggplot2 Data Visualization with ggplot2
Cleveland Dot Plot Cleveland Dot Plot
This code through references and cites the following sources:
Custom lollipop chart. (2018). https://www.r-graph-gallery.com/301-custom-lollipop-chart.html
Zach (2019). How to Create a Lollipop Chart in R. https://www.statology.org/lollipop-chart-r/
Robert I. Kabacoff, Ph.D. (2017). Graphics with ggplot2. https://www.statmethods.net/advgraphs/ggplot2.html