Introduction

This code through explores how to create various lollipop plots using the ggplot2 package in R.


Content Overview

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.


Why You Should Care

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.


Learning Objectives

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


Lollipop Plots

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.


Further Exposition

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.


Basic Example

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
animals <- c("Dog", "Cat", "Fish", "Hamster", "Turtle", "Snake")
age <- c(7, 10, 6, 4, 2, 3)

data <- data.frame(x=animals, y=age)

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


Advanced Examples

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)

animals <- c("Dog", "Cat", "Fish", "Hamster", "Turtle", "Snake")
age <- c(7, 10, 6, 4, 2, 3)

data <- data.frame(x=animals, y=age)

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)

animals <- c("Dog", "Cat", "Fish", "Hamster", "Turtle", "Snake")
age <- c(7, 10, 6, 4, 2, 3)

data <- data.frame(x=animals, y=age)

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)

animals <- c("Dog", "Cat", "Fish", "Hamster", "Turtle", "Snake")
age <- c(7, 10, 6, 4, 2, 3)

data <- data.frame(x=animals, y=age)

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)

animals <- c("Dog", "Cat", "Fish", "Hamster", "Turtle", "Snake")
age <- c(7, 10, 6, 4, 2, 3)

data <- data.frame(x=animals, y=age)

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


Further Resources

Learn more about [package, technique, dataset] with the following:


Works Cited

This code through references and cites the following sources: