Treemaps
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
::install_github("wilkox/treemapify")
devtoolsinstall.packages("palmerpenguins")
library(treemapify)
library(palmerpenguins)
library(ggplot2)
DATA:
Filter the missing values from sex
, group the data by species
, island
, and sex
, then count the species
and island
(then ungroup()
).
Code
<- penguins |>
treemap_peng ::select(species, island, sex) |>
dplyr::drop_na() |>
tidyr::group_by(species, island, sex) |>
dplyr::count(species, island, sex) |>
dplyrungroup()
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
toarea
,sex
tofill
andlabel
,species
tosubgroup
, andisland
tosubgroup2
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(
labs_treemap title = "Adult foraging penguins",
fill = "Sex")
<- ggplot(treemap_peng,
ggp2_treemap aes(area = n,
fill = sex,
label = sex,
subgroup = species,
subgroup2 = island)) +
::geom_treemap() +
treemapify::geom_treemap_text(
treemapifyplace = "bottomright",
color = "#d0d0d0",
min.size = 0,
alpha = 0.90,
fontface = "italic",
family = "sans")
+
ggp2_treemap labs_treemap
GRAPH:
Code
<- labs(
labs_treemap title = "Adult foraging penguins",
fill = "Sex")
<- ggplot(treemap_peng,
ggp2_treemap aes(area = n,
fill = sex,
label = sex,
subgroup = species,
subgroup2 = island)) +
::geom_treemap() +
treemapify::geom_treemap_text(
treemapifyplace = "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
andcolor
- Set the
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_treemap +
ggp2_tm_subgroup ::geom_treemap_subgroup2_border(
treemapifycolor = "#282b2d",
size = 4.0) +
::geom_treemap_subgroup2_text(
treemapifyplace = "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
andcolor
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_subgroup +
ggp2_tm_subgroup2 ::geom_treemap_subgroup_border(
treemapifycolor = "#ffffff",
size = 4) +
::geom_treemap_subgroup_text(
treemapifyplace = "topleft",
alpha = 0.65,
grow = TRUE,
color = "#ffffff",
min.size = 0,
family = "mono",
fontface = "bold")
+
ggp2_tm_subgroup2 labs_treemap