The {gtsummary} package provides an elegant and flexible way to create publication-ready analytical and summary tables using the R programming language. The {gtsummary} package summarizes data sets, regression models, and more, using sensible defaults with highly customizable capabilities.
Summarize data frames or tibbles easily in R. Perfect for presenting descriptive statistics, comparing group demographics (e.g creating a Table 1 for medical journals), and more. Automatically detects continuous, categorical, and dichotomous variables in your data set, calculates appropriate descriptive statistics, and also includes amount of missingness in each variable.
Summarize regression models in R and include reference rows for categorical variables. Common regression models, such as logistic regression and Cox proportional hazards regression, are automatically identified and the tables are pre-filled with appropriate column headers (i.e. Odds Ratio and Hazard Ratio).
Customize gtsummary tables using a growing list of formatting/styling functions. Bold labels, italicize levels, add p-value to summary tables, style the statistics however you choose, merge or stack tables to present results side by side… there are so many possibilities to create the table of your dreams!
Report statistics inline from summary tables and regression summary tables in R markdown. Make your reports completely reproducible!
By leveraging {broom}, {gt}, and {labelled} packages, {gtsummary} creates beautifully formatted, ready-to-share summary and result tables in a single line of R code!
Check out the examples below, review the vignettes for a detailed exploration of the output options, and view the gallery for various customization examples.
The {gtsummary} package was written as a companion to the {gt} package from RStudio. You can install {gtsummary} with the following code.
install.packages("gtsummary")
Install the development version of {gtsummary} with:
::install_github("ddsjoberg/gtsummary") remotes
Use tbl_summary()
to summarize a data frame.
Example basic table:
library(gtsummary)
# make dataset with a few variables to summarize
<- trial %>% select(age, grade, response, trt)
trial2
# summarize the data with our package
<- tbl_summary(trial2) table1
There are many customization options to add
information (like comparing groups) and format
results (like bold labels) in your table. See the tbl_summary()
tutorial for many more options, or below for one example.
<-
table2 tbl_summary(
trial2,by = trt, # split table by group
missing = "no" # don't list missing data separately
%>%
) add_n() %>% # add column with total number of non-missing observations
add_p() %>% # test for a difference between groups
modify_header(label = "**Variable**") %>% # update the column header
bold_labels()
Use tbl_regression()
to easily and beautifully display regression model results in a table.
See the tutorial
for customization options.
<- glm(response ~ trt + age + grade, trial, family = binomial)
mod1
<- tbl_regression(mod1, exponentiate = TRUE) t1
You can also present side-by-side regression model results using
tbl_merge()
library(survival)
# build survival model table
<-
t2 coxph(Surv(ttdeath, death) ~ trt + grade + age, trial) %>%
tbl_regression(exponentiate = TRUE)
# merge tables
<-
tbl_merge_ex1 tbl_merge(
tbls = list(t1, t2),
tab_spanner = c("**Tumor Response**", "**Time to Death**")
)
Review even more output options in the table gallery.
The {gtsummary} package was written to be a companion to the {gt} package from RStudio. But not all output types are supported by the {gt} package. Therefore, we have made it possible to print {gtsummary} tables with various engines.
Review the gtsummary + R Markdown vignette for details.
{gtsummary} tables can also be saved directly to file as an image, RTF, LaTeX, and Word file.
%>%
tbl as_gt() %>%
::gtsave(filename = ".") # use extensions .html .tex .ltx .rtf gt
For a Word file, use
%>%
tbl as_flex_table() %>%
::save_as_docx() flextable
The best resources are the gtsummary vignettes: table
gallery, tbl_summary()
tutorial, tbl_regression()
tutorial, inline_text()
tutorial, gtsummary
themes, gtsummary+R
markdown.
The R Journal Article Reproducible Summary Tables with the gtsummary Package.
The RStudio Education Blog includes a post with a brief introduction to the package.
A recording of a presentation given to the Weill Cornell Biostatistics Department and the Memorial Sloan Kettering R Users Group.
> citation("gtsummary")
To cite gtsummary in publications use:
Sjoberg DD, Whiting K, Curry M, Lavery JA, Larmarange J. Reproducible summary tables with the gtsummary package.
The R Journal 2021;13:570–80. https://doi.org/10.32614/RJ-2021-053.
A BibTeX entry for LaTeX users is
@Article{gtsummary,
author = {Daniel D. Sjoberg and Karissa Whiting and Michael Curry and Jessica A. Lavery and Joseph Larmarange},
title = {Reproducible Summary Tables with the gtsummary Package},
journal = {{The R Journal}},
year = {2021},
url = {https://doi.org/10.32614/RJ-2021-053},
doi = {10.32614/RJ-2021-053},
volume = {13},
issue = {1},
pages = {570-580},
}
Big thank you to @jeffreybears for the hex sticker!
Please note that the {gtsummary} project is released with a Contributor
Code of Conduct. By contributing to this project, you agree to abide
by its terms. Thank you to all contributors!
@ablack3, @ABorakati, @aghaynes, @ahinton-mmc, @aito123, @akarsteve, @akefley, @albertostefanelli, @alexis-catherine, @amygimma, @anaavu, @andrader, @angelgar, @arbet003, @arnmayer, @aspina7, @asshah4, @awcm0n, @barthelmes, @BeauMeche, @benediktclaus, @berg-michael, @bhattmaulik, @brachem-christian, @bwiernik, @bx259, @calebasaraba, @CarolineXGao, @Chris-M-P, @chrisleitzinger, @cjprobst, @clmawhorter, @CodieMonster, @coeus-analytics, @coreysparks, @ctlamb, @davidgohel, @davidkane9, @dax44, @dchiu911, @ddsjoberg, @DeFilippis, @denis-or, @dieuv0, @discoleo, @djbirke, @dmenne, @eamoncaddigan, @ElfatihHasabo, @emilyvertosick, @ercbk, @erikvona, @eweisbrod, @feizhadj, @fh-jsnider, @ge-generation, @gjones1219, @gorkang, @GuiMarthe, @hass91, @HichemLa, @hughjonesd, @iaingallagher, @ilyamusabirov, @IndrajeetPatil, @IsadoraBM, @j-tamad, @jalavery, @jeanmanguy, @jemus42, @jenifav, @jennybc, @JeremyPasco, @JesseRop, @jflynn264, @jjallaire, @jmbarajas, @JoanneF1229, @joelgautschi, @jojosgithub, @JonGretar, @jthomasmock, @juseer, @jwilliman, @karissawhiting, @kmdono02, @kwakuduahc1, @lamhine, @larmarange, @leejasme, @loukesio, @lspeetluk, @ltin1214, @lucavd, @LuiNov, @maia-sh, @Marsus1972, @matthieu-faron, @mbac, @mdidish, @MelissaAssel, @michaelcurry1123, @mljaniczek, @moleps, @motocci, @mvuorre, @MyKo101, @oranwutang, @palantre, @Pascal-Schmidt, @pedersebastian, @philsf, @polc1410, @postgres-newbie, @proshano, @raphidoc, @rich-iannone, @RiversPharmD, @rmgpanw, @roman2023, @ryzhu75, @sachijay, @saifelayan, @sammo3182, @sbalci, @shannonpileggi, @shengchaohou, @ShixiangWang, @simonpcouch, @slb2240, @slobaugh, @spiralparagon, @StaffanBetner, @Stephonomon, @storopoli, @szimmer, @tamytsujimoto, @TarJae, @themichjam, @THIB20, @tibirkrajc, @tjmeyers, @tldrcharlene, @tormodb, @toshifumikuroda, @UAB-BST-680, @uakimix, @uriahf, @Valja64, @vvm02, @xkcococo, @yonicd, @yoursdearboy, @zabore, @zachariae, @zaddyzad, @zeyunlu, @zhengnow, @zlkrvsm, and @zongell-star