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
  • 100% stacked column chart
  • Donut chart
  • Pie chart
  • Treemap
  1. Home
  2. Tutorials
  3. R
  4. Part-to-a-whole

100% stacked column chart

100% stacked column charts are similar to stacked column charts in that categories are represented as vertical bars and series as components of those bars. However, in a 100% stacked column chart, each series bar represents the percentage of the whole to which it belongs, where the total (cumulative) of each stacked bar always equals 100%.

More about: 100% stacked 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/part_to_a_whole/column_stacked_100perc.csv")

# Plot
ggplot(
  df,
  aes(
    x = factor(year),
    y = percentage,
    fill = funding_type
  )
) +
  geom_col(position = "fill", width = .7) +
  labs(
    title = "Levels of earmarking | 2012-2020",
    caption = "Source: UNHCR<br>© UNHCR, The UN Refugee Agency"
  ) +
  scale_fill_unhcr_d(palette = "pal_unhcr") +
  scale_y_continuous(
    expand = expansion(c(0, 0.01)),
    labels = scales::label_percent()
  ) +
  theme_unhcr(grid = "Y", axis = "x", axis_title = FALSE, legend_text = 10)

Donut chart

The donut chart is a variation of a pie charts, with the total amount divided into categories based on a proportional value. For the most part, there aren't significant differences between a pie chart and a donut chart, so the choice of a donut over a standard circle is mostly aesthetic.

More about: Donut chart - Other tutorials: Matplotlib D3

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

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

# Plot
ggplot(df) +
  geom_col(aes(
    x = 1,
    y = funding_value,
    fill = funding_type
  )) +
  geom_text(
    aes(
      y = cumsum(funding_value) - funding_value / 2,
      label = paste0(
        funding_type, "\nUS$ ",
        round(funding_value / 1e9, 2), "B\n",
        round(100 * funding_value / sum(funding_value), 1),
        "%"
      )
    ),
    x = 1,
    color = if_else(df$funding_type == "Received", "white", "grey10"),
    size = 9 / ggplot2::.pt
  ) +
  geom_text(
    x = 0,
    y = 0,
    label = paste0(
      "Total required",
      "\nUS$ ", round(sum(df$funding_value) / 1e9, 2), "B"
    ),
    size = 11 / ggplot2::.pt,
    vjust = 1
  ) +
  labs(
    title = "2021 UNHCR's funding",
    caption = "Source: Define source here<br>© UNHCR, The UN Refugee Agency"
  ) +
  scale_x_continuous(expand = expansion(mult = c(0.8, 0.2))) +
  scale_fill_unhcr_d(direction = -1) +
  coord_polar(theta = "y") +
  theme_unhcr(void = TRUE, legend = FALSE)

Pie chart

A pie chart shows how a total amount is divided between different categorical variables as a circle divided into proportional segments. Each categorical value corresponds with a single slice of the circle, and each arc length indicates the proportion of each category.

More about: Pie chart - Other tutorials: Matplotlib D3

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

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

# Plot
ggplot(df) +
  geom_col(aes(
    x = 1,
    y = funding_value,
    fill = funding_type
  )) +
  geom_text(
    aes(
      y = cumsum(funding_value) - funding_value / 2,
      label = paste0(
        funding_type, "\nUS$ ",
        round(funding_value / 1e9, 2), "B\n",
        round(100 * funding_value / sum(funding_value), 1),
        "%"
      )
    ),
    x = 1,
    color = if_else(df$funding_type == "Received", "white", "grey10"),
    size = 10 / ggplot2::.pt
  ) +
  labs(
    title = "2021 UNHCR's funding",
    caption = "Source: Define source here<br>© UNHCR, The UN Refugee Agency"
  ) +
  scale_fill_unhcr_d(direction = -1) +
  coord_polar(theta = "y") +
  theme_unhcr(void = TRUE, legend = FALSE)

Treemap

As a variation of a tree diagram, a treemap is meant to show hierarchical structure using the size of the rectangle to represent quantity. Each category is assigned a rectangle, with subcategories displayed inside the large rectangle, in proportionate size against each other.

More about: Treemap - Other tutorials: Matplotlib D3

The {treemapify} package is used to create treemaps in R. You can find more information about the package here.

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

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

# Plot
ggplot(
  df,
  aes(area = staff_number)
) +
  geom_treemap(
    color = "#FFFFFF",
    size = 1,
    fill = unhcr_pal(n = 1, "pal_blue"),
    start = "topleft"
  ) +
  geom_treemap_text(
    aes(label = paste0(
      round(100 * staff_number / sum(staff_number), 1),
      "%\n",
      region
    )),
    color = "#FFFFFF",
    size = 10,
    start = "topleft",
    family = "Lato",
    padding.y = unit(2, "mm"),
    lineheight = 1.1
  ) +
  labs(
    title = "UNHCR global workforce by region | 2021",
    caption = "Source: Define source here<br>© UNHCR, The UN Refugee Agency"
  ) +
  theme_unhcr(void = TRUE)
Contact us
  • Guidance
  • Chart types
  • Resources
  • Tutorials
  • Product gallery

© UNHCR