Study Notes: gt package and format table

R
Author

Jihong

Published

May 25, 2020

A introduction about gt package is here

⌘+C
knitr::opts_chunk$set(echo = TRUE, 
                      warning = FALSE, 
                      message = FALSE,
                      fig.align = "default", 
                      eval = TRUE)

library(gt)
suppressMessages(library(tidyverse))
Warning: package 'dplyr' was built under R version 4.2.3
Warning: package 'stringr' was built under R version 4.2.3

1 Basics of gt

A basic gt table can be created as so

⌘+C
data("iris")
glimpse(iris)
Rows: 150
Columns: 5
$ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.4, 4.…
$ Sepal.Width  <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 3.7, 3.…
$ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1.5, 1.…
$ Petal.Width  <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0.2, 0.…
$ Species      <fct> setosa, setosa, setosa, setosa, setosa, setosa, setosa, s…
⌘+C
iris %>% 
  head() %>% 
  gt()
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa

You can add row names (rowname_col argument) and add group names (groupname_col argument) into the table:

⌘+C
iris %>% 
  arrange(desc(Sepal.Length)) %>% # 6 types of iris with largest sepal length
  mutate(Rank = paste0("ID", 1:nrow(.))) %>% 
  head(20) %>% 
  gt(groupname_col = "Species", 
     rowname_col = "Rank")
Sepal.Length Sepal.Width Petal.Length Petal.Width
virginica
ID1 7.9 3.8 6.4 2.0
ID2 7.7 3.8 6.7 2.2
ID3 7.7 2.6 6.9 2.3
ID4 7.7 2.8 6.7 2.0
ID5 7.7 3.0 6.1 2.3
ID6 7.6 3.0 6.6 2.1
ID7 7.4 2.8 6.1 1.9
ID8 7.3 2.9 6.3 1.8
ID9 7.2 3.6 6.1 2.5
ID10 7.2 3.2 6.0 1.8
ID11 7.2 3.0 5.8 1.6
ID12 7.1 3.0 5.9 2.1
ID15 6.9 3.2 5.7 2.3
ID16 6.9 3.1 5.4 2.1
ID17 6.9 3.1 5.1 2.3
ID19 6.8 3.0 5.5 2.1
ID20 6.8 3.2 5.9 2.3
versicolor
ID13 7.0 3.2 4.7 1.4
ID14 6.9 3.1 4.9 1.5
ID18 6.8 2.8 4.8 1.4

Next, the boarder could be added into the table:

⌘+C
iris %>% 
  arrange(desc(Sepal.Length)) %>% # 6 types of iris with largest sepal length
  mutate(Rank = paste0("ID", 1:nrow(.))) %>% 
  head(20) %>% 
  gt(groupname_col = "Species", 
     rowname_col = "Rank") %>% 
  ########################### 
  # Below is changed
  ###########################
  tab_style( # tab_style to change style of cells, 
    # cells_borders provides the formatting
    # locations tells it where add black borders to all column labels
    style = list(
      cell_borders(
        sides = "left",
        color = "black",
        weight = px(1.2)
      )
    ),
    locations = list(
      cells_body(
        columns = colnames(iris)
      )
    )
  ) %>% 
  # Add botton line below the column names
  tab_style(
    style = list(
      cell_borders(
        sides = "bottom",
        color = "black",
        weight = px(3)
      )
    ),
    locations = list(
      cells_column_labels(
        columns = gt::everything()
      )
    )
  )
Sepal.Length Sepal.Width Petal.Length Petal.Width
virginica
ID1 7.9 3.8 6.4 2.0
ID2 7.7 3.8 6.7 2.2
ID3 7.7 2.6 6.9 2.3
ID4 7.7 2.8 6.7 2.0
ID5 7.7 3.0 6.1 2.3
ID6 7.6 3.0 6.6 2.1
ID7 7.4 2.8 6.1 1.9
ID8 7.3 2.9 6.3 1.8
ID9 7.2 3.6 6.1 2.5
ID10 7.2 3.2 6.0 1.8
ID11 7.2 3.0 5.8 1.6
ID12 7.1 3.0 5.9 2.1
ID15 6.9 3.2 5.7 2.3
ID16 6.9 3.1 5.4 2.1
ID17 6.9 3.1 5.1 2.3
ID19 6.8 3.0 5.5 2.1
ID20 6.8 3.2 5.9 2.3
versicolor
ID13 7.0 3.2 4.7 1.4
ID14 6.9 3.1 4.9 1.5
ID18 6.8 2.8 4.8 1.4
Back to top