Last updated: 2019-03-10

Checks: 6 0

Knit directory: gt_examples/

This reproducible R Markdown analysis was created with workflowr (version 1.2.0). The Report tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20190309) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility. The version displayed above was the version of the Git repository at the time these results were generated.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    myproject/

Untracked files:
    Untracked:  code/DeletethisFile.Rmd
    Untracked:  code/vertical_table.R
    Untracked:  data/gt_datasets.svg
    Untracked:  trash.Rmd

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the R Markdown and HTML files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view them.

File Version Author Date Message
Rmd 7410d00 frm1789 2019-03-10 Change style
html ec56ac6 frm1789 2019-03-10 Build site.
Rmd cbb34ac frm1789 2019-03-10 merge origin/master
html 2d0d22e frm1789 2019-03-10 Build site.
Rmd 5b05b01 frm1789 2019-03-10 Change style
Rmd 2196943 Karl Broman 2019-03-10 Add a 6th example, similar to the 5th
Rmd 8383cd6 Karl Broman 2019-03-10 Add example with counts and percentages
html a3357ac frm1789 2019-03-10 Build site.
html 4f7442e frm1789 2019-03-10 Build site.
Rmd 4cad5f4 Florencia Mangini 2019-03-10 Merge pull request #2 from kbroman/cleanup
html 323a4bf frm1789 2019-03-09 Build site.
Rmd e74963f frm1789 2019-03-09 Change style
Rmd d286e53 Karl Broman 2019-03-09 Fix a doi link in analysis/index.Rmd
html f44cf75 frm1789 2019-03-09 Build site.
Rmd 5ec7b0e frm1789 2019-03-09 Change style
html 7b802ff frm1789 2019-03-09 Build site.
html a469093 frm1789 2019-03-09 Build site.
Rmd a7777bb frm1789 2019-03-09 merge origin/master
Rmd 7c4b53c Karl Broman 2019-03-09 Add example with labels spanning multiple columns
html 89f40a6 frm1789 2019-03-09 Build site.
Rmd caba3fa frm1789 2019-03-09 Change style
html 31c611a frm1789 2019-03-09 Build site.
html ccb3d44 frm1789 2019-03-09 Build site.
Rmd a61b98b frm1789 2019-03-09 Change style
html f2e71d4 frm1789 2019-03-09 Build site.
Rmd 74ef3bc frm1789 2019-03-09 Adding examples
html 901ba7d frm1789 2019-03-09 Build site.
Rmd 064a281 frm1789 2019-03-09 Change style
html dfa77be frm1789 2019-03-09 Build site.
Rmd cae060a frm1789 2019-03-09 Change style
html 5b576d8 frm1789 2019-03-09 Build site.
Rmd cf23a3a frm1789 2019-03-09 Change style
html 790ab5f frm1789 2019-03-09 Build site.
Rmd 919499f frm1789 2019-03-09 Change style
html 5172a5d frm1789 2019-03-09 Build site.
Rmd 9bd25af frm1789 2019-03-09 Change style
html f143184 frm1789 2019-03-09 Build site.
html b64b81a frm1789 2019-03-09 Build site.
Rmd 918755d frm1789 2019-03-09 Start my new project
html 40f4a4a frm1789 2019-03-09 Build site.
Rmd cad473d frm1789 2019-03-09 Start workflowr project.

Welcome

This is a small list with 25 visualization using gt Package.

What is this?

GT package is one of the most amazing package to create tables, and we want to show a gallery of examples with full R code to encourage you to use it in your projects.

  • How to start with GT Tables
  • How to customize a basic table
  • Examples about how to use

How to create a good table?

An informal definition could be: “A good table is used to read a set of numerical data in the quickest and easiest way”

25 examples about how to use this amazing package

1. Vertical table

library(gt)
library(tidyverse)
library(glue)

# Define the start and end dates for the data range
start_date <- "2010-06-07"
end_date <- "2010-06-14"

# Create a gt table based on preprocessed
# `sp500` table data
sp500 %>%
  dplyr::filter(date >= start_date & date <= end_date) %>%
  dplyr::select(-adj_close) %>%
  dplyr::mutate(date = as.character(date)) %>%
  gt() %>%
  tab_header(
    title = "S&P 500",
    subtitle = glue::glue("{start_date} to {end_date}")
  ) %>%
  fmt_date(
    columns = vars(date),
    date_style = 3
  ) %>%
  fmt_currency(
    columns = vars(open, high, low, close),
    currency = "USD"
  ) %>%
  fmt_number(
    columns = vars(volume),
    scale_by = 1 / 1E9,
    pattern = "{x}B"
  )
