Creating beautiful tables in R with {gt}

Author

Albert Rapp

Published

November 27, 2022

Introduction

Tables don’t have to be dull lists of numbers. If done correctly, they can, in fact, be beautiful & engaging. And in this book, we’re going to learn how to create such tables with R. More precisely, we will learn how to use the {gt} package.

Chapter 1 is the starting point of our {gt} journey. It will cover useful guidelines for any table and how to implement them with {gt}. You can think of this chapter as a quickstart guide.

Afterwards, we’re learning how to add eye-catching elements like plots, icons and images to our table in Chapter 2. Many of these elements are powered through the amazing {gtExtras} package. But you will also learn how to add any plot to your table (regardless of whether it is implemented in {gtExtras} or not.)

Next, we’ll talk about formatting the entries of your table in Chapter 3. {gt} has two powerful sets of functions to get this job done, namely fmt_*() and sub_*(). These functions all work the same and are pretty easy to pick up. Hence, it’s easy to format the data in your table the way you want or need.

In Chapter 4, we’re learning how to change the theme of our table. This will be a long chapter. After all, we can basically change anything about our table’s appearance.

All of the knowledge that we’ve acquired in the previous chapters culminate in Chapter 5. That’s where we will build elaborate tables with {gt}.

Finally, Chapter 6 is a special chapter dedicated to the interactions between Quarto and {gt}. As of right now (Quarto v1.2.267 & {gt} v0.8.0), Quarto’s default theme can interact with your {gt} table and change its theme. But there are ways to isolate Quarto from {gt}. This is what you’ll learn in this chapter.

Prerequisites

This book is meant for people with an intermediate knowledge of R, specifically the Tidyverse. I do not expect people to know HTML/CSS for the most parts.

Throughout most of the book, we’ll only to do small changes with HTML/CSS and these bits will be explained. But there will be parts in Chapter 4 and the subsequent chapters that will talk about changing CSS styles. Feel free to skip these parts if you’re uncomfortable with CSS. But I’ll do my best to help you with CSS.

About this book

This book is completely free and the code is open-source. If you find the things you learn here useful, I’d appreciate it if you sponsor my work via Buy Me Coffee.

Also, if you’re interested in working with me, I’m open to freelance work. You can book an appointment with me on Calendly.

Session info

Just so you’re sure what versions I’m using. Here’s my session info and my Quarto version.

