Bar chart
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. The horizontal axis shows data value, and the vertical axis displays the categories being compared.
More about: Bar chart - Other tutorials: Matplotlib D3
Basic bar chart
# Loading required packages
library(unhcrthemes)
library(tidyverse)
# Loading data
df <- read_csv("https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/comparison/bar.csv")
# Plot
ggplot(df, aes(
x = displaced_number,
y = reorder(country_origin, displaced_number)
)) +
geom_col(fill = unhcr_pal(n = 1, "pal_blue"), width = 0.8) +
scale_x_continuous(
expand = expansion(mult = c(0, .1)),
breaks = seq(0, 7e6, by = 1e6),
labels = scales::label_number(scale_cut = scales::cut_short_scale())
) +
labs(
title = "People displaced across borders by country of origin | 2021",
x = "Number of people (in million)",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
theme_unhcr(grid = "X", axis = "y", axis_title = "x")
Bar chart 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/comparison/bar.csv")
# Plot
ggplot(df, aes(
x = displaced_number,
y = reorder(country_origin, displaced_number)
)) +
geom_col(fill = unhcr_pal(n = 1, "pal_blue"), width = 0.8) +
geom_text(
aes(label = round(displaced_number / 1e6, 1)),
hjust = -.2
) +
scale_x_continuous(expand = expansion(mult = c(0, .1))) +
labs(
title = "People displaced across borders by country of origin | 2021",
subtitle = "Number of people (in million)",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
theme_unhcr(grid = FALSE, axis = "y", axis_title = FALSE, axis_text = "y")
Column chart
In a column chart, each category is represented by a vertical rectangle, with the height of the rectangle being proportional to the values being plotted.
More about: Column chart - Other tutorials: Matplotlib Plotly Python D3
Basic column chart
# Loading required packages
library(unhcrthemes)
library(tidyverse)
# Loading data
df <- read_csv("https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/comparison/column.csv")
# Plot
ggplot(df) +
geom_col(aes(x = year, y = displaced_number),
fill = unhcr_pal(n = 1, "pal_blue"),
width = 0.8
) +
labs(
title = "Total displaced population | 2011 - 2021",
y = "Number of people (in million)",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
scale_x_continuous(breaks = seq(2011, 2021, by = 1)) +
scale_y_continuous(
expand = expansion(c(0, 0.1)),
labels = scales::label_number(scale_cut = scales::cut_short_scale())
) +
theme_unhcr(grid = "Y", axis = "x", axis_title = "y")
Column chart 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/comparison/column.csv")
# Plot
ggplot(df, aes(x = year, y = displaced_number)) +
geom_col(fill = unhcr_pal(n = 1, "pal_blue"), width = 0.8) +
geom_text(
aes(label = scales::label_number(
scale_cut = scales::cut_short_scale(),
accuracy = .1
)(displaced_number)),
vjust = -.4
) +
labs(
title = "Total displaced population | 2011 - 2021",
subtitle = "Number of people (in million)",
y = "Number of people (in million)",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
scale_x_continuous(breaks = seq(2011, 2021, by = 1)) +
scale_y_continuous(expand = expansion(c(0, 0.1))) +
theme_unhcr(grid = FALSE, axis = "x", axis_title = FALSE, axis_text = "x")
Grouped bar chart
Grouped bar charts are a type of colour-coded bar chart that is used to represent and compare different categories of two or more groups.
More about: Grouped bar chart - Other tutorials: Matplotlib D3
Basic grouped 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/comparison/bar_grouped.csv")
# Plot
ggplot(df, aes(
x = asylum_application,
y = factor(region),
fill = factor(year)
)) +
geom_col(position = position_dodge(0.7), width = 0.6) +
scale_x_continuous(
expand = expansion(mult = c(0, .1)),
labels = scales::label_number(scale_cut = scales::cut_short_scale())
) +
scale_y_discrete(labels = scales::label_wrap(17), limits = rev) +
scale_fill_unhcr_d(palette = "pal_unhcr") +
labs(
title = "Individual asylum applications registered by region | 2019-2020",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
theme_unhcr(grid = "X", axis = "y", axis_title = FALSE)
Grouped 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/comparison/bar_grouped.csv")
# Plot
ggplot(df, aes(
x = asylum_application,
y = factor(region),
fill = factor(year)
)) +
geom_col(position = position_dodge(0.7), width = 0.6) +
geom_text(
aes(label = scales::label_number(
scale = .001,
suffix = "K",
accuracy = .1
)(asylum_application)),
position = position_dodge(0.7),
hjust = -0.1,
size = 10 / ggplot2::.pt
) +
scale_x_continuous(expand = expansion(mult = c(0, .1))) +
scale_y_discrete(labels = scales::label_wrap(17), limits = rev) +
scale_fill_unhcr_d(palette = "pal_unhcr") +
labs(
title = "Individual asylum applications registered by region | 2019-2020",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
theme_unhcr(grid = FALSE, axis = "y", axis_title = FALSE, axis_text = "y")
Grouped column chart
Grouped column charts are a type of colour-coded column chart used to represent and compare different categories of two or more groups.
More about: Grouped column chart - Other tutorials: Matplotlib Plotly Python D3
Basic grouped 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/comparison/column_grouped.csv")
# Plot
ggplot(df, aes(
x = factor(year),
y = refugee_number,
fill = factor(main_office)
)) +
geom_col(position = position_dodge(0.7), width = 0.6) +
scale_y_continuous(
expand = expansion(c(0, 0.1)),
labels = scales::label_number(scale_cut = scales::cut_short_scale())
) +
scale_fill_unhcr_d(palette = "pal_unhcr", nmax = 3, order = c(2, 3, 1)) +
labs(
title = "Refugees in Africa region | 2018-2021",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
theme_unhcr(grid = "Y", axis = "x", axis_title = FALSE)
Grouped 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/comparison/column_grouped.csv")
# Plot
ggplot(df, aes(
x = factor(year),
y = refugee_number,
fill = factor(main_office)
)) +
geom_col(position = position_dodge(0.7), width = 0.6) +
geom_text(
aes(label = scales::label_number(
scale = 1 / 1e6,
suffix = "M",
accuracy = .1
)(refugee_number)),
position = position_dodge(0.7),
vjust = -1,
size = 10 / ggplot2::.pt
) +
scale_y_continuous(
expand = expansion(c(0, 0.1)),
labels = scales::label_number(scale_cut = scales::cut_short_scale())
) +
scale_fill_unhcr_d(palette = "pal_unhcr", nmax = 3, order = c(2, 3, 1)) +
labs(
title = "Refugees in Africa region | 2018-2021",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
theme_unhcr(grid = FALSE, axis = "x", axis_title = FALSE, axis_text = "x")
Stacked bar chart
Stacked bar charts stack horizontal bars that represent different groups one after another. The length of the stacked bar shows the combined value of the groups.
More about: Stacked bar chart - Other tutorials: Matplotlib D3
Basic stacked 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/comparison/bar_stacked.csv")
# Plot
ggplot(df, aes(
x = population_number,
y = reorder(country_origin, population_number),
fill = factor(population_type)
)) +
geom_col(width = 0.7) +
scale_x_continuous(
expand = expansion(mult = c(0, .1)),
labels = scales::label_number(scale_cut = scales::cut_short_scale()),
breaks = seq(0, 7e6, 1e6)
) +
scale_fill_unhcr_d(
palette = "pal_unhcr",
nmax = 10,
order = c(3, 1, 9),
labels = c("Asylum-seekers", "Refugees", "Other people in need of\ninternational protection")
) +
labs(
title = "People displaced across borders by country of origin | 2021",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
theme_unhcr(grid = "X", axis = "y", axis_title = FALSE)
Stacked 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/comparison/bar_stacked.csv")
# Plot
ggplot(df, aes(
x = population_number,
y = reorder(country_origin, population_number),
fill = factor(population_type)
)) +
geom_col(width = 0.7) +
geom_text(
aes(
label = scales::label_number(
scale = 1 / 1e6,
suffix = "M",
accuracy = .1
)(population_number)
),
position = position_stack(vjust = .5),
hjust = if_else(df$population_number < 2e5 & df$population_type == "ASY", -.35, 0.5),
color = if_else(df$population_number < 2e5 & df$population_type == "ASY", "grey20", "white"),
size = 8 / ggplot2::.pt
) +
scale_x_continuous(
expand = expansion(mult = c(0, .1)),
) +
scale_fill_unhcr_d(
palette = "pal_unhcr",
nmax = 10,
order = c(3, 1, 9),
labels = c("Asylum-seekers", "Refugees", "Other people in need of\ninternational protection")
) +
labs(
title = "People displaced across borders by country of origin | 2021",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
theme_unhcr(grid = FALSE, axis = "y", axis_title = FALSE, axis_text = "y")
Stacked column chart
The stacked column chart stacks vertical bars that represent different groups on top of each other. The height of the stacked bar shows the combined value of the groups.
More about: Stacked column chart - Other tutorials: Matplotlib D3
Basic stacked 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/comparison/column_stacked.csv")
# Plot
ggplot(df, aes(
x = factor(year),
y = rst_in_thousand,
fill = rst_type
)) +
geom_col(width = 0.7) +
scale_y_continuous(
expand = expansion(c(0, 0.2)),
labels = scales::label_number(suffix = "K")
) +
scale_fill_unhcr_d(palette = "pal_unhcr", nmax = 2, order = c(2, 1)) +
labs(
title = "Resettlement by UNHCR and others | 2010-2020",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
theme_unhcr(grid = "Y", axis = "x", axis_title = FALSE)
Stacked 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/comparison/column_stacked.csv")
# Plot
ggplot(df, aes(
x = factor(year),
y = rst_in_thousand,
fill = rst_type
)) +
geom_col(width = 0.7) +
geom_text(
aes(label = paste0(rst_in_thousand, "K")),
position = position_stack(vjust = 0.5),
size = 8 / ggplot2::.pt,
color = if_else(df$rst_type == "UNHCR resettlement", "#FFFFFF", "#1a1a1a")
) +
scale_y_continuous(expand = expansion(c(0, 0.2))) +
scale_fill_unhcr_d(palette = "pal_unhcr", nmax = 2, order = c(2, 1)) +
labs(
title = "Resettlement by UNHCR and others | 2010-2020",
caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
) +
theme_unhcr(grid = FALSE, axis = "x", axis_title = FALSE, axis_text = "x")