How To Make Lollipop Plot in R with ggplot2?
Last Updated :
05 Oct, 2021
A lollipop plot is the combination of a line and a dot. It shows the relationship between a numeric and a categorical variable just like a barplot. A lollipop plot can be used at places where a barplot and scatter plot both are required. This single plot helps us visualize the problem better and takes less ink space than traditional barplots.
In ggplot2, we make a lollipop plot by concatenating geom_segment() and geom_point() function.
Syntax: ggplot(data, aes(x=x, y=y)) + geom_segment() +geom_point( )
Creating basic lollipop Plot
R
# Create sample data
set.seed(5642)
sample_data <- data.frame(name=c("Geek1","Geek2",
"Geek3","Geek4",
"Geeek5") ,
value=c(31,12,15,28,45))
# Load ggplot2
library("ggplot2")
# Create lollipop plot
ggplot(sample_data, aes(x=name, y=value)) +
geom_segment( aes(x=name, xend=name, y=0, yend=value)) +
geom_point(size=4)
Output:

Add annotation to lollipop plot
To add Annotations to ggplot2 lollipop plot, we use geom_label() function:
Syntax: geom_label(aes(name, value, label = signif(value)), colour, nudge_x, size)
Here,
- color: determines the color of annotations
- nudge_x: determines the x shift of annotations
- size: determines the size of annotations
Code:
R
# Create sample data
set.seed(5642)
sample_data <- data.frame(name=c("Geek1","Geek2","Geek3","Geek4","Geeek5") ,
value=c(31,12,15,28,45))
# Load ggplot2
library("ggplot2")
# Create lollipop plot with annotations
ggplot(sample_data, aes(x=name, y=value)) +
geom_segment( aes(x=name, xend=name, y=0, yend=value)) +
geom_point(size=4) +
geom_label(aes(name, value , label = signif(value)),
colour = "darkred", nudge_x = 0.35, size = 4)
Output:

Customize the Plot
To customize this plot we can change the color of the segment and point using the fill property. We can also change the size of the point using the size property. To add color to geom_segment() we add a color property with the desired color and add size property for the thickness of the segment. To add color to geom_point() we add a color property with the desired color and add size property for increasing the size of the point.
R
# Create sample data
set.seed(5642)
sample_data <- data.frame(name=c("Geek1","Geek2","Geek3",
"Geek4","Geeek5") ,
value=c(31,12,15,28,45))
# Load ggplot2
library("ggplot2")
# Create lollipop plot with custom colors
ggplot(sample_data, aes(x=name, y=value)) +
geom_segment( aes(x=name, xend=name, y=0, yend=value),
color="red", size=3) +
geom_point( color="green", size=10)
Output:

Reorder lollipop plot
We can reorder the lollipop plot using reorder() function with value as a base.
Syntax: ggplot(sample_data, aes(x=reorder(name,value),y=value))
We add reorder function in the aesthetics parameter to reorder our data frame in ascending order with a base of value.
R
# Create sample data
set.seed(5642)
sample_data <- data.frame(name=c("Geek1","Geek2","Geek3",
"Geek4","Geeek5") ,
value=c(31,12,15,28,45))
# Load ggplot2
library("ggplot2")
# Create lollipop plot with reordered data
ggplot(sample_data, aes(x=reorder(name,value),y=value)) +
geom_point(size = 3, colour = "black") +
geom_segment(aes(xend = name, yend = 0), size = 1.2)
Output:
Similar Reads
How To Make Dumbbell Plot in R with ggplot2? The Dumbbell Plot shows the change between two points in our dataset. It is named so because of its Dumbbell shape. It helps us to understand the span of data categorically. To make Dumbbell Plot in R using ggplot2, Â we use the geom_dumbbell() function. Syntax: geom_dumbbell(data, aes(y, x, xend), s
2 min read
How To Make Density Plots with ggplot2 in R? Density plots are a data visualization method used to estimate the probability density function (PDF) of a continuous variable. They provide smooth, continuous data distribution which makes them more informative than histograms in certain situations. The plot is produced by applying a kernel functio
2 min read
How To Make World Map with ggplot2 in R? In this article, we will discuss how to create a world map and plot data on it using the R Programming Language. To create a world map using it we will use the geom_map() function of the ggplot2 package of the R Language. This function returns a ggplot object so all the functions that work on other
4 min read
How to create a plot using ggplot2 with Multiple Lines in R ? In this article, we will discuss how to create a plot using ggplot2 with multiple lines in the R programming language. Method 1: Using geom_line() function In this approach to create a ggplot with multiple lines, the user need to first install and import the ggplot2 package in the R console and then
3 min read
How to Combine Multiple ggplot2 Plots in R? In this article, we will discuss how to combine multiple ggplot2 plots in the R programming language. Combining multiple ggplot2 plots using '+' sign to the final plot In this method to combine multiple plots, here the user can add different types of plots or plots with different data to a single p
2 min read
How To Make Half Violinplot with ggplot2 in R? Half Violin plots are basically used to visualize the distribution and the overall summary of the data at the same time. They are also known as Raincloud plots. A combination of half violin plots with jittered points on top, boxplots and can be further enhanced by adding central measures of tendency
2 min read