UNHCR Logo
  • Guidance
  • Chart types
  • Resources
  • Tutorials
  • Product gallery
Tutorials
  • R
    • Change over time
    • Comparison
    • Correlation
    • Distribution
    • Geospatial
    • Part-to-a-whole
    • Ranking
  • Matplotlib
    • Change over time
    • Comparison
    • Correlation
    • Distribution
    • Part-to-a-whole
    • Ranking
  • Plotly Python
    • Comparison
  • D3
    • Change over time
    • Comparison
    • Correlation
    • Distribution
    • Geospatial
    • Part-to-a-whole
On this page
  • Bubble chart
  • Connected scatterplot
  • Heatmap
  • Scatterplot
  1. Home
  2. Tutorials
  3. R
  4. Correlation

Bubble chart

A bubble chart displays multi-dimensional data in a two-dimensional plot. It can be considered as a variation of the scatterplot, in which the dots are replaced with bubbles. However, unlike a scatterplot which has only two variables defined by the X and Y axis, on a bubble chart each data point (bubble) can be assigned with a third variable (by size of bubble) and a fourth variable (by colour of bubble)

More about: Bubble chart - Other tutorials: Matplotlib D3

Bubble chart

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

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

# Plot
ggplot(
  df,
  aes(x = refugee_number, y = idp_number)
) +
  geom_point(aes(size = total_number),
    color = unhcr_pal(n = 1, "pal_blue"),
    alpha = 0.6
  ) +
  scale_size(
    range = c(4, 16),
    name = "Total population",
    labels = scales::label_number(scale_cut = scales::cut_short_scale()),
    breaks = c(8e6, 10e6, 12e6)
  ) +
  geom_text_repel(aes(label = region),
    size = 9 / ggplot2::.pt
  ) +
  labs(
    title = "Comparison of refugee and IDP population by region | 2021",
    y = "Number of IDPs",
    x = "Number of refugees",
    caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
  ) +
  scale_x_continuous(labels = scales::label_number(scale_cut = scales::cut_short_scale())) +
  scale_y_continuous(
    labels = scales::label_number(scale_cut = scales::cut_short_scale()),
    breaks = scales::pretty_breaks(n = 6)
  ) +
  coord_cartesian(clip = "off") +
  theme_unhcr(grid = "XY", axis = FALSE, legend_title = TRUE)

Bubble chart with colours

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

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

# Order regions for visualization
df$region <- factor(df$region,
  levels = c("East and Horn of Africa and Great Lakes", "Southern Africa", "West and Central Africa", "Americas", "Asia and the Pacific", "Europe", "Middle East and North Africa")
)

# Plot
ggplot(
  df,
  aes(x = refugee_number, y = idp_number)
) +
  geom_point(aes(size = total_number, color = region),
    alpha = 0.6
  ) +
  scale_size(
    range = c(4, 16),
    name = "Total population",
    labels = scales::label_number(scale_cut = scales::cut_short_scale()),
    breaks = c(8e6, 10e6, 12e6)
  ) +
  geom_text_repel(aes(label = region),
    size = 9 / ggplot2::.pt
  ) +
  labs(
    title = "Comparison of refugee and IDP population by region | 2021",
    y = "Number of IDPs",
    x = "Number of refugees",
    caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
  ) +
  scale_x_continuous(labels = scales::label_number(scale_cut = scales::cut_short_scale())) +
  scale_y_continuous(
    labels = scales::label_number(scale_cut = scales::cut_short_scale()),
    breaks = scales::pretty_breaks(n = 6)
  ) +
  scale_color_unhcr_d(palette = "pal_unhcr_region", guide = "none") +
  coord_cartesian(clip = "off") +
  theme_unhcr(grid = "XY", axis = FALSE, legend_title = TRUE) +
  guides(size = guide_legend(override.aes = list(shape = 21)))

Connected scatterplot

A connected scatterplot is a type of visualization that displays the evolution of a series of data points that are connected by straight line segments. In some cases, it is not the most intuitive to read; but it is impressive for storytelling.

More about: Connected scatterplot - Other tutorials: Matplotlib D3

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

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

# Plot
ggplot(
  df,
  aes(x = refugee_number, y = idp_number)
) +
  geom_segment(
    aes(
      xend = c(tail(refugee_number, n = -1), NA),
      yend = c(tail(idp_number, n = -1), NA)
    ),
    color = unhcr_pal(n = 1, "pal_grey")
  ) +
  geom_point(
    color = unhcr_pal(n = 1, "pal_blue"),
    size = 3
  ) +
  ggrepel::geom_text_repel(
    data = df[seq(1, nrow(df), 2), ],
    aes(label = year),
    size = 9 / .pt,
    point.padding = 5
  ) +
  labs(
    title = "Evolution of refugee vs IDP population in Afghanistan | 2001-2021",
    y = "Number of IDPs", x = "Number of refugees",
    caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
  ) +
  scale_x_continuous(
    labels = scales::label_number(scale_cut = scales::cut_short_scale())
  ) +
  scale_y_continuous(
    labels = scales::label_number(scale_cut = scales::cut_short_scale())
  ) +
  theme_unhcr()

