This post explains how to customize the title and subtitle 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(
Country = c("USA", "China", "India"),
Capitals = c("Washington D.C.", "Beijing", "New Delhi"),
Population = c(331, 1441, 1393),
GDP = c(21.43, 14.34, 2.87)
)
# create and display the gt table (equivalent to "gt(data)")
data %>%
gt()
Country | Capitals | Population | GDP |
---|---|---|---|
USA | Washington D.C. | 331 | 21.43 |
China | Beijing | 1441 | 14.34 |
India | New Delhi | 1393 | 2.87 |
The most practical and intuitive way to create a gt table is to use
it in combination of the dplyr
package.
We define a simple dataset and pass it to the gt()
function.
Thanks to the tab_header()
function, we can super easily
add a title. And with the md()
function we can write it in
markdown.
library(gt)
library(dplyr)
# create and display the gt table
data %>%
gt() %>%
tab_header(title = md("Some **title**"))
Some title | |||
Country | Capitals | Population | GDP |
---|---|---|---|
USA | Washington D.C. | 331 | 21.43 |
China | Beijing | 1441 | 14.34 |
India | New Delhi | 1393 | 2.87 |
Thanks to the tab_header()
function, we can super easily
add title. And with the html()
function we can write the
text in HTML.
library(gt)
library(dplyr)
# create and display the gt table
data %>%
gt() %>%
tab_header(title = html("<span style='color:red;'>A red title</span>"))
A red title | |||
Country | Capitals | Population | GDP |
---|---|---|---|
USA | Washington D.C. | 331 | 21.43 |
China | Beijing | 1441 | 14.34 |
India | New Delhi | 1393 | 2.87 |
Still with the tab_header()
function, we can combine
title and subtitle by just adding a subtitle argument:
library(gt)
library(dplyr)
# create and display the gt table
data %>%
gt() %>%
tab_header(title = html("<span style='color:red;'>A <strong>red</strong> title</span>"),
subtitle = md("This text will be *below the title* and is written in `markdown`"))
A red title | |||
This text will be below the title and is written in markdown |
|||
Country | Capitals | Population | GDP |
---|---|---|---|
USA | Washington D.C. | 331 | 21.43 |
China | Beijing | 1441 | 14.34 |
India | New Delhi | 1393 | 2.87 |
In this example, we’ll add an image (the R logo) in the subtitle
using html formatting. In this case, we also need the
htmltools
package.
library(gt)
library(dplyr)
library(htmltools)
# create and display the gt table
data %>%
gt() %>%
tab_header(title = html("<span style='color:red;'>A <strong>red</strong> title</span>"),
subtitle = tagList(
tags$div(style = css(`text-align` = "center"),
HTML(web_image("https://www.r-project.org/logo/Rlogo.png")
)
)
)
)
A red title | |||
![]() |
|||
Country | Capitals | Population | GDP |
---|---|---|---|
USA | Washington D.C. | 331 | 21.43 |
China | Beijing | 1441 | 14.34 |
India | New Delhi | 1393 | 2.87 |
We now know how to customize your table titles 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.