The R package {gt} 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.
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.
gt::gt(df)| row_group_column | row_name_column | column_a | column_b |
|---|---|---|---|
| AFC | LAC | 11 | KC |
| NFC | SEA | 12 | LA |
| AFC | LAC | 11 | KC |
| NFC | SEA | 12 | LA |
The column row_group_column is intended to serve as row
group variable so let’s apply this.
gt::gt(df, groupname_col = "row_group_column")| row_name_column | column_a | column_b |
|---|---|---|
| AFC | ||
| LAC | 11 | KC |
| LAC | 11 | KC |
| NFC | ||
| SEA | 12 | LA |
| SEA | 12 | LA |
We also would like to render images in the stub, i.e. the rownames so
we tell gt about the row_name_column.
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| column_a | column_b | |
|---|---|---|
| AFC | ||
| LAC | 11 | KC |
| LAC | 11 | KC |
| NFC | ||
| SEA | 12 | LA |
| SEA | 12 | LA |
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.
example_table |>
nflplotR::gt_nfl_logos(columns = "column_b")| column_a | column_b | |
|---|---|---|
| AFC | ||
| LAC | 11 | |
| LAC | 11 | |
| NFC | ||
| SEA | 12 | |
| SEA | 12 | |
Please note, that the locations helper will allow you to selectively apply the function to a set of rows
example_table |>
nflplotR::gt_nfl_logos(locations = gt::cells_body(rows = gt::starts_with("LAC")))| column_a | column_b | |
|---|---|---|
| AFC | ||
| LAC | 11 | |
| LAC | 11 | |
| NFC | ||
| SEA | 12 | LA |
| SEA | 12 | LA |
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.
example_table |>
nflplotR::gt_nfl_logos(locations = gt::cells_row_groups())| column_a | column_b | |
|---|---|---|
| LAC | 11 | KC |
| LAC | 11 | KC |
| SEA | 12 | LA |
| SEA | 12 | LA |
Stub
Now we would like to convert rownames to images.
example_table |>
nflplotR::gt_nfl_wordmarks(locations = gt::cells_stub())| column_a | column_b | |
|---|---|---|
| AFC | ||
| 11 | KC | |
| 11 | KC | |
| NFC | ||
| 12 | LA | |
| 12 | LA | |
Column Spanners
We use another table to demonstrate how to convert team names to logos and wordmarks in column spanners.
Here only logos
df <- data.frame(a = 3, b = 4, c = 5, d = 6)
df |>
gt::gt() |>
gt::tab_spanner("KC", c(a, b)) |>
gt::tab_spanner("LAC", c(c, d)) |>
nflplotR::gt_nfl_logos(locations = gt::cells_column_spanners())|
|
|
||
|---|---|---|---|
| a | b | c | d |
| 3 | 4 | 5 | 6 |
And now mix logo and wordmark
df <- data.frame(a = 3, b = 4, c = 5, d = 6)
df |>
gt::gt() |>
gt::tab_spanner("KC", c(a, b)) |>
gt::tab_spanner("LAC", c(c, d)) |>
nflplotR::gt_nfl_logos(locations = gt::cells_column_spanners("KC")) |>
nflplotR::gt_nfl_wordmarks(locations = gt::cells_column_spanners("LAC"))|
|
|
||
|---|---|---|---|
| a | b | c | d |
| 3 | 4 | 5 | 6 |
Combine all together
The locations argument allows multiple locations in one
call by wrapping them in a list.
example_table |>
nflplotR::gt_nfl_wordmarks(locations = gt::cells_stub()) |>
nflplotR::gt_nfl_logos(
locations = list(
gt::cells_body(), gt::cells_row_groups()
)
)