Heatmap

A heatmap is a type of visualization that values are depicted through variations in colour within a two-dimensional matrix of cells. It allows us to visualize complex data and understand it at a glance.

More about: Heatmap - Other tutorials: Matplotlib

Heatmap

# 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/correlation/heatmap.csv")

# Plot
ggplot(
  df,
  aes(x = factor(year), y = fct_rev(location))
) +
  geom_tile(aes(fill = values),
    color = "white",
    linetype = 1,
    lwd = .5
  ) +
  labs(
    title = "Refugee population by region | 2011 - 2020",
    caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
  ) +
  scale_y_discrete(labels = scales::label_wrap(17)) +
  scale_fill_stepsn(
    colors = unhcr_pal(n = 5, "pal_blue"),
    n.break = 5,
    name = "Number of people\nin millions"
  ) +
  coord_fixed() +
  theme_unhcr(grid = FALSE, axis = FALSE, axis_title = FALSE, legend_title = TRUE)

Heatmap 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/correlation/heatmap.csv")

# Plot
ggplot(
  df,
  aes(x = factor(year), y = fct_rev(location))
) +
  geom_tile(aes(fill = values),
    color = "white",
    linetype = 1,
    lwd = .5
  ) +
  geom_text(aes(label = scales::number(values, accuracy = .1)),
    color = if_else(df$values > 2, "white", unhcr_pal(n = 5, "pal_grey")[5]),
    size = 10 / ggplot2::.pt
  ) +
  labs(
    title = "Refugee population by region | 2011 - 2020",
    subtitle = "Number of people in millions",
    caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
  ) +
  scale_y_discrete(labels = scales::label_wrap(17)) +
  scale_fill_stepsn(
    colors = unhcr_pal(n = 5, "pal_blue"),
    n.break = 5
  ) +
  coord_fixed() +
  theme_unhcr(grid = FALSE, axis = FALSE, axis_title = FALSE, legend = FALSE)

Scatterplot

A scatterplot is a type of visualization using Cartesian Coordinates to display two variables for a set of data. The data are displayed as a collection of dots. The position of each dot on the horizontal and vertical axis indicates the values for an individual data point.

More about: Scatterplot - Other tutorials: Matplotlib D3

Scatterplot

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

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

# Plot
ggplot(
  df,
  aes(x = refugee_number, y = idp_number)
) +
  geom_point(
    color = unhcr_pal(n = 1, "pal_blue"),
    size = 2.5
  ) +
  ggrepel::geom_text_repel(aes(label = region),
    size = 10 / ggplot2::.pt
  ) +
  labs(
    title = "Comparison of refugee and IDP population by region | 2021",
    y = "Number of IDPs", x = "Number of refugees",
    caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
  ) +
  scale_x_continuous(
    labels = scales::label_number(scale_cut = scales::cut_short_scale())
  ) +
  scale_y_continuous(
    labels = scales::label_number(scale_cut = scales::cut_short_scale()),
    breaks = seq(2e6, 12e6, by = 2e6)
  ) +
  theme_unhcr()

Scatterplot with colours

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

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

# Order regions for visualization
df$region <- factor(df$region,
  levels = c(
    "East and Horn of Africa and Great Lakes",
    "Southern Africa",
    "West and Central Africa",
    "Americas",
    "Asia and the Pacific",
    "Europe",
    "Middle East and North Africa"
  )
)

# Plot
ggplot(
  df,
  aes(x = refugee_number, y = idp_number)
) +
  geom_point(
    aes(color = region),
    size = 2.5
  ) +
  ggrepel::geom_text_repel(aes(label = region),
    size = 10 / ggplot2::.pt
  ) +
  labs(
    title = "Comparison of refugee and IDP population by region | 2021",
    y = "Number of IDPs", x = "Number of refugees",
    caption = "Source: UNHCR Refugee Data Finder<br>© UNHCR, The UN Refugee Agency"
  ) +
  scale_x_continuous(
    labels = scales::label_number(scale_cut = scales::cut_short_scale())
  ) +
  scale_y_continuous(
    labels = scales::label_number(scale_cut = scales::cut_short_scale()),
    breaks = seq(2e6, 12e6, by = 2e6)
  ) +
  scale_color_unhcr_d(palette = "pal_unhcr_region") +
  theme_unhcr(legend = FALSE)
Contact us
  • Guidance
  • Chart types
  • Resources
  • Tutorials
  • Product gallery

© UNHCR