This post explains how to customize the footer in a table with the gt package. It provides several reproducible examples with explanation and R code.
gt
packageThe gt package is an excellent way to create and customize nice table output in R. You can read more about this package on its github page. You can install it directly from the CRAN by running the following:
Tables from gt
are sober but highly customizable
library(gt)
library(dplyr)
# dataset
data = data.frame(
Planet = c("Earth", "Mars", "Jupiter", "Venus"),
Moons = c(1, 2, 79, 0),
Distance_from_Sun = c(149.6, 227.9, 778.3, 108.2),
Diameter = c(12742, 6779, 139822, 12104)
)
# create and display the gt table (equivalent to "gt(data)")
data %>%
gt()
Planet | Moons | Distance_from_Sun | Diameter |
---|---|---|---|
Earth | 1 | 149.6 | 12742 |
Mars | 2 | 227.9 | 6779 |
Jupiter | 79 | 778.3 | 139822 |
Venus | 0 | 108.2 | 12104 |
In this example, we now indicate the footer with letters (in upper
case) thanks to the opt_footnote_marks()
function.
library(gt)
library(dplyr)
# create and display the gt table
data %>%
gt() %>%
tab_footnote(footnote = md("Measured in **millions** of Km"),
locations = cells_column_labels(columns = Distance_from_Sun)) %>%
tab_footnote(footnote = md("Measured in **Km**"),
locations = cells_column_labels(columns = Diameter)) %>%
tab_footnote(footnote = md("The original data are from *Some Organization*")) %>%
opt_footnote_marks(marks = "LETTERS")
Planet | Moons | Distance_from_SunA | DiameterB |
---|---|---|---|
Earth | 1 | 149.6 | 12742 |
Mars | 2 | 227.9 | 6779 |
Jupiter | 79 | 778.3 | 139822 |
Venus | 0 | 108.2 | 12104 |
The original data are from Some Organization | |||
A Measured in millions of Km | |||
B Measured in Km |
We now know how to customize your table footer with the gt package. There is much more you can do using this package, so feel free to visit the gt table section of the gallery to learn more about it and check other examples.
👋 After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! 🔥