-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathintro_02.Rmd
363 lines (285 loc) · 11.4 KB
/
intro_02.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
---
title: "Create/Modify Parts"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(gt)
library(tidyverse)
```
# The *Create/Modify Parts* Family of Functions
A **gt** table can contain a few useful parts for conveying additional information. These include a header (with a titles and subtitle), a footer (with footnotes and source notes), and additional areas for labels (row group labels, column spanner labels, the stubhead label). We can modify the look of table parts more generally with `tab_options()` and perform styling on targeted table locations with `tab_style()`.
### `tab_header()`: Add a table header
We can add a table header to the gt table with a title and even a subtitle. A table header is an optional table part that is positioned above the column labels. We have the flexibility to use Markdown formatting for the header's title and subtitle. Furthermore, if the table is intended for HTML output, we can use HTML in either of the title or subtitle.
##### EXAMPLE
Use `gtcars` to create a **gt** table; add a header part to contain a `title` and `subtitle`.
```{r}
gtcars %>%
dplyr::select(mfr, model, msrp) %>%
dplyr::slice(1:5) %>%
gt() %>%
tab_header(
title = md("Data listing from **gtcars**"),
subtitle = md("`gtcars` is an R dataset")
)
```
---------------------------------------------------------------------
### `tab_spanner()`: Add a spanner column label
Set a spanner column label by mapping it to columns already in the table. This label is placed above one or more column labels, spanning the width of those columns and column labels.
##### EXAMPLE
Use `gtcars` to create a **gt** table; Group several columns related to car performance under a spanner column with the label `performance`.
```{r}
gtcars %>%
dplyr::select(
-mfr, -trim, bdy_style, drivetrain,
-drivetrain, -trsmn, -ctry_origin
) %>%
dplyr::slice(1:8) %>%
gt(rowname_col = "model") %>%
tab_spanner(
label = "performance",
columns = c(hp, hp_rpm, trq, trq_rpm, mpg_c, mpg_h)
)
```
---------------------------------------------------------------------
### `tab_spanner_delim()`: Create group names and column labels via delimited names
This function will split selected delimited column names such that the first components (LHS) are promoted to being spanner column labels, and the secondary components (RHS) will become the column labels. Please note that reference to individual columns must continue to be the column names from the input table data (which are unique by necessity).
##### EXAMPLE
Use `iris` to create a gt table; split any columns that are dot-separated between column spanner labels (first part) and column labels (second part).
```{r}
iris %>%
dplyr::group_by(Species) %>%
dplyr::slice(1:4) %>%
gt() %>%
tab_spanner_delim(delim = ".")
```
---------------------------------------------------------------------
### `tab_row_group()`: Add a row group
Create a row group with a collection of rows. This requires specification of the rows to be included, either by supplying row labels, row indices, or through use of a select helper function like `starts_with()`.
##### EXAMPLES
Use `gtcars` to create a **gt** table and add two row groups with the labels: `numbered` and `NA` (a group without a title, or, the rest).
```{r}
gtcars %>%
dplyr::select(model, year, hp, trq) %>%
dplyr::slice(1:8) %>%
gt(rowname_col = "model") %>%
tab_row_group(
label = "numbered",
rows = matches("^[0-9]")
)
```
---------------------------------------------------------------------
Use `gtcars` to create a **gt** table. Add two row groups with the labels `powerful` and `super powerful`: the distinction being `hp` lesser or greater than `600`.
```{r}
gtcars %>%
dplyr::select(model, year, hp, trq) %>%
dplyr::slice(1:8) %>%
gt(rowname_col = "model") %>%
tab_row_group(
label = "powerful",
rows = hp <= 600
) %>%
tab_row_group(
label = "super powerful",
rows = hp > 600
)
```
---------------------------------------------------------------------
### `tab_stubhead()`: Add label text to the stubhead
Add a label to the stubhead of a **gt** table. The stubhead is the lone element that is positioned left of the column labels, and above the stub. If a stub does not exist, then there is no stubhead (so no change will be made when using this function in that case). We have the flexibility to use Markdown formatting for the stubhead label. Furthermore, if the table is intended for HTML output, we can use HTML for the stubhead label.
##### EXAMPLE
Use `gtcars` to create a **gt** table. Add a stubhead label to describe what is in the stub.
```{r}
gtcars %>%
dplyr::select(model, year, hp, trq) %>%
dplyr::slice(1:5) %>%
gt(rowname_col = "model") %>%
tab_stubhead(label = "car")
```
---------------------------------------------------------------------
### `tab_footnote()`: Add a table footnote
The `tab_footnote()` function can make it a painless process to add a footnote to a **gt** table. There are two components to a footnote: (1) a footnote mark that is attached to the targeted cell text, and (2) the footnote text (that starts with the corresponding footnote mark) that is placed in the table's footer area. Each call of `tab_footnote()` will add a different note, and one or more cells can be targeted via the location helper functions (e.g., `cells_body()`, `cells_column_labels()`, etc.).
##### EXAMPLE
Use `sza` to create a **gt** table; color the `sza` column using the `data_color()` function, then, add a footnote to the `sza` column label explaining what the color scale signifies.
```{r}
sza %>%
dplyr::filter(
latitude == 20 &
month == "jan" &
!is.na(sza)
) %>%
dplyr::select(-latitude, -month) %>%
gt() %>%
data_color(
columns = sza,
colors = scales::col_numeric(
palette = c("white", "yellow", "navyblue"),
domain = c(0, 90)
)
) %>%
tab_footnote(
footnote = "Color indicates height of sun.",
locations = cells_column_labels(columns = sza)
)
```
---------------------------------------------------------------------
### `tab_source_note()`: Add a source note citation
Add a source note to the footer part of the gt table. A source note is useful for citing the data included in the table. Several can be added to the footer, simply use multiple calls of `tab_source_note()` and they will be inserted in the order provided. We can use Markdown formatting for the note, or, if the table is intended for HTML output, we can include HTML formatting.
##### EXAMPLE
Use `gtcars` to create a **gt** table. Add a source note to the table footer that cites the data source.
```{r}
gtcars %>%
dplyr::select(mfr, model, msrp) %>%
dplyr::slice(1:5) %>%
gt() %>%
tab_source_note(
source_note = "From edmunds.com"
)
```
---------------------------------------------------------------------
### `tab_style()`: Add custom styles to one or more cells
With the `tab_style()` function we can target specific cells and apply styles to them. This is best done in conjunction with the helper functions `cell_text()`, `cell_fill()`, and `cell_borders()`. At present this function is focused on the application of styles for HTML output only (as such, other output formats will ignore all `tab_style()` calls). Using the aforementioned helper functions, here are some of the styles we can apply:
- the background color of the cell (`cell_fill()`: `color`)
- the cell's text color, font, and size (`cell_text()`: `color`, `font`, `size`)
- the text style (`cell_text()`: `style`), enabling the use of italics or oblique text.
- the text weight (`cell_text()`: `weight`), allowing the use of thin to bold text (the degree of choice is greater with variable fonts)
- the alignment and indentation of text (`cell_text()`: `align` and `indent`)
- the cell borders (`cell_borders()`)
##### EXAMPLES
Use `exibble` to create a **gt** table. Add styles that are to be applied to data cells that satisfy a condition (using `tab_style()`).
```{r}
exibble %>%
dplyr::select(num, currency) %>%
gt() %>%
fmt_number(
columns = c(num, currency),
decimals = 1
) %>%
tab_style(
style = list(
cell_fill(color = "lightcyan"),
cell_text(weight = "bold")
),
locations = cells_body(
columns = num,
rows = num >= 5000
)
) %>%
tab_style(
style = list(
cell_fill(color = "#F9E3D6"),
cell_text(style = "italic")
),
locations = cells_body(
columns = currency,
rows = currency < 100
)
)
```
---------------------------------------------------------------------
Use `sp500` to create a **gt** table. Color entire rows of cells based on values in a particular column.
```{r}
sp500 %>%
dplyr::filter(
date >= "2015-12-01" &
date <= "2015-12-15"
) %>%
dplyr::select(-c(adj_close, volume)) %>%
gt() %>%
tab_style(
style = cell_fill(color = "lightgreen"),
locations = cells_body(
columns = everything(),
rows = close > open
)
) %>%
tab_style(
style = list(
cell_fill(color = "tomato"),
cell_text(color = "white")
),
locations = cells_body(
columns = everything(),
rows = open > close
)
)
```
---------------------------------------------------------------------
### `tab_options()`: Modify the table output options
Modify the options available in a table. These options are named by the components, the subcomponents, and the element that can adjusted.
##### EXAMPLES
Use `exibble` to create a **gt** table with all the main parts added; we can use this going forward to demo some `tab_options()`.
```{r}
tab_1 <-
exibble %>%
dplyr::select(
-c(fctr, date, time, datetime)
) %>%
gt(
rowname_col = "row",
groupname_col = "group"
) %>%
tab_header(
title = md("Data listing from **exibble**"),
subtitle = md("`exibble` is an R dataset")
) %>%
fmt_number(columns = num) %>%
fmt_currency(columns = currency) %>%
tab_footnote(
footnote = "Using commas for separators.",
locations = cells_body(
columns = num,
rows = num > 1000
)
) %>%
tab_footnote(
footnote = "Using commas for separators.",
locations = cells_body(
columns = currency,
rows = currency > 1000
)
) %>%
tab_footnote(
footnote = "Alphabetical fruit.",
locations = cells_column_labels(columns = char)
)
```
Modify the table width to `100%` (which spans the entire content width area).
```{r}
tab_1 %>%
tab_options(
table.width = pct(100)
)
```
---------------------------------------------------------------------
Modify the table's background color to be "lightcyan".
```{r}
tab_1 %>%
tab_options(
table.background.color = "lightcyan"
)
```
---------------------------------------------------------------------
Use letters as the marks for footnote references. Also, separate footnotes in the footer by spaces instead of newlines.
```{r}
tab_1 %>%
tab_options(
footnotes.marks = letters,
footnotes.multiline = FALSE
)
```
---------------------------------------------------------------------
Change the padding of data rows to `5px`.
```{r}
tab_1 %>% tab_options(data_row.padding = px(5))
```
---------------------------------------------------------------------
Reduce the size of the title and the subtitle text.
```{r}
tab_1 %>%
tab_options(
heading.title.font.size = "small",
heading.subtitle.font.size = "small"
)
```
---------------------------------------------------------------------