S&P 500
2010-06-07 to 2010-06-14
date open high low close volume
Mon, Jun 14, 2010 $1,095.00 $1,105.91 $1,089.03 $1,089.63 4.43B
Fri, Jun 11, 2010 $1,082.65 $1,092.25 $1,077.12 $1,091.60 4.06B
Thu, Jun 10, 2010 $1,058.77 $1,087.85 $1,058.77 $1,086.84 5.14B
Wed, Jun 9, 2010 $1,062.75 $1,077.74 $1,052.25 $1,055.69 5.98B
Tue, Jun 8, 2010 $1,050.81 $1,063.15 $1,042.17 $1,062.00 6.19B
Mon, Jun 7, 2010 $1,065.84 $1,071.36 $1,049.86 $1,050.47 5.47B

2. Table with references

This example is from Table 1 From proceedings of the Workshop on Language in Social Media (LSM 2011), pages 30–38, Portland, Oregon, 23 June 2011. (c) 2011 Association for Computational Linguistics

# the table's data
exa <- data.frame( acronym = c("gr8, gr8t", "lol", "rotf", "bff"),
                  english = c("great","laughing out loud", "rolling on the floor", "best friend forever"))

# create the gt table
gt(exa) %>%
  cols_align("left") %>%
  cols_label(acronym="Acronym",
             english="English expansion") %>%
  tab_source_note(
    source_note = "Table 1: Example acrynom and their expansion in the acronym dictionary."
  ) 
Acronym English expansion
gr8, gr8t great
lol laughing out loud
rotf rolling on the floor
bff best friend forever
Table 1: Example acrynom and their expansion in the acronym dictionary.

3. Table with spanning columns

This example is Table S2 in Broman et al. (2015) Genetics 192:267-279 doi:10.1534/genetics.112.142448

# the table's data
tab <- data.frame(n=c(300, 450, 600),
                  all_part_all_crosses = c(4.56, 4.51, 4.49),
                  all_part_min_crosses = c(4.48, 4.47, 4.44),
                  tree_part_all_crosses = c(4.43, 4.36, 4.32),
                  tree_part_min_crosses = c(4.33, 4.33, 4.29))

# create the gt table
gt(tab) %>%
    cols_align("center") %>%
    cols_label(n="total sample size",
               all_part_all_crosses="all crosses",
               all_part_min_crosses="min crosses",
               tree_part_all_crosses="all crosses",
               tree_part_min_crosses="min crosses") %>%
    tab_spanner(label="Tree partitions",
                starts_with("tree")) %>%
    tab_spanner(label="All partitions",
                starts_with("all"))
total sample size All partitions Tree partitions
all crosses min crosses all crosses min crosses
300 4.56 4.48 4.43 4.33
450 4.51 4.47 4.36 4.33
600 4.49 4.44 4.32 4.29

4. Counts and percentages

This example is Table S2 of Lobo et al. bioRxiv doi:10.1101/529040, with columns containing both counts and percentages, with the percentages in parentheses.

tab <- data.frame(A_count = c(8863572, 2870063, 671722),
                  A_proportion=c(0.853601028762177, 0.727484285290261, 0.556184650236973),
                  B_count = c(1520169, 1075126, 536010),
                  B_proportion=c(0.146398971237823, 0.272515714709739, 0.443815349763027),
                  row.names=c("AA", "AB", "BB"))
tab$genotype <- rownames(tab)

gt(tab, rowname_col="genotype") %>%
    fmt_percent(ends_with("proportion"),
                decimals=1,
                pattern="({x})") %>%
    cols_align("center") %>%
    cols_align("right", columns=ends_with("count")) %>%
    cols_label(A_count = "A count",
               A_proportion = "(%)",
               B_count = "B count",
               B_proportion = "(%)") %>%
    tab_spanner(label="allele in DO-360 microbiome",
                columns=TRUE) %>%
    tab_stubhead_label("DO-360 genotype")
DO-360 genotype allele in DO-360 microbiome
A count (%) B count (%)
AA 8863572 (85.4%) 1520169 (14.6%)
AB 2870063 (72.7%) 1075126 (27.3%)
BB 671722 (55.6%) 536010 (44.4%)

5. Counts and percentages, with an extra column.

This example is Table S4 of Lobo et al. bioRxiv doi:10.1101/529040. It is much like example 5, but has an additional column at the beginning.

tab <- data.frame(DO358_genotype = c("AA", "AA", "AA", "AB", "AB", "AB", "BB", "BB", "BB"),
                  DO344_genotype = c("AA", "AB", "BB", "AA", "AB", "BB", "AA", "AB", "BB"),
                  A_count = c(2394215, 869613, 103036, 686970, 297500, 55982, 73727, 47000, 542),
                  A_proportion = c(0.99747944497795, 0.794823306181542, 0.590911176362635,
                                   0.718274560155246, 0.51429835873996, 0.299220173924198,
                                   0.428685226532701, 0.219100940269354, 0.00457986885689177),
                   B_count = c(6050, 224483, 71332, 269447, 280958, 131111, 98257, 167513, 117802),
                   B_proportion = c(0.00252055502204965, 0.205176693818458, 0.409088823637365,
                                    0.281725439844754, 0.48570164126004, 0.700779826075802,
                                    0.571314773467299, 0.780899059730646, 0.995420131143108),
                   stringsAsFactors=FALSE)

