-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathdrilldown.Rmd
258 lines (230 loc) · 5.92 KB
/
drilldown.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
---
title: Drilldown
author: Joshua Kunst
date: "`r Sys.Date()`"
description: >
Drilldown is the concept of inspecting increasingly high resolution data
through clicking on chart items like columns, points or pie slices.
Let's see some _How tos_ use this feature.
output: rmarkdown::html_vignette
editor_options:
chunk_output_type: console
vignette: >
%\VignetteIndexEntry{Drilldown}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
<script>
$(document).ready(function(){
el = $(".page-header > h1")
icon = "arrow-down"
text = el.text()
text_new = "<span class=\"fa fa-" + icon + "\"></span> " + text
el.html(text_new)
}) ;
</script>
```{r, include=FALSE}
library(highcharter)
options(highcharter.theme = hc_theme_hcrt(tooltip = list(valueDecimals = 2)))
```
Drilldown is the concept of inspecting increasingly high resolution data
through clicking on chart items like columns, points or pie slices.
## Exmaple I: Gapminder data
```{r}
library(dplyr)
library(forcats)
library(purrr)
library(stringr)
data(gapminder, package = "gapminder")
gapminder2007 <- gapminder |>
filter(year == max(year)) |>
select(-year) |>
mutate(pop = pop/1e6) |>
arrange(desc(pop))
gapminder_column <- gapminder2007 |>
group_by(continent) |>
summarise(
lifeExp = weighted.mean(lifeExp, pop),
gdpPercap = weighted.mean(gdpPercap, pop),
pop = sum(pop)
) |>
mutate_if(is.numeric, round) |>
arrange(desc(pop)) |>
mutate(continent = fct_inorder(continent))
gapminder_column
gapminder_drilldown <- gapminder2007 |>
group_nest(continent) |>
mutate(
id = continent,
type = "column",
# in the drilldown we'll give the mapping via creating the columns
data = map(data, mutate, name = country, y = pop),
data = map(data, list_parse)
)
gapminder_drilldown
```
The data is ready. Now, in this example due the continents and countries have
the sames values (pop, lifeExp, ...) we can use the same `pointFormat` for
the tooltips:
```{r}
x <- c("Population (MM)", "Life expectancy at birth", "GDP per capita (US$)")
y <- c("{point.pop}", "{point.lifeExp}", "$ {point.gdpPercap}")
tt <- tooltip_table(x, y)
hchart(
gapminder_column,
"column",
hcaes(x = continent, y = pop, name = continent, drilldown = continent),
name = "Population",
colorByPoint = TRUE
) |>
hc_drilldown(
allowPointDrilldown = TRUE,
series = list_parse(gapminder_drilldown)
) |>
hc_tooltip(
pointFormat = tt, # "{point.name} {point.pop}"
useHTML = TRUE,
valueDecimals = 0
) |>
hc_yAxis(
title = list(text = "Population in millions (log scale)"),
type = "logarithmic",
minorTickInterval = 'auto'
) |>
hc_xAxis(
title = ""
)
```
## Exmaple II: Pokémon data
Same recipe, different data. Just copy & pasting code:
```{r}
pkmn_min <- pokemon |>
count(type_1, color = type_1_color) |>
mutate(type_1 = fct_reorder(type_1, .x = n)) |>
arrange(desc(type_1))
pkmn_ddn <- pokemon |>
count(type_1, type_2, color = type_mix_color) |>
arrange(type_1, desc(n)) |>
mutate(type_2 = ifelse(is.na(type_2), str_c("only ", type_1), type_2)) |>
group_nest(type_1) |>
mutate(
id = type_1,
type = "column",
# in the drilldown we'll give the mapping via creating the columns
data = map(data, mutate, name = type_2, y = n),
data = map(data, list_parse)
)
hchart(
pkmn_min,
type = "column",
hcaes(x = type_1, y = n, color = color, drilldown = type_1),
name = "Pokémons"
) |>
hc_drilldown(
activeAxisLabelStyle = list(textDecoration = "none"),
allowPointDrilldown = TRUE,
series = list_parse(pkmn_ddn)
) |>
hc_yAxis(
title = list(text = ""),
endOnTick = FALSE,
opposite = TRUE
) |>
hc_xAxis(
title = list(text = ""),
endOnTick = FALSE,
gridLineWidth = 0,
tickWidth = 0
) |>
hc_chart(
style = list(fontFamily = "Gloria Hallelujah")
)
```
## Example III: Custom tooltips and colors
(Example thanks to Claire).
```{r}
dtrees <- tibble(
tree = c("A", "B"),
apples = c(5, 7),
species = c("Fuji", "Gala"),
trunk_size = c(30, 40)
) |>
# rowise is used to avoid vectorization in tags$td, ie, do it row by row
rowwise() |>
mutate(
tooltip_text = list(
tags$table(
tags$tr(tags$th("Tree"), tags$td(tree)),
tags$tr(tags$th("# Apples"), tags$td(apples))
)
)
) |>
ungroup() |>
mutate(
tooltip_text = map_chr(tooltip_text, as.character),
# clean text
tooltip_text = str_trim(str_squish(tooltip_text))
)
dflowers <- tibble(
tree = c(rep("A", 3), rep("B", 4)),
rose = c("R1", "R2", "R3", "R4", "R5", "R6", "R7"),
petals = c(10, 13, 15, 20, 24, 26, 27),
color = c(
"gray",
"#FFB6C1",
"#8B0000",
"purple",
"#FF10F0",
"#ffffbf",
"red"
),
price = c(3, 2, 4, 3.5, 5, 2.5, 4.5)
) |>
rowwise() |>
mutate(
tooltip_text = list(
tags$table(
tags$tr(tags$th("Flower"), tags$td(rose)),
tags$tr(tags$th("# Petals"), tags$td(petals)),
tags$tr(tags$th("Price"), tags$td(str_c("$ ", price)))
)
)
) |>
ungroup() |>
mutate(
tooltip_text = map_chr(tooltip_text, as.character),
# clean text
tooltip_text = str_trim(str_squish(tooltip_text))
)
dflowers_dd <- dflowers |>
group_nest(id = tree) |>
mutate(
type = "column",
data = map(data, mutate, name = rose, y = petals),
data = map(data, list_parse),
name = "Petals"
)
hchart(
dtrees,
"column",
hcaes(tree, apples, drilldown = tree),
name = "Apples",
colorByPoint = TRUE
) |>
hc_drilldown(
breadcrumbs = list(
format = 'back to {level.name} series',
# enabled = FALSE,
showFullPath = FALSE
),
allowPointDrilldown = TRUE,
series = list_parse(dflowers_dd)
) |>
hc_yAxis(title = list(text = "")) |>
hc_xAxis(title = list(text = "")) |>
hc_tooltip(
headerFormat = "", # remove header
pointFormat = "{point.tooltip_text}",
useHTML = TRUE
)
```