Graph info

Should I use this graph?


This graph requires:

✅ at least two categorical variables

✅ a numeric variable of counts (for each level of the categorical variables)

Description

Treemaps display proportionally divided rectangular hierarchies for levels of categorical variables. The layout consists of ‘squarified’ tiles, which display the relative contribution of each categorical value to the overall graph space.

We’ll build a treemap using the treemapify package. Also check out mosaic plots.

Getting set up

PACKAGES:

Install packages.

Code
devtools::install_github("wilkox/treemapify")
install.packages("palmerpenguins")
library(treemapify) 
library(palmerpenguins)
library(ggplot2)

DATA:

Artwork by @allison_horst

Filter the missing values from sex, group the data by species, island, and sex, then count the species and island (then ungroup()).

Code
treemap_peng <- penguins |> 
    dplyr::select(species, island, sex) |>
    tidyr::drop_na() |>
    dplyr::group_by(species, island, sex) |>
    dplyr::count(species, island, sex) |>
    ungroup()
glimpse(treemap_peng)
Rows: 10
Columns: 4
$ species <fct> Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Chinstrap, Chi…
$ island  <fct> Biscoe, Biscoe, Dream, Dream, Torgersen, Torgersen, Dream, Dre…
$ sex     <fct> female, male, female, male, female, male, female, male, female…
$ n       <int> 22, 22, 27, 28, 24, 23, 34, 34, 58, 61

The grammar

CODE:

Create labels with labs()

Initialize the graph with ggplot() and provide data

  • Map the n to area, sex to fill and label, species to subgroup, and island to subgroup2

  • Add geom_treemap()

  • Add geom_treemap_text()

    • place: this controls where the boxes start
    • color: text color
    • min.size: the minimum font size (when re-sizing)
    • alpha: opacity
    • fontface: itlalic/bond
    • family: "sans"/"sansserif"/"mono"
Code
labs_treemap <- labs(
  title = "Adult foraging penguins",
    fill = "Sex")
ggp2_treemap <- ggplot(treemap_peng, 
       aes(area = n, 
           fill = sex, 
           label = sex,
           subgroup = species,
           subgroup2 = island)) +
       treemapify::geom_treemap() +
       treemapify::geom_treemap_text(
           place = "bottomright",
           color = "#d0d0d0",
           min.size = 0,
           alpha = 0.90,
           fontface = "italic",
           family = "sans") 
ggp2_treemap + 
  labs_treemap

GRAPH:

Code
labs_treemap <- labs(
  title = "Adult foraging penguins",
    fill = "Sex")
ggp2_treemap <- ggplot(treemap_peng, 
       aes(area = n, 
           fill = sex, 
           label = sex,
           subgroup = species,
           subgroup2 = island)) +
       treemapify::geom_treemap() +
       treemapify::geom_treemap_text(
           place = "bottomright",
           color = "#d0d0d0",
           min.size = 0,
           alpha = 0.90,
           fontface = "italic",
           family = "sans") 
ggp2_treemap + 
  labs_treemap

More info

treemapify has multiple options for building treemaps. We cover a few of these below, but you should check out the package vignette.

SUBGROUP:

ggplot2 build layers in the order they’re written, so it’s advised to build the subgroups in order from “from deepest to shallowest”, with subgroups2 first (ending on subgroup)

  • Add geom_treemap_subgroup2_border()

    • Set the size and color
  • Add geom_treemap_subgroup2_text()

    • place: this controls where the boxes start
    • color: text color
    • min.size: the minimum font size (when re-sizing)
    • alpha: opacity
    • fontface: itlalic/bond
    • family: "sans"/"sansserif"/"mono"

See full list of arguments here.

Code
ggp2_tm_subgroup <- ggp2_treemap +
    treemapify::geom_treemap_subgroup2_border(
           color = "#282b2d",
           size = 4.0) + 
       treemapify::geom_treemap_subgroup2_text(
           place = "center",
           alpha = 0.65,
           color = "#282b2d",
           min.size = 0,
           family = "sans")
ggp2_tm_subgroup +
  labs_treemap

SUBGROUP 2:

Add geom_treemap_subgroup_border()

  • Set the size and color

Add geom_treemap_subgroup_text()

  • place: this controls where the boxes start
  • color: text color
  • min.size: the minimum font size (when re-sizing)
  • alpha: opacity
  • fontface: itlalic/bond
  • family: "sans"/"sansserif"/"mono"
Code
ggp2_tm_subgroup2 <- ggp2_tm_subgroup + 
       treemapify::geom_treemap_subgroup_border(
           color = "#ffffff", 
           size = 4) + 
       treemapify::geom_treemap_subgroup_text(
           place = "topleft",
           alpha = 0.65,
           grow = TRUE,
           color = "#ffffff",
           min.size = 0,
           family = "mono",
           fontface = "bold")

ggp2_tm_subgroup2 + 
  labs_treemap