gt(tab) %>%
    fmt_percent(ends_with("proportion"),
                decimals=1,
                pattern="({x})") %>%
    cols_align("right") %>%
    cols_align("center", columns=ends_with("genotype")) %>%
    cols_label(DO358_genotype="DO 358 genotype",
               DO344_genotype="DO 344 genotype",
               A_count = "A count",
               A_proportion = "(%)",
               B_count = "B count",
               B_proportion = "(%)") %>%
    tab_spanner(label="allele in DO-358 microbiome",
                columns=matches("^[AB]_"))
DO 358 genotype DO 344 genotype allele in DO-358 microbiome
A count (%) B count (%)
AA AA 2394215 (99.7%) 6050 (0.3%)
AA AB 869613 (79.5%) 224483 (20.5%)
AA BB 103036 (59.1%) 71332 (40.9%)
AB AA 686970 (71.8%) 269447 (28.2%)
AB AB 297500 (51.4%) 280958 (48.6%)
AB BB 55982 (29.9%) 131111 (70.1%)
BB AA 73727 (42.9%) 98257 (57.1%)
BB AB 47000 (21.9%) 167513 (78.1%)
BB BB 542 (0.5%) 117802 (99.5%)

6. Adding colors from Viridis palette

This example is using the dataset gtcars included into gt package and Viridis palette.

library(tidyverse) 
library(viridis)
library(scales)
library(gt) 

#selecting the data
exa2 <- gtcars %>% 
  select(mfr, hp) %>% 
  group_by(mfr) %>% 
  summarise(mean(hp))

#choosing colors
q_colors =  19 
v_colors =  viridis(q_colors, option ="D")

#creating table
gt(exa2) %>% 
  data_color(columns=vars("mean(hp)"), 
             color=scales::col_bin( bins=c(100, 200, 300, 400, 500, 600,700), 
             palette = v_colors, 
             domain=c(0, 700)) ) 
mfr mean(hp)
Acura 573.0000
Aston Martin 539.5000
Audi 482.0000
Bentley 500.0000
BMW 443.4000
Chevrolet 650.0000
Dodge 645.0000
Ferrari 660.7778
Ford 647.0000
Jaguar 340.0000
Lamborghini 620.0000
Lotus 400.0000
Maserati 401.0000
McLaren 570.0000
Mercedes-Benz 416.0000
Nissan 545.0000
Porsche 315.0000
Rolls-Royce 593.5000
Tesla 259.0000


sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: OS X El Capitan 10.11.6

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] scales_1.0.0      viridis_0.5.1     viridisLite_0.3.0
 [4] bindrcpp_0.2.2    glue_1.3.0        forcats_0.3.0    
 [7] stringr_1.3.1     dplyr_0.7.8       purrr_0.2.5      
[10] readr_1.1.1       tidyr_0.8.2       tibble_1.4.2     
[13] ggplot2_3.1.0     tidyverse_1.2.1   gt_0.1.0         

loaded via a namespace (and not attached):
 [1] tidyselect_0.2.5 haven_1.1.2      lattice_0.20-35  colorspace_1.3-2
 [5] htmltools_0.3.6  yaml_2.2.0       rlang_0.3.0.1    pillar_1.3.1    
 [9] withr_2.1.2      modelr_0.1.2     readxl_1.1.0     bindr_0.1.1     
[13] plyr_1.8.4       munsell_0.5.0    commonmark_1.7   gtable_0.2.0    
[17] workflowr_1.2.0  cellranger_1.1.0 rvest_0.3.2      evaluate_0.12   
[21] knitr_1.20       broom_0.5.0      Rcpp_1.0.0       backports_1.1.2 
[25] checkmate_1.8.5  jsonlite_1.6     fs_1.2.6         gridExtra_2.3   
[29] hms_0.4.2        digest_0.6.18    stringi_1.2.4    grid_3.5.1      
[33] rprojroot_1.3-2  cli_1.0.1        tools_3.5.1      magrittr_1.5    
[37] sass_0.1.0.9000  lazyeval_0.2.1   crayon_1.3.4     whisker_0.3-2   
[41] pkgconfig_2.0.2  xml2_1.2.0       lubridate_1.7.4  assertthat_0.2.0
[45] rmarkdown_1.10   httr_1.3.1       rstudioapi_0.8   R6_2.3.0        
[49] nlme_3.1-137     git2r_0.23.0     compiler_3.5.1