Lollipop chart with R

Cedric Vidonne

Lei Chen

Lollipop chart with R

A bar chart is a chart in which each category is represented by a horizontal rectangle, with the length of the rectangle proportional to the values being plotted.

More about: Lollipop chart


Lollipop chart

# Loading required packages
library(unhcrthemes)
library(tidyverse)
library(scales)

# Loading data
df <- read_csv("https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/ranking/lollipop.csv")

# Plot
ggplot(df) +
  geom_point(aes(
    x = displaced_number,
    y = reorder(country_origin, displaced_number)
  ),
  color = unhcr_pal(n = 1, "pal_blue"),
  size = 2.5
  ) +
  geom_segment(aes(
    x = 0,
    xend = displaced_number,
    y = reorder(country_origin, displaced_number),
    yend = reorder(country_origin, displaced_number)
  ),
  color = unhcr_pal(n = 1, "pal_blue")
  ) +
  labs(
    title = "People displaced across borders by country of origin | 2021",
    x = "Number of people (in million)",
    caption = "Source: UNHCR Refugee Data Finder\n© UNHCR, The UN Refugee Agency"
  ) +
  scale_x_continuous(
    expand = expansion(c(0, 0.1)),
    breaks = pretty_breaks(n = 7),
    labels = label_number_si(),
    limits = c(0, NA)
  ) +
  theme_unhcr(
    grid = "X",
    axis = "y",
    axis_title = "x"
  )

A lollipop chart showing people displaced across borders by country of origin | 2020


Lollipop chart with labels

# Loading required packages
library(unhcrthemes)
library(tidyverse)
library(scales)

# Loading data
df <- read_csv("https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/ranking/lollipop.csv")

# Plot
ggplot(df) +
  geom_point(aes(
    x = displaced_number,
    y = reorder(country_origin, displaced_number)
  ),
  color = unhcr_pal(n = 1, "pal_blue"),
  size = 2.5
  ) +
  geom_segment(aes(
    x = 0,
    xend = displaced_number,
    y = reorder(country_origin, displaced_number),
    yend = reorder(country_origin, displaced_number)
  ),
  color = unhcr_pal(n = 1, "pal_blue")
  ) +
  geom_text(aes(
    x = displaced_number,
    y = reorder(country_origin, displaced_number),
    label = round(displaced_number / 1e6, 2)
  ),
  hjust = -0.5,
  size = 8 / .pt
  ) +
  labs(
    title = "People displaced across borders by country of origin | 2021",
    subtitle = "Number of people (in million)",
    caption = "Source: UNHCR Refugee Data Finder\n© UNHCR, The UN Refugee Agency"
  ) +
  scale_x_continuous(
    expand = expansion(c(0, 0.1)),
    breaks = pretty_breaks(n = 7),
    labels = label_number_si(),
    limits = c(0, NA)
  ) +
  theme_unhcr(
    grid = FALSE,
    axis = "y",
    axis_title = FALSE,
    axis_text = "y"
  )

A lollipop chart showing people displaced across borders by country of origin | 2020


Related chart with R