Treemap with R

Cedric Vidonne

Lei Chen

Treemap with R

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


Treemap

# 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 = 9,
  start = "topleft",
  family = "Lato"
  ) +
  labs(
    title = "UNHCR global workforce by region | 2021",
    caption = "Source: Define source here\n© UNHCR, The UN Refugee Agency"
  ) +
  theme_unhcr(
    axis = FALSE,
    axis_title = FALSE,
    grid = FALSE
  )

A treemap showing UNHCR global workforce by region | 2021


Related chart with R