Dot plot with R

Cedric Vidonne

Lei Chen

Dot plot with R

A dot plot is a type of graph in which the dots are connected to show changes or differences. It’s an alternative to the grouped bar chart or slope chart.

More about: Dot plot


Dot plot

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

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

# Plot
ggplot(
  df,
  aes(
    x = percent,
    y = reorder(location, desc(order))
  )
) +
  geom_line(aes(group = location),
    size = 0.75,
    color = unhcr_pal(n = 1, "pal_grey")
  ) +
  geom_point(aes(color = period),
    size = 2.5
  ) +
  scale_color_unhcr_d(
    palette = "pal_unhcr",
    labels = c("Before COVID-19", "First month of the crisis"),
    direction = -1
  ) +
  labs(
    title = "COVID-19 impact on poverty rates of informal workers",
    caption = "Source: International Labor Organization\n© UNHCR, The UN Refugee Agency"
  ) +
  scale_x_continuous(
    limit = c(0, 1),
    label = label_percent()
  ) +
  scale_y_discrete(label = wrap_format(25)) +
  theme_unhcr(
    grid = "XY",
    axis = FALSE,
    axis_title = FALSE
  )

A dotplot showing COVID-19 impact on poverty rates of informal workers


Dot plot with labels

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

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

# Plot
ggplot(
  df,
  aes(
    x = percent,
    y = reorder(location, desc(order))
  )
) +
  geom_line(aes(group = location),
    size = 0.75,
    color = unhcr_pal(n = 1, "pal_grey")
  ) +
  geom_point(aes(color = period),
    size = 2.5
  ) +
  scale_color_unhcr_d(
    palette = "pal_unhcr",
    labels = c("Before COVID-19", "First month of the crisis"),
    direction = -1
  ) +
  geom_text(
    data = df %>% filter(period == "before_covid"),
    aes(label = scales::percent(percent, accuracy = 1)),
    nudge_x = -0.06,
    size = 8 / .pt
  ) +
  geom_text(
    data = df %>% filter(period == "first_months"),
    aes(label = scales::percent(percent, accuracy = 1)),
    nudge_x = 0.06,
    size = 8 / .pt
  ) +
  labs(
    title = "COVID-19 impact on poverty rates of informal workers",
    caption = "Source: International Labor Organization\n© UNHCR, The UN Refugee Agency"
  ) +
  scale_x_continuous(
    limit = c(0, 1),
    label = label_percent(),
    breaks = c(0, 1)
  ) +
  scale_y_discrete(label = wrap_format(25)) +
  theme_unhcr(
    grid = "XY",
    axis = FALSE,
    axis_title = FALSE,
    axis_text = "XY"
  )

A dotplot showing COVID-19 impact on poverty rates of informal workers


Related chart with R