data %>%
gt() %>%
tab_footnote(
footnote = "Text of footnote.",
locations = cells_column_labels(col_B)
)Format footnotes in a table for publication
You are making a table for publication, and you want to add footnotes.
Step 1 - Start with a data frame that contains the values that you want to display in the arrangement that you want to display them.
Step 2 - Use gt::gt() to turn your data frame into an editable, professional looking table.
Step 3 - Use gt::tab_footnote() to add a footnote. Set locations to a helper function to specify the cell or group of cells to associate with the footnote.
Helper functions to specify locations
You can use the following helper functions to associate cell locations with tab_footnote():
| Function | Cells targeted |
|---|---|
cells_title() |
table title or subtitle |
cells_stubhead() |
stubhead location |
cells_column_spanners() |
spanner column labels |
cells_column_labels() |
column labels |
cells_row_groups() |
row group labels |
cells_stub() |
row labels in the table stub |
cells_body() |
cells in the table body using the intersection of rows and columns |
cells_summary() |
summary cells using the groups argument |
cells_grand_summary() |
grand summary cells of table |
cells_stub_summary() |
summary row labels in the table stub |
cells_stub_grand_summary() |
grand summary row labels in table stub |
Example
store_sales describes the number of various Quaker oatmeal products sold at three stores during a week.
store_sales# A tibble: 4 × 4
product store8 store21 store44
<chr> <int> <int> <int>
1 Quaker Instant Grits 12 oz 20 12 3
2 Quaker Kids Choice 14.3 oz 3 7 11
3 Quick Quaker Oats 18 oz 55 21 53
4 Quick Quaker Oats 42 oz 123 18 52
Before we publish the table, we’d like to add a footnote. To do this, we load the gt package and then convert our data frame to a gt table. The we use tab_footnote() to add footnotes to our table.
library(gt)
store_sales %>%
gt() %>%
tab_footnote(
footnote = "Store 8 had a sale on Quick Quaker Oats 42 oz this week.",
locations = cells_body(
columns = store8,
rows = (product == "Quick Quaker Oats 42 oz")
),
placement = "right"
) %>%
tab_footnote(
footnote = "This is the smallest store of the three.",
locations = cells_column_labels(columns = store21)
)| product | store8 | store211 | store44 |
|---|---|---|---|
| Quaker Instant Grits 12 oz | 20 | 12 | 3 |
| Quaker Kids Choice 14.3 oz | 3 | 7 | 11 |
| Quick Quaker Oats 18 oz | 55 | 21 | 53 |
| Quick Quaker Oats 42 oz | 1232 | 18 | 52 |
| 1 This is the smallest store of the three. | |||
| 2 Store 8 had a sale on Quick Quaker Oats 42 oz this week. | |||
Notice that gt placed the Store 21 footnote first, even though we called it last. By default, gt numbers the footnotes in the order that they appear in the table.
Choose placement of footnotes
Set the placement argument of tab_footnotes() to left or right to choose where in the cells the footnote marks will appear.
Footnotes in SAS
Footnotes linked to specific cells are not as straightforward in SAS. The FOOTNOTE option within SAS procedures such as PRINT or TABULATE is most similar to gt::tab_source_note() function which adds a note to the footer part of the table.
In SAS:
PROC PRINT data = publish_data;
FOOTNOTE "Note beneath table.";
RUN;In R:
publish_data %>%
gt() %>%
tab_source_note(
source_note = "Note beneath table."
)