Lollipop plot with ggplot extension ggalt

Customizing lollipop. plot with geom_lollipop() in ggalt
Customizing lollipop. plot with geom_lollipop() in ggalt

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

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
Exit mobile version