• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

Lollipop plot with ggplot extension ggalt

datavizpyr · January 4, 2023 ·

In this tutorial, we will learn how to make lollipop plot using ggalt, one of the ggplot2 extension packages. A lollipop plot is a great alternative for bar plot as the amount ink used to make the lollipop plot is much smaller.

ggalt, R package developed by @hrbrmstr, has a geom for making lollipop called geom_lollipop(). And we will be using geom_lollipop() to make the lollipo plot and customise a bit.

First, let us load the R packages needed including ggalt. We are use ggalt version 0.4.0 here to make the lollipop plot.

library(tidyverse)
library(gapminder)
library(ggalt)
theme_set(theme_bw(16))
packageVersion("ggalt")
## [1] '0.4.0'

Data preparation for lollipop plot

We will be using a part of gapminder data and make the lollipop plot for the mean life expectancy. values for randomly selected countries from two continents, Africa and Americas.

Let us get the data for making the lollipop chart.

set.seed(2022)
df <- gapminder %>% 
  filter(continent %in% c("Africa", "Americas")) %>%
  group_by(country, continent) %>% 
  summarize(mean_lifeExp = mean(lifeExp)) %>%
  ungroup() %>%
  slice_sample(n =25) 

Our data looks like this with three columns one for country, continent and average life expectancy.

df %>% head()
## # A tibble: 6 × 3
##   country      continent mean_lifeExp
##   <fct>        <fct>            <dbl>
## 1 Namibia      Africa            53.5
## 2 Panama       Americas          67.8
## 3 Venezuela    Americas          66.6
## 4 Tanzania     Africa            47.9
## 5 Botswana     Africa            54.6
## 6 Sierra Leone Africa            36.8

Lollipop plot with geom_lollipop() in ggalt

Now we are ready to make the lollipop plot, a stick with a circle at the end of a stick.

Here we use horizontal=TRUE as argument geom_lollipop() to make the horizontal lollipop chart with country on y axis and mean life expectancy on x axis.

df %>%
  ggplot(aes(y=reorder(country, mean_lifeExp),
             x= mean_lifeExp))+
  geom_lollipop(horizontal=TRUE)+
  labs(y=NULL)
ggsave("lollipop_plot_with_ggalt_geom_lollipop.png")
How to make lollipop plot in R with ggplot
How to make lollipop chart in R with ggplot

Add colors by a variable to Lollipop plot

We can add color to the lollipop chart based. on the value of another variable using color argument to ggplot2’s aes() function.

df  %>%
  ggplot(aes(y=reorder(country, mean_lifeExp),
             x= mean_lifeExp,
             color=continent))+
  geom_lollipop(horizontal=TRUE)+
  labs(y=NULL)+
  theme(legend.position = "top")
ggsave("add_color_lollipop_plot_with_ggalt_geom_lollipop.png")
Add color to lollipop plot
Add color to lollipop plot

Customizing lollipop plot with ggalt

We can also further customize the lollipop plot based on our need. In the example below, we increase the size of circle at the end of stick by using point.size argument to geom_lollipop() function.

df  %>%
  ggplot(aes(y=reorder(country, mean_lifeExp),
             x= mean_lifeExp,
             color=continent))+
  geom_lollipop(point.size=2, 
                horizontal=TRUE)+
  labs(y=NULL)+
  theme(legend.position = "top")
ggsave("customizing_lollipop_plot_with_ggalt_geom_lollipop.png")
Customizing lollipop. plot with geom_lollipop() in ggalt
Customizing lollipop. plot with geom_lollipop() in ggalt

Related posts:

How to add colors to axis tick labels using ggtext?How to Add Colors to Axis Tick Label in ggplot2 Customising Histogram plotting functionHow to write a function make histograms with ggplot How to Increase Color Legend's Point Size in ggplot2How to Increase Legend Key Size in ggplot2 How to reverse legend key order in ggplot2How to reverse legend key order in ggplot2

Filed Under: ggplot2, R, Uncategorized Tagged With: geom_lollipop() ggalt, lollipop plot in R

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version