/
gt.Rmd
218 lines (173 loc) · 5.86 KB
/
gt.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
---
title: "nflplotR & the gt Package"
description: "How nflplotR works as gt extension"
author: "Sebastian Carl"
opengraph:
image:
src: "https://raw.githubusercontent.com/nflverse/nflplotR/main/man/figures/social_preview_gt.png"
twitter:
card: summary_large_image
creator: "@mrcaseb"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{nflplotR & the gt Package}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
out.width = "100%",
dpi = 600
)
options(nflreadr.cache_warning = FALSE)
options(warn = -1)
```
The R package [{gt}](https://gt.rstudio.com/) is becoming increasingly popular for creating aesthetically pleasing tables. nflplotR supports rendering of team logos, team wordmarks, and player headshots in gt tables similar to ggplot2. This article will provide some typical examples.
## Team Logos & Wordmarks
The functions `gt_nfl_logos()` and `gt_nfl_wordmarks()` come with a powerful `locations` argument that allows usage of gt selection helpers. We will create an example dataframe to show how this all works.
```{r}
df <- data.frame(
row_group_column = c("AFC", "NFC", "AFC", "NFC"),
row_name_column = c("LAC", "SEA"),
column_a = 11:12,
column_b = c("KC", "LA")
)
```
Our example dataframe in a gt table without any formatting.
```{r}
gt::gt(df)
```
The column `row_group_column` is intended to serve as row group variable so let's apply this.
```{r}
gt::gt(df, groupname_col = "row_group_column")
```
We also would like to render images in the stub, i.e. the rownames so we tell gt about the `row_name_column`.
```{r}
example_table <- gt::gt(
df,
groupname_col = "row_group_column",
rowname_col = "row_name_column"
) |>
# align the complete table left
gt::tab_options(
table.align = "left"
)
example_table
```
This is our final table. We have valid NFL abbreviations in the cell body, in row group labels and in the stub. We can now use nflplotR to render images instead of those abbreviations.
### Cell Body
To render images in the cell body, i.e. the rows of the table, we can either use the `columns` argument or the appropriate `locations` helper.
```{r}
example_table |>
nflplotR::gt_nfl_logos(columns = "column_b")
```
Please note, that the locations helper will allow you to selectively apply the function to a set of rows
```{r}
example_table |>
nflplotR::gt_nfl_logos(locations = gt::cells_body(rows = gt::starts_with("LAC")))
```
### Row Group Label
Rendering images outside of the cell body will always require the appropriate call to the `locations` argument. The `columns` argument cannot handle anything outside cell bodies.
```{r}
example_table |>
nflplotR::gt_nfl_logos(locations = gt::cells_row_groups())
```
### Stub
Now we would like to convert rownames to images.
```{r}
example_table |>
nflplotR::gt_nfl_wordmarks(locations = gt::cells_stub())
```
### Combine all together
The `locations` argument allows multiple locations in one call by wrapping them in a list.
```{r}
example_table |>
nflplotR::gt_nfl_wordmarks(locations = gt::cells_stub()) |>
nflplotR::gt_nfl_logos(
locations = list(
gt::cells_body(), gt::cells_row_groups()
)
)
```
### How about Column Labels?
Well...it's complicated, because [gt behaves inconsistent in my opinion](https://github.com/rstudio/gt/issues/1433).
The actually correct way would be a call to `nflplotR::gt_nfl_logos` or `nflplotR::gt_nfl_wordmarks` with the `locations` argument set to `gt::cells_column_labels()`. Currently, this wouldn't render any images in column labels as discussed in the above linked issue.
However, as a convenient workaround, nflplotR supports logos and wordmarks in column labels through `gt_nfl_cols_label()`.
LOGOS:
```{r}
teams <- nflplotR::valid_team_names() |> head(6)
df <- cbind(1, 2, 3, 4, 5, 6) |>
as.data.frame() |>
rlang::set_names(teams)
gt::gt(df) |>
nflplotR::gt_nfl_cols_label() |>
# align the complete table left
gt::tab_options(
table.align = "left"
)
```
WORDMARKS (note how non matches remain unchanged):
```{r}
gt::gt(df) |>
nflplotR::gt_nfl_cols_label(type = "wordmark") |>
# align the complete table left
gt::tab_options(
table.align = "left"
)
```
HEADSHOTS:
```{r}
headshot_df <- data.frame(
"00-0036355" = 1,
"00-0033873" = 2,
check.names = FALSE
)
gt::gt(headshot_df) |>
nflplotR::gt_nfl_cols_label(type = "headshot") |>
# align the complete table left
gt::tab_options(
table.align = "left"
)
```
### Logos and Wordmarks Rendered by nflplotR
This example creates a table that renders all team logos and wordmarks. We split the table into 2 x 16 rows to avoid an overly long table and convert all variables starting with "logo" to logos and all variables starting with "wordmark" to wordmarks.
```{r}
teams <- nflplotR::valid_team_names()
# remove conference logos from this example
teams <- teams[!teams %in% c("AFC", "NFC", "NFL")]
# create dataframe with all 32 team names
df <- data.frame(
team_a = head(teams, 16),
logo_a = head(teams, 16),
wordmark_a = head(teams, 16),
team_b = tail(teams, 16),
logo_b = tail(teams, 16),
wordmark_b = tail(teams, 16)
)
# create gt table and translate team names to logo/wordmark images
df |>
gt::gt() |>
nflplotR::gt_nfl_logos(columns = gt::starts_with("logo")) |>
nflplotR::gt_nfl_wordmarks(columns = gt::starts_with("wordmark")) |>
# align the complete table left
gt::tab_options(
table.align = "left"
)
```
## Player Headshots
All of the above applies to `gt_nfl_headshots()` as well. All you need is a gsis ID.
```{r}
df <- data.frame(
A = c("00-0036355", "00-0033873"),
B = c("00-0033077", "00-0035228")
)
df |>
gt::gt() |>
nflplotR::gt_nfl_headshots(columns = gt::everything(), height = 50) |>
# align the complete table left
gt::tab_options(
table.align = "left"
)
```