Lollipop chart
As a variant of the bar/column chart, the lollipop chart consists of lines and dots at the end to highlight the values. Like a bar chart, a lollipop chart is used to compare categorical data.
More about: Lollipop chart - Other tutorials: Matplotlib
Basic 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 = 3
) +
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",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
scale_x_continuous(
expand = expansion(c(0, 0.1)),
breaks = scales::pretty_breaks(n = 7),
labels = scales::label_number(scale_cut = scales::cut_short_scale()),
limits = c(0, NA)
) +
theme_unhcr(grid = "X", axis_title = FALSE, axis = "y")
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 = 3
) +
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 = paste0(round(displaced_number / 1e6, 2), "M")
),
hjust = -0.3,
size = 10 / ggplot2::.pt
) +
labs(
title = "People displaced across borders by country of origin | 2021",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
scale_x_continuous(
expand = expansion(c(0, 0.1)),
breaks = scales::pretty_breaks(n = 7),
labels = scales::label_number(scale_cut = scales::cut_short_scale()),
limits = c(0, NA)
) +
theme_unhcr(grid = FALSE, axis_title = FALSE, axis = "y", axis_text = "Y")
Ordered bar chart
An ordered bar chart is a chart in which each category is represented by a horizontal rectangle, with the length of the rectangle being ordered and proportional to the values being plotted.
More about: Ordered bar chart - Other tutorials: Matplotlib
Basic ordered bar 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/bar_ordered.csv")
# Plot
ggplot(
df,
aes(
x = returnee_number,
y = reorder(country_origin, returnee_number)
)
) +
geom_col(
fill = unhcr_pal(n = 1, "pal_blue"),
width = .7
) +
labs(
title = "Refugee returns by country of origin (top 5) | 2020",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
scale_x_continuous(
expand = expansion(c(0, 0.1)),
breaks = scales::pretty_breaks(n = 7),
labels = scales::label_number(scale_cut = scales::cut_short_scale())
) +
theme_unhcr(grid = "X", axis_title = FALSE, axis = "y")
Ordered bar 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/bar_ordered.csv")
# Plot
ggplot(
df,
aes(
x = returnee_number,
y = reorder(country_origin, returnee_number)
)
) +
geom_col(
fill = unhcr_pal(n = 1, "pal_blue"),
width = .7
) +
geom_text(
aes(label = scales::comma(returnee_number)),
hjust = -0.1,
size = 10 / ggplot2::.pt
) +
labs(
title = "Refugee returns by country of origin (top 5) | 2020",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
scale_x_continuous(
expand = expansion(c(0, 0.1))
) +
theme_unhcr(grid = FALSE, axis_title = FALSE, axis = "y", axis_text = "Y")
Ordered column chart
An ordered column chart is a chart in which each category is represented by a vertical rectangle, with the height of the rectangle being ordered and proportional to the values being plotted.
More about: Ordered column chart - Other tutorials: Matplotlib
Basic ordered column 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/column_ordered.csv")
# Plot
ggplot(
df,
aes(
x = reorder(country_origin, -returnee_number),
y = returnee_number
)
) +
geom_col(
fill = unhcr_pal(n = 1, "pal_blue"),
width = .7
) +
labs(
title = "Refugee returns by country of origin (top 5) | 2020",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
scale_y_continuous(
expand = expansion(c(0, 0.1)),
breaks = scales::pretty_breaks(n = 7),
labels = scales::label_number(scale_cut = scales::cut_short_scale())
) +
theme_unhcr(grid = "Y", axis_title = FALSE, axis = "x")
Ordered column 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/column_ordered.csv")
# Plot
ggplot(
df,
aes(
x = reorder(country_origin, -returnee_number),
y = returnee_number
)
) +
geom_col(
fill = unhcr_pal(n = 1, "pal_blue"),
width = .7
) +
geom_text(
aes(label = scales::comma(returnee_number)),
vjust = -1,
size = 10 / ggplot2::.pt
) +
labs(
title = "Refugee returns by country of origin (top 5) | 2020",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
scale_y_continuous(
expand = expansion(c(0, 0.1))
) +
theme_unhcr(grid = FALSE, axis_title = FALSE, axis = "x", axis_text = "x")
Slope chart
A slope chart looks like a line chart, but unlike the line chart, it has only two data points for each line. The change between two data points can be easily identified with connected lines (slope up means increase, slope down means decrease).
More about: Slope chart - Other tutorials: Matplotlib
# Loading required packages
library(unhcrthemes)
library(tidyverse)
library(ggrepel)
# Loading data
df <- read_csv("https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/change_over_time/slope.csv")
# Plot
ggplot(df, aes(x = year, y = refugee_number, group = country_origin)) +
geom_line(
linewidth = .7,
color = unhcr_pal(n = 1, "pal_blue")
) +
geom_point(size = 3, color = unhcr_pal(n = 1, "pal_blue")) +
ggrepel::geom_text_repel(
aes(label = paste(
country_origin,
if_else(refugee_number > 1e6,
paste0(round(refugee_number / 1e6, 1), "M"),
paste0(round(refugee_number / 1e3, 1), "k")
)
)),
size = 9 / ggplot2::.pt,
direction = "y",
hjust = if_else(df$year == 2016, 1.3, -0.3)
) +
labs(
title = "Evolution of refugee population in East and Horn of Africa region | 2016 vs 2021",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
scale_x_continuous(
breaks = c(2016, 2021),
limits = c(2013, 2024)
) +
scale_y_continuous(limits = c(-2e5, NA)) +
theme_unhcr(grid = "X", axis_title = FALSE, axis_text = "x")