First post with quarto

Using the gt package to make a table with fake respiratory pathogen data.

gt
tables
tidyverse
Published

October 23, 2022

Obtaining the fictional data from Mockeroo

For this exercise I created a set of fictional records of person in hospital with common respiratory virus infections. The data are not in any way representative of the epidemiology of the respiratoey viruses. It’s all made up.

The data can be downloaded from this mockeroo schema

The data will be used for a series of posts about data manipulation, and visulisation of data with graphs and tables.

The features of the fictional data are:

  • id - each record has a unique identifier
  • sex - sex of case (male or female)
  • date_of_birth - self-explanatory, 1% are NA
  • condition - the respiratory pathogen name. These are weighted, most are COVID-19 and influenza
  • hospital - the hospital where the case was detected
  • admission_date - date admitted to hospital
  • discharge_date - randmly set to within 12 days of admission

Load the data and group it by hospital and condition

library(tidyverse)
library(gt)

resp <- read_csv("respiratory.csv")


# Group data by hospital and pathogen
resp_hosp_group <- resp %>% 
    group_by(hospital, condition) %>% 
    tally() %>% 
    arrange(desc(n)) %>% 
    pivot_wider(names_from = condition, values_from = n) %>% 
    ungroup() 

Use the grouped data to make a gt table

#  Make a nice table
resp_hosp_group %>% 
    gt() %>% 
    tab_header(
        title = "Common respiratory pathogens in fictional hospitals between 1 Jan 2021 and 30 June 2022",
        subtitle = md("Data generated from [mockeroo](https://www.mockaroo.com/6c4871a0)")
        ) %>% 
    opt_align_table_header(align = "left") %>%
    cols_label(
        `Respiratory syncytial virus` = "RSV",
        `Parainfluenza virus` = "Parainfluenza",
        hospital = "Hospital"
    ) %>% 
    tab_footnote(
        footnote = "Respiratory syncytial virus",
        locations = cells_column_labels(
            columns = c(`Respiratory syncytial virus`)
        )
    ) %>%
    tab_footnote(
        footnote = "Includes human parainfluenza subtypes 1, 2, and 3",
        locations = cells_column_labels(
            columns = c(
                `Parainfluenza virus`
            )
        )
    ) %>%
    opt_footnote_marks(marks = c("*", "#", "^",  "~")) %>% 
    cols_align(align = c("left"), columns = everything()) %>%
    cols_align(align = c("right"), columns = where(is.numeric)) %>% 
    cols_width(hospital ~ px(200),
        everything() ~ px(120)) %>% 
    tab_style(
        locations = cells_column_labels(),
        style = list(
            cell_fill("#183C5D"),
            cell_text("#FFFFFF", weight = 500)
        ) 
    ) %>% 
    tab_style(
        locations = cells_body(
            columns = hospital
        ),
        style = list(
            cell_fill("#0F5CA2"),
            cell_text("#FFFFFF", weight = 500)
        ) 
    ) 
Common respiratory pathogens in fictional hospitals between 1 Jan 2021 and 30 June 2022
Data generated from mockeroo
Hospital COVID-19 Influenza RSV* Adenovirus Parainfluenza# Metapneumovirus
Northshore General 8598 3410 2116 1698 896 856
Springhill General 6901 2883 1735 1373 742 676
Morningside General 5352 2083 1336 1104 507 539
Sunnybank Medical 3446 1363 888 672 358 339
Jenkins Medical 2883 1153 743 552 284 277
Blackwater Medical 2276 948 563 463 227 213
Alliance Medical 1694 679 419 311 171 191
Ophelia Surgery 601 239 144 124 46 51
* Respiratory syncytial virus
# Includes human parainfluenza subtypes 1, 2, and 3

There is a very good tutorial that explains this in more detail available here