Area chart
An area chart, like a line chart, displays the evolution of numeric variables over a continuous period of time. However, in an area chart, the area between the line and x-axis is filled with colour or texture.
More about: Area chart - Other tutorials: Matplotlib D3
# 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/change_over_time/area.csv")
# Plot
ggplot(
filter(df, population_type == "Refugees"),
aes(
x = year,
y = population_number
)
) +
geom_area(
fill = unhcr_pal(n = 1, "pal_blue"),
alpha = 0.3
) +
geom_line(
size = 1,
color = unhcr_pal(n = 1, "pal_blue")
) +
labs(
title = "Number of refugees | 1991-2021",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
scale_x_continuous(breaks = seq(1990, 2020, by = 5)) +
scale_y_continuous(
expand = expansion(c(0, 0.1)),
breaks = scales::breaks_pretty(n = 4),
labels = scales::label_number(scale_cut = scales::cut_short_scale())
) +
theme_unhcr(grid = "Y", axis = "x", axis_title = FALSE)
Dot plot
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 - Other tutorials: Matplotlib
Basic 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),
linewidth = .7,
color = unhcr_pal(n = 1, "pal_grey")
) +
geom_point(aes(color = period), size = 3) +
labs(
title = "COVID-19 impact on poverty rates of informal workers",
caption = "Source: International Labor Organization<br>© UNHCR, The UN Refugee Agency"
) +
scale_x_continuous(
limit = c(0, 1),
label = scales::label_percent()
) +
scale_y_discrete(label = scales::wrap_format(25)) +
scale_color_unhcr_d(
palette = "pal_unhcr",
labels = c("Before COVID-19", "First month of the crisis"),
direction = -1
) +
theme_unhcr(axis_title = FALSE)
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),
linewidth = .7,
color = unhcr_pal(n = 1, "pal_grey")
) +
geom_point(aes(color = period), size = 3) +
geom_label(
aes(label = scales::percent(percent, accuracy = 1)),
nudge_x = if_else(df$period == "before_covid", -0.05, 0.05),
size = 9 / ggplot2::.pt,
label.size = NA
) +
labs(
title = "COVID-19 impact on poverty rates of informal workers",
caption = "Source: International Labor Organization<br>© UNHCR, The UN Refugee Agency"
) +
scale_x_continuous(
limit = c(0, 1),
label = scales::label_percent()
) +
scale_y_discrete(label = scales::wrap_format(25)) +
scale_color_unhcr_d(
palette = "pal_unhcr",
labels = c("Before COVID-19", "First month of the crisis"),
direction = -1
) +
theme_unhcr(axis_title = FALSE)
Line chart
A line chart is a type of chart that displays the evolution of one or several numeric variables over a continuous interval or time period.
More about: Line chart - Other tutorials: Matplotlib D3
Single line 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/change_over_time/line.csv")
# Plot
ggplot(filter(df, population_type == "Refugees")) +
geom_line(
aes(
x = year,
y = population_number,
group = population_type
),
linewidth = 1,
color = unhcr_pal(n = 1, "pal_blue")
) +
labs(
title = "Number of refugees | 1991-2021",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
scale_x_continuous(breaks = scales::breaks_pretty()) +
scale_y_continuous(
expand = expansion(c(0, 0.1)),
breaks = scales::breaks_pretty(n = 4),
labels = scales::label_number(scale_cut = scales::cut_short_scale()),
limits = c(5 * 1e6, 25 * 1e6)
) +
theme_unhcr(grid = "Y", axis = "x", axis_title = FALSE)
Multiple lines 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/change_over_time/line.csv")
# Plot
ggplot(df, aes(
x = year,
y = population_number,
color = population_type
)) +
geom_line(linewidth = 1) +
geom_text(
data = filter(df, year == max(year)),
aes(label = population_type),
hjust = 0.5,
nudge_y = 2.5e6,
size = 10 / ggplot2::.pt
) +
labs(
title = "Number of refugees and IDPs of concern to UNHCR | 1991-2021",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
scale_x_continuous(breaks = scales::breaks_pretty()) +
scale_y_continuous(
expand = expansion(c(0, 0.1)),
breaks = scales::breaks_pretty(n = 4),
labels = scales::label_number(scale_cut = scales::cut_short_scale()),
limits = c(0, 60 * 1e6)
) +
scale_color_unhcr_d(palette = "pal_unhcr", nmax = 10, order = c(5, 1)) +
theme_unhcr(grid = "Y", axis = "x", axis_title = FALSE, legend = FALSE)
Line column chart
A line column chart is a type of visualization that combines both line and column charts together, using dual axes displayed on the left and right sides of the chart.
More about: Line column chart - Other tutorials: Matplotlib D3
# 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/change_over_time/line_column.csv")
# Plot
ggplot(df, aes(x = factor(year))) +
geom_col(aes(y = displaced_population),
fill = unhcr_pal(n = 1, "pal_blue")
) +
geom_line(
aes(y = displaced_proportion * 10, group = 1),
linewidth = 1,
color = unhcr_pal(n = 1, "pal_red")
) +
labs(
title = "Trend of global displacement | 2007 - 2016",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
scale_y_continuous("Displaced population (millions)",
expand = expansion(c(0, 0.2)),
breaks = scales::pretty_breaks(),
sec.axis = sec_axis(~ . / 10,
breaks = scales::pretty_breaks(),
name = "Proportion displaced (number displaced per 1,000)"
)
) +
theme_unhcr(grid = "Y", axis = "x", axis_title = "Y") +
theme(
axis.title.y.right = element_text(color = unhcr_pal(n = 1, "pal_red")),
axis.title.y.left = element_text(color = unhcr_pal(n = 1, "pal_blue"))
)
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")
Stacked area chart
As a variation of a simple area chart, a stacked area chart displays the changes of value of multiple data series over a period of time.
More about: Stacked area chart - Other tutorials: Matplotlib D3
# 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/change_over_time/area_stacked.csv") |>
mutate(months = lubridate::my(paste(months, "2020")))
# Plot
ggplot(df) +
geom_area(aes(
x = months,
y = funding_million,
fill = funding_type
)) +
labs(
title = "Evolution of funding in West Africa region | 2020",
y = "USD millions",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
scale_x_date(
date_labels = "%b",
breaks = scales::breaks_pretty(n = 12)
) +
scale_y_continuous(expand = expansion(c(0, 0.1))) +
scale_fill_unhcr_d(palette = "pal_unhcr") +
theme_unhcr(grid = "Y", axis = "x", axis_title = "y")
Streamgraph
A streamgraph is a variation of a stacked area chart. Instead of displaying values on top of a fixed, straight baseline at the bottom of the stack, the values of the streamgraph are displaced around a central baseline.
More about: Streamgraph - Other tutorials: Matplotlib D3
This example use the {ggstream}
package, you can find more information on the package here.
# Loading required packages
library(unhcrthemes)
library(tidyverse)
library(scales)
library(ggstream)
# Loading data
df <- read_csv("https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/change_over_time/streamgraph.csv")
# Organize data for plot and create legend labels
df <- df |>
mutate(population_type = factor(population_type,
levels = c("REF", "ASY", "IDP", "STA", "OOC", "VDA")
))
legend_labels <- c("Refugees", "Asylum-seekers", "IDPs", "Stateless persons", "Others of concern", "Other people in need of international protection")
# Plot
ggplot(
df,
aes(
x = year,
y = population_number,
fill = population_type
)
) +
ggstream::geom_stream() +
labs(
title = "People of concern to UNHCR | 1991-2021",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
scale_x_continuous(
breaks = scales::pretty_breaks(n = 6)
) +
scale_y_continuous(
labels = ~ scales::label_number(scale_cut = scales::cut_short_scale())(abs(.x)),
breaks = scales::pretty_breaks(n = 6),
) +
scale_fill_unhcr_d(
palette = "pal_unhcr_poc",
nmax = 9,
order = c(1, 3:5, 8:9),
labels = legend_labels
) +
theme_unhcr(axis_title = FALSE, legend_text_size = 9.5)