sessioninfo::session_info("installed")
## ─ Session info ───────────────────────────────────────────────────────────────
##  setting  value
##  version  R version 4.2.2 Patched (2022-11-10 r83330)
##  os       Ubuntu 20.04.5 LTS
##  system   x86_64, linux-gnu
##  ui       X11
##  language (EN)
##  collate  en_US.UTF-8
##  ctype    en_US.UTF-8
##  tz       Europe/Berlin
##  date     2022-11-27
##  pandoc   2.5 @ /usr/bin/ (via rmarkdown)
## 
## ─ Packages ───────────────────────────────────────────────────────────────────
##  ! package        * version    date (UTC) lib source
##    AsioHeaders      1.22.1-1   2022-06-14 [1] CRAN (R 4.2.1)
##    askpass          1.1        2019-01-13 [1] CRAN (R 4.0.0)
##    assertthat       0.2.1      2019-03-21 [1] CRAN (R 4.0.0)
##    backports        1.4.1      2021-12-13 [1] CRAN (R 4.1.2)
##    base64enc        0.1-3      2015-07-28 [1] CRAN (R 4.0.0)
##    bigD             0.2.0      2022-09-05 [1] CRAN (R 4.2.2)
##    bit              4.0.4      2020-08-04 [1] CRAN (R 4.0.2)
##    bit64            4.0.5      2020-08-30 [1] CRAN (R 4.0.2)
##    bitops           1.0-7      2021-04-24 [1] CRAN (R 4.0.5)
##    blob             1.2.3      2022-04-10 [1] CRAN (R 4.1.3)
##    boot             1.3-28     2021-05-03 [2] CRAN (R 4.0.5)
##    brew             1.0-8      2022-09-29 [1] CRAN (R 4.2.1)
##    brio             1.1.3      2021-11-30 [1] CRAN (R 4.2.1)
##    broom            1.0.1      2022-08-29 [1] CRAN (R 4.2.1)
##    broom.helpers    1.9.0      2022-09-23 [1] CRAN (R 4.2.1)
##    bslib            0.4.1      2022-11-02 [1] CRAN (R 4.2.2)
##    cachem           1.0.6      2021-08-19 [1] CRAN (R 4.2.0)
##    callr            3.7.3      2022-11-02 [1] CRAN (R 4.2.2)
##    cellranger       1.1.0      2016-07-27 [1] CRAN (R 4.0.0)
##    chromote         0.1.1      2022-09-07 [1] CRAN (R 4.2.1)
##    class            7.3-20     2022-01-13 [2] CRAN (R 4.1.2)
##  P cli              3.4.1      2022-09-23 [?] CRAN (R 4.2.1)
##    clipr            0.8.0      2022-02-22 [1] CRAN (R 4.1.2)
##    cluster          2.1.4      2022-08-22 [2] CRAN (R 4.2.1)
##    codetools        0.2-18     2020-11-04 [2] CRAN (R 4.0.3)
##    colorspace       2.0-3      2022-02-21 [1] CRAN (R 4.1.2)
##    commonmark       1.8.1      2022-10-14 [1] CRAN (R 4.2.2)
##    cpp11            0.4.3      2022-10-12 [1] CRAN (R 4.2.1)
##    crayon           1.5.2      2022-09-29 [1] CRAN (R 4.2.1)
##    credentials      1.3.2      2021-11-29 [1] CRAN (R 4.2.1)
##    curl             4.3.3      2022-10-06 [1] CRAN (R 4.2.1)
##    data.table       1.14.4     2022-10-17 [1] CRAN (R 4.2.2)
##    DBI              1.1.3      2022-06-18 [1] CRAN (R 4.2.1)
##    dbplyr           2.2.1      2022-06-27 [1] CRAN (R 4.2.1)
##    desc             1.4.2      2022-09-08 [1] CRAN (R 4.2.1)
##    devtools         2.4.5      2022-10-11 [1] CRAN (R 4.2.2)
##    diffobj          0.3.5      2021-10-05 [1] CRAN (R 4.2.1)
##  P digest           0.6.30     2022-10-18 [?] CRAN (R 4.2.1)
##    downlit          0.4.2      2022-07-05 [1] CRAN (R 4.2.1)
##    dplyr            1.0.10     2022-09-01 [1] CRAN (R 4.2.1)
##    dtplyr           1.2.2      2022-08-20 [1] CRAN (R 4.2.1)
##    ellipsis         0.3.2      2021-04-29 [1] CRAN (R 4.0.5)
##    emoji            15.0       2022-11-03 [1] CRAN (R 4.2.2)
##  P evaluate         0.18       2022-11-07 [?] CRAN (R 4.2.2)
##    exams            2.4-0      2022-10-17 [1] CRAN (R 4.2.2)
##    fansi            1.0.3      2022-03-24 [1] CRAN (R 4.1.3)
##    farver           2.1.1      2022-07-06 [1] CRAN (R 4.2.1)
##  P fastmap          1.1.0      2021-01-25 [?] CRAN (R 4.0.3)
##    fontawesome      0.4.0      2022-10-25 [1] CRAN (R 4.2.2)
##    forcats          0.5.2      2022-08-19 [1] CRAN (R 4.2.1)
##    foreign          0.8-82     2022-01-13 [2] CRAN (R 4.1.2)
##    fs               1.5.2      2021-12-08 [1] CRAN (R 4.1.2)
##    gapminder        0.3.0      2017-10-31 [1] CRAN (R 4.2.1)
##    gargle           1.2.1      2022-09-08 [1] CRAN (R 4.2.1)
##    generics         0.1.3      2022-07-05 [1] CRAN (R 4.2.1)
##    gert             1.9.1      2022-10-05 [1] CRAN (R 4.2.2)
##    ggplot2          3.4.0      2022-11-04 [1] CRAN (R 4.2.2)
##    gh               1.3.1      2022-09-08 [1] CRAN (R 4.2.1)
##    gitcreds         0.1.2      2022-09-08 [1] CRAN (R 4.2.1)
##  P glue             1.6.2      2022-02-24 [?] CRAN (R 4.1.2)
##    googledrive      2.0.0      2021-07-08 [1] CRAN (R 4.1.0)
##    googlesheets4    1.0.1      2022-08-13 [1] CRAN (R 4.2.1)
##    gt               0.8.0      2022-11-16 [1] CRAN (R 4.2.2)
##    gtable           0.3.1      2022-09-01 [1] CRAN (R 4.2.1)
##    gtExtras         0.4.3      2022-11-05 [1] CRAN (R 4.2.2)
##    gtsummary        1.6.2      2022-09-30 [1] CRAN (R 4.2.1)
##    haven            2.5.1      2022-08-22 [1] CRAN (R 4.2.1)
##    here             1.0.1      2020-12-13 [1] CRAN (R 4.2.0)
##    highr            0.9        2021-04-16 [1] CRAN (R 4.0.5)
##    hms              1.1.2      2022-08-19 [1] CRAN (R 4.2.1)
##  P htmltools        0.5.3      2022-07-18 [?] CRAN (R 4.2.1)
##  P htmlwidgets      1.5.4      2021-09-08 [?] CRAN (R 4.2.1)
##    httpuv           1.6.6      2022-09-08 [1] CRAN (R 4.2.1)
##    httr             1.4.4      2022-08-17 [1] CRAN (R 4.2.1)
##    ids              1.0.1      2017-05-31 [1] CRAN (R 4.0.0)
##    ini              0.3.1      2018-05-20 [1] CRAN (R 4.2.1)
##    isoband          0.2.6      2022-10-06 [1] CRAN (R 4.2.1)
##    janitor          2.1.0      2021-01-05 [1] CRAN (R 4.2.0)
##    jquerylib        0.1.4      2021-04-26 [1] CRAN (R 4.0.5)
##  P jsonlite         1.8.3      2022-10-21 [?] CRAN (R 4.2.1)
##    juicyjuice       0.1.0      2022-11-10 [1] CRAN (R 4.2.2)
##    KernSmooth       2.23-20    2021-05-03 [2] CRAN (R 4.0.5)
##  P knitr            1.40       2022-08-24 [?] CRAN (R 4.2.1)
##    labeling         0.4.2      2020-10-20 [1] CRAN (R 4.0.3)
##    labelled         2.10.0     2022-09-14 [1] CRAN (R 4.2.1)
##    later            1.3.0      2021-08-18 [1] CRAN (R 4.2.0)
##    lattice          0.20-45    2021-09-22 [2] CRAN (R 4.2.0)
##  P lifecycle        1.0.3      2022-10-07 [?] CRAN (R 4.2.1)
##    lubridate        1.9.0      2022-11-06 [1] CRAN (R 4.2.2)
##  P magrittr         2.0.3      2022-03-30 [?] CRAN (R 4.1.3)
##    markdown         1.3        2022-10-29 [1] CRAN (R 4.2.2)
##    MASS             7.3-58.1   2022-08-03 [2] CRAN (R 4.2.1)
##    Matrix           1.5-3      2022-11-11 [2] CRAN (R 4.2.2)
##    memoise          2.0.1      2021-11-26 [1] CRAN (R 4.2.0)
##    mgcv             1.8-41     2022-10-21 [2] CRAN (R 4.2.1)
##    mime             0.12       2021-09-28 [1] CRAN (R 4.1.1)
##    miniUI           0.1.1.1    2018-05-18 [1] CRAN (R 4.2.1)
##    modelr           0.1.9      2022-08-19 [1] CRAN (R 4.2.1)
##    munsell          0.5.0      2018-06-12 [1] CRAN (R 4.0.0)
##    nlme             3.1-160    2022-10-10 [2] CRAN (R 4.2.1)
##    nnet             7.3-18     2022-09-28 [2] CRAN (R 4.2.1)
##    openssl          2.0.4      2022-10-17 [1] CRAN (R 4.2.2)
##    packrat          0.8.1      2022-06-29 [1] CRAN (R 4.2.1)
##    paletteer        1.5.0      2022-10-19 [1] CRAN (R 4.2.2)
##    palmerpenguins   0.1.1      2022-08-15 [1] CRAN (R 4.2.1)
##    pillar           1.8.1      2022-08-19 [1] CRAN (R 4.2.1)
##    pkgbuild         1.3.1      2021-12-20 [1] CRAN (R 4.2.1)
##    pkgconfig        2.0.3      2019-09-22 [1] CRAN (R 4.0.0)
##    pkgdown          2.0.6      2022-07-16 [1] CRAN (R 4.2.1)
##    pkgload          1.3.1      2022-10-28 [1] CRAN (R 4.2.2)
##    praise           1.0.0      2015-08-11 [1] CRAN (R 4.2.1)
##    prettyunits      1.1.1      2020-01-24 [1] CRAN (R 4.0.0)
##    prismatic        1.1.1      2022-08-15 [1] CRAN (R 4.2.1)
##    processx         3.8.0      2022-10-26 [1] CRAN (R 4.2.2)
##    profvis          0.3.7      2020-11-02 [1] CRAN (R 4.2.1)
##    progress         1.2.2      2019-05-16 [1] CRAN (R 4.0.0)
##    promises         1.2.0.1    2021-02-11 [1] CRAN (R 4.2.0)
##    ps               1.7.2      2022-10-26 [1] CRAN (R 4.2.2)
##    purrr            0.3.5      2022-10-06 [1] CRAN (R 4.2.1)
##    R6               2.5.1      2021-08-19 [1] CRAN (R 4.1.1)
##    ragg             1.2.4      2022-10-24 [1] CRAN (R 4.2.2)
##    rappdirs         0.3.3      2021-01-31 [1] CRAN (R 4.0.3)
##    rcmdcheck        1.4.0      2021-09-27 [1] CRAN (R 4.2.1)
##    RColorBrewer     1.1-3      2022-04-03 [1] CRAN (R 4.1.3)
##    Rcpp             1.0.9      2022-07-08 [1] CRAN (R 4.2.1)
##    readr            2.1.3      2022-10-01 [1] CRAN (R 4.2.1)
##    readxl           1.4.1      2022-08-17 [1] CRAN (R 4.2.1)
##    rematch          1.0.1      2016-04-21 [1] CRAN (R 4.0.0)
##    rematch2         2.1.2      2020-05-01 [1] CRAN (R 4.0.0)
##    remotes          2.4.2      2021-11-30 [1] CRAN (R 4.2.1)
##    renv             0.15.5     2022-05-26 [1] CRAN (R 4.2.0)
##    reprex           2.0.2      2022-08-17 [1] CRAN (R 4.2.1)
##  P rlang            1.0.6      2022-09-24 [?] CRAN (R 4.2.1)
##  P rmarkdown        2.18       2022-11-09 [?] CRAN (R 4.2.2)
##    roxygen2         7.2.1      2022-07-18 [1] CRAN (R 4.2.1)
##    rpart            4.1.19     2022-10-21 [2] CRAN (R 4.2.1)
##    rprojroot        2.0.3      2022-04-02 [1] CRAN (R 4.1.3)
##    rsconnect        0.8.28     2022-10-24 [1] CRAN (R 4.2.2)
##    rstudioapi       0.14       2022-08-22 [1] CRAN (R 4.2.1)
##    rversions        2.1.2      2022-08-31 [1] CRAN (R 4.2.1)
##    rvest            1.0.3      2022-08-19 [1] CRAN (R 4.2.1)
##    sass             0.4.2      2022-07-16 [1] CRAN (R 4.2.1)
##    scales           1.2.1      2022-08-20 [1] CRAN (R 4.2.1)
##    selectr          0.4-2      2019-11-20 [1] CRAN (R 4.0.0)
##  P sessioninfo      1.2.2      2021-12-06 [?] CRAN (R 4.2.1)
##    shiny            1.7.3      2022-10-25 [1] CRAN (R 4.2.2)
##    snakecase        0.11.0     2019-05-25 [1] CRAN (R 4.2.0)
##    sourcetools      0.1.7      2018-04-25 [1] CRAN (R 4.2.0)
##    spatial          7.3-15     2022-01-13 [2] CRAN (R 4.1.2)
##  P stringi          1.7.8      2022-07-11 [?] CRAN (R 4.2.1)
##  P stringr          1.4.1.9000 2022-11-09 [?] Github (tidyverse/stringr@f482fb0)
##    survival         3.4-0      2022-08-09 [2] CRAN (R 4.2.1)
##    svglite          2.1.0      2022-02-03 [1] CRAN (R 4.2.1)
##    sys              3.4.1      2022-10-18 [1] CRAN (R 4.2.2)
##    systemfonts      1.0.4      2022-02-11 [1] CRAN (R 4.1.2)
##    testthat         3.1.5      2022-10-08 [1] CRAN (R 4.2.2)
##    textshaping      0.3.6      2021-10-13 [1] CRAN (R 4.2.1)
##    thematic         0.1.2.1    2021-06-09 [1] CRAN (R 4.2.0)
##    tibble           3.1.8      2022-07-22 [1] CRAN (R 4.2.1)
##    tidyr            1.2.1      2022-09-08 [1] CRAN (R 4.2.1)
##    tidyselect       1.2.0      2022-10-10 [1] CRAN (R 4.2.1)
##    tidyverse        1.3.2      2022-07-18 [1] CRAN (R 4.2.1)
##    timechange       0.1.1      2022-11-04 [1] CRAN (R 4.2.2)
##    tinytex          0.42       2022-09-27 [1] CRAN (R 4.2.1)
##    tzdb             0.3.0      2022-03-28 [1] CRAN (R 4.1.3)
##    urlchecker       1.0.1      2021-11-30 [1] CRAN (R 4.2.1)
##    usethis          2.1.6      2022-05-25 [1] CRAN (R 4.2.1)
##    utf8             1.2.2      2021-07-24 [1] CRAN (R 4.1.0)
##    uuid             1.1-0      2022-04-19 [1] CRAN (R 4.2.0)
##    V8               4.2.2      2022-11-03 [1] CRAN (R 4.2.2)
##    vctrs            0.5.1.9000 2022-11-18 [1] Github (r-lib/vctrs@48794fd)
##    viridisLite      0.4.1      2022-08-22 [1] CRAN (R 4.2.1)
##    vroom            1.6.0      2022-09-30 [1] CRAN (R 4.2.1)
##    waldo            0.4.0      2022-03-16 [1] CRAN (R 4.2.1)
##    webshot2         0.1.0      2022-05-18 [1] CRAN (R 4.2.1)
##    websocket        1.4.1      2021-08-18 [1] CRAN (R 4.2.1)
##    whisker          0.4        2019-08-28 [1] CRAN (R 4.2.1)
##    withr            2.5.0      2022-03-03 [1] CRAN (R 4.1.3)
##  P xfun             0.34       2022-10-18 [?] CRAN (R 4.2.2)
##    xml2             1.3.3      2021-11-30 [1] CRAN (R 4.1.2)
##    xopen            1.0.0      2018-09-17 [1] CRAN (R 4.2.1)
##    xtable           1.8-4      2019-04-21 [1] CRAN (R 4.2.0)
##    yaml             2.3.6      2022-10-18 [1] CRAN (R 4.2.1)
##    zip              2.2.2      2022-10-26 [1] CRAN (R 4.2.2)
## 
##  [1] /media/albert/Files/NextCloud/R Projects/gt_book/renv/library/R-4.2/x86_64-pc-linux-gnu
##  [2] /usr/lib/R/library
## 
##  P ── Loaded and on-disk path mismatch.
## 
## ──────────────────────────────────────────────────────────────────────────────
quarto check
## 
## 
[✓] Checking Quarto installation......OK
##       Version: 1.2.267
##       Path: /opt/quarto/bin
## 
## 
(|) Checking basic markdown render....
(/) Checking basic markdown render....
[✓] Checking basic markdown render....OK
## 
## 
[✓] Checking Python 3 installation....OK
##       Version: 3.8.10
##       Path: /usr/bin/python3
##       Jupyter: (None)
## 
##       Jupyter is not available in this Python installation.
##       Install with python3 -m pip install jupyter
## 
## 
(|) Checking R installation...........
(/) Checking R installation...........
(-) Checking R installation...........
(\) Checking R installation...........
[✓] Checking R installation...........OK
##       Version: 4.2.2
##       Path: /usr/lib/R
##       LibPaths:
##         - /media/albert/Files/NextCloud/R Projects/gt_book/renv/library/R-4.2/x86_64-pc-linux-gnu
##         - /usr/lib/R/library
##       rmarkdown: 2.18
## 
## 
(|) Checking Knitr engine render......
(/) Checking Knitr engine render......
(-) Checking Knitr engine render......
(\) Checking Knitr engine render......
(|) Checking Knitr engine render......
(/) Checking Knitr engine render......
(-) Checking Knitr engine render......
(\) Checking Knitr engine render......
(|) Checking Knitr engine render......
[✓] Checking Knitr engine render......OK