-
Notifications
You must be signed in to change notification settings - Fork 18
/
README.Rmd
181 lines (147 loc) · 7.05 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
---
output:
github_document:
html_preview: true
fig_height: 6
fig_width: 6
---
```{r, echo = FALSE}
library(knitr)
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-"
)
```
<!-- badges: start -->
[![R-CMD-check](https://github.com/wilkox/treemapify/workflows/R-CMD-check/badge.svg)](https://github.com/wilkox/treemapify/actions)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/treemapify)](https://cran.r-project.org/package=treemapify)
[![lifecycle](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)
# treemapify
treemapify provides [ggplot2](https://ggplot2.tidyverse.org) geoms for drawing [treemaps](https://en.wikipedia.org/wiki/Treemap).
## Installation
Install the release version of treemapify from CRAN:
`install.packages("treemapify")`
If you want the development version, install it from GitHub:
`devtools::install_github("wilkox/treemapify")`
## The G20 dataset
treemapify includes an example dataset containing statistics about the G-20
group of major world economies.
```{r message = FALSE}
library(ggplot2)
library(treemapify)
G20
```
## Drawing a simple treemap
In a treemap, each tile represents a single observation, with the area of the
tile proportional to a variable. Let's start by drawing a treemap with each tile
representing a G-20 country. The area of the tile will be mapped to the
country's GDP, and the tile's fill colour mapped to its HDI (Human Development
Index). `geom_treemap()` is the basic geom for this purpose.
```{r basic_treemap}
ggplot(G20, aes(area = gdp_mil_usd, fill = hdi)) +
geom_treemap()
```
This plot isn't very useful without the knowing what country is represented by
each tile. `geom_treemap_text()` can be used to add a text label to each tile. It
uses the [ggfittext](https://github.com/wilkox/ggfittext) package to resize
the text so it fits the tile. In addition to standard text formatting aesthetics
you would use in `geom_text()`, like `fontface` or `colour`, we can pass
additional options specific for ggfittext. For example, we can place the text
in the centre of the tile with `place = "centre"`, and expand it to fill as much
of the tile as possible with `grow = TRUE`.
```{r geom_treemap_text}
ggplot(G20, aes(area = gdp_mil_usd, fill = hdi, label = country)) +
geom_treemap() +
geom_treemap_text(fontface = "italic", colour = "white", place = "centre",
grow = TRUE)
```
## Subgrouping tiles
`geom_treemap()` supports subgrouping of tiles within a treemap by passing a
`subgroup` aesthetic. Let's subgroup the countries by region, draw a border
around each subgroup with `geom_treemap_subgroup_border()`, and label each
subgroup with `geom_treemap_subgroup_text()`. `geom_treemap_subgroup_text()`
takes the same arguments for text placement and resizing as
`geom_treemap_text()`.
```{r subgrouped_treemap}
ggplot(G20, aes(area = gdp_mil_usd, fill = hdi, label = country,
subgroup = region)) +
geom_treemap() +
geom_treemap_subgroup_border() +
geom_treemap_subgroup_text(place = "centre", grow = T, alpha = 0.5, colour =
"black", fontface = "italic", min.size = 0) +
geom_treemap_text(colour = "white", place = "topleft", reflow = T)
```
Note that Argentina is not labelled. `geom_treemap_text()` will hide text
labels that cannot fit a tile without being shrunk below a minimum size, by
default 4 points. This can be adjusted with the `min.size` argument.
Up to three nested levels of subgrouping are supported with the `subgroup2` and
`subgroup3` aesthetics. Borders and text labels for these subgroups can be
drawn with `geom_treemap_subgroup2_border()`, etc. Note that ggplot2 draws plot
layers in the order that they are added. This means it is possible to
accidentally hide one layer of subgroup borders with another. Usually, it's
best to add the border layers in order from deepest to shallowest, i.e.
`geom_treemap_subgroup3_border()` then `geom_treemap_subgroup2_border()` then
`geom_treemap_subgroup_border()`.
```{r multiple_subgrouped_treemap}
ggplot(G20, aes(area = 1, label = country, subgroup = hemisphere,
subgroup2 = region, subgroup3 = econ_classification)) +
geom_treemap() +
geom_treemap_subgroup3_border(colour = "blue", size = 1) +
geom_treemap_subgroup2_border(colour = "white", size = 3) +
geom_treemap_subgroup_border(colour = "red", size = 5) +
geom_treemap_subgroup_text(place = "middle", colour = "red", alpha = 0.5, grow = T) +
geom_treemap_subgroup2_text(colour = "white", alpha = 0.5, fontface = "italic") +
geom_treemap_subgroup3_text(place = "top", colour = "blue", alpha = 0.5) +
geom_treemap_text(colour = "white", place = "middle", reflow = T)
```
As demonstrated, there is no assurance that the resulting plot will look good.
Like any ggplot2 plot, treemapify plots can be faceted, scaled, themed, etc.
```{r complex_treemap}
ggplot(G20, aes(area = gdp_mil_usd, fill = region, label = country, subgroup = region)) +
geom_treemap() +
geom_treemap_text(grow = T, reflow = T, colour = "black") +
facet_wrap( ~ hemisphere) +
scale_fill_brewer(palette = "Set1") +
theme(legend.position = "bottom") +
labs(
title = "The G-20 major economies by hemisphere",
caption = "The area of each tile represents the country's GDP as a
proportion of all countries in that hemisphere",
fill = "Region"
)
```
## Animated treemaps
The default algorithm for laying out the tiles is the 'squarified' algorithm.
This tries to minimise the tiles' aspect ratios, making sure there are no long
and flat or tall and skinny tiles. While 'squarified' treemaps are aesthetically
pleasing, the downside is that the position of tiles within the plot area can
change dramatically with even small changes to the dataset. This makes it
difficult to compare treemaps side-by-side, or create animated treemaps.
By providing the `layout = "fixed"` option to treemapify geoms, an alternative
layout algorithm is used that will always position the tiles based on the order
of observations in the data frame. It's very important that the same value for
`layout` is passed to all treemapify geoms, otherwise different layers of the
plot might not share the same layout.
With the help of `layout = "fixed"`, and with the
[`gganimate`](https://github.com/thomasp85/gganimate) package, it becomes possible
to create animated treemaps showing e.g. change over time.
```{r animated treemap, message = FALSE, results = FALSE}
library(gganimate)
library(gapminder)
p <- ggplot(gapminder, aes(
label = country,
area = pop,
subgroup = continent,
fill = lifeExp
)) +
geom_treemap(layout = "fixed") +
geom_treemap_text(layout = "fixed", place = "centre", grow = TRUE, colour = "white") +
geom_treemap_subgroup_text(layout = "fixed", place = "centre") +
geom_treemap_subgroup_border(layout = "fixed") +
transition_time(year) +
ease_aes('linear') +
labs(title = "Year: {frame_time}")
anim_save("man/figures/animated_treemap.gif", p, nframes = 48)
```
![An example of an animated treemap](man/figures/animated_treemap.gif)