# sourcing the purrr shims from rlang library(rlang) source("https://raw.githubusercontent.com/r-lib/rlang/main/R/standalone-purrr.R") knitr::opts_chunk$set( collapse = TRUE, warning = FALSE, comment = "#>" ) gt_compact_fun <- function(x) { gt::tab_options(x, table.font.size = "small", data_row.padding = gt::px(1), summary_row.padding = gt::px(1), grand_summary_row.padding = gt::px(1), footnotes.padding = gt::px(1), source_notes.padding = gt::px(1), row_group.padding = gt::px(1) ) }
The tbl_summary()
function calculates descriptive statistics for continuous, categorical, and dichotomous variables in R, and presents the results in a beautiful, customizable summary table ready for publication (for example, Table 1 or demographic tables).
This vignette will walk a reader through the tbl_summary()
function, and the various functions available to modify and make additions to an existing table summary object.
Before going through the tutorial, install and load {gtsummary}.
# install.packages("gtsummary") library(gtsummary)
We'll be using the trial
data set throughout this example.
This set contains data from r nrow(trial)
patients who received one of two types of chemotherapy (Drug A or Drug B).
The outcomes are tumor response and death.
Each variable in the data frame has been assigned an attribute label (i.e. attr(trial$trt, "label") == "Chemotherapy Treatment")
with the labelled package.
These labels are displayed in the {gtsummary} output table by default.
Using {gtsummary} on a data frame without labels will simply print variable names in place of variable labels; there is also an option to add labels later.
trial |> imap( ~ dplyr::tibble( Variable = glue::glue("`{.y}`"), Class = class(.x), Label = attr(.x, "label") ) ) |> dplyr::bind_rows() |> gt::gt() |> gt::tab_source_note("Includes mix of continuous, dichotomous, and categorical variables") |> gt::fmt_markdown(columns = Variable) |> gt::cols_align("left") |> gt_compact_fun()
head(trial)
The default output from tbl_summary()
is meant to be publication ready.
Let's start by creating a table of summary statistics from the trial
data set.
The tbl_summary()
function can take, at minimum, a data frame as the only input, and returns descriptive statistics for each column in the data frame.
trial |> tbl_summary(include = c(trt, age, grade))
Note the sensible defaults with this basic usage; each of the defaults may be customized.
Variable types are automatically detected so that appropriate descriptive statistics are calculated.
Label attributes from the data set are automatically printed.
Missing values are listed as "Unknown" in the table.
Variable levels are indented and footnotes are added.
For this study data the summary statistics should be split by treatment group, which can be done by using the by=
argument.
To compare two or more groups, include add_p()
with the function call, which detects variable type and uses an appropriate statistical test.
trial |> tbl_summary(by = trt, include = c(age, grade)) |> add_p()
There are four primary ways to customize the output of the summary table.
tbl_summary()
function argumentsadd_*()
functionstbl_summary()
function argumentsThe tbl_summary()
function includes many input options for modifying the appearance.
dplyr::tribble( ~Argument, ~Description, "`label`", "specify the variable labels printed in table", "`type`", "specify the variable type (e.g. continuous, categorical, etc.)", "`statistic`", "change the summary statistics presented", "`digits`", "number of digits the summary statistics will be rounded to", "`missing`", "whether to display a row with the number of missing observations", "`missing_text`", "text label for the missing number row", "`missing_stat`", "statistic(s) to show on the missing row", "`sort`", "change the sorting of categorical levels by frequency", "`percent`", "print column, row, or cell percentages", "`include`", "list of variables to include in summary table" ) |> gt::gt() |> gt::fmt_markdown(columns = Argument) |> gt_compact_fun()
Example modifying tbl_summary()
arguments.
trial |> tbl_summary( by = trt, include = c(age, grade), statistic = list( all_continuous() ~ "{mean} ({sd})", all_categorical() ~ "{n} / {N} ({p}%)" ), digits = all_continuous() ~ 2, label = list(grade = "Tumor Grade"), missing_text = "(Missing)" )
There are multiple ways to specify the statistic=
argument using a single formula, a list of formulas, and a named list.
The following table shows equivalent ways to specify the mean statistic for continuous variables age
and marker.
Any {gtsummary} function argument that accepts formulas will accept each of these variations.
dplyr::tribble( ~`select_helper`, ~`varname`, ~`named_list`, '`all_continuous() ~ "{mean}"`', '`c("age", "marker") ~ "{mean}"`', '`list(age = "{mean}", marker = "{mean}")`', '`list(all_continuous() ~ "{mean}")`', '`c(age, marker) ~ "{mean}"`', NA_character_, NA_character_, '`list(c(age, marker) ~ "{mean}")`', NA_character_ ) |> gt::gt() |> gt::fmt_markdown(everything()) |> gt::cols_label( select_helper = gt::md("**Select with Helpers**"), varname = gt::md("**Select by Variable Name**"), named_list = gt::md("**Select with Named List**") ) |> gt::sub_missing(columns = everything(), missing_text = "---") |> gt::tab_options(table.font.size = "85%") |> gt::cols_width(everything() ~ gt::px(390))
# print picture of slide if in packagedown so not included in CRAN if (identical(Sys.getenv("IN_PKGDOWN"), "true")) { knitr::include_graphics("https://github.com/ddsjoberg/gtsummary/raw/main/data-raw/crayon_images/crayon-selectors.png") }
The {gtsummary} package has functions to adding information or statistics to tbl_summary()
tables.
dplyr::tribble( ~Function, ~Description, "`add_p()`", "add p-values to the output comparing values across groups", "`add_overall()`", "add a column with overall summary statistics", "`add_n()`", "add a column with N (or N missing) for each variable", "`add_difference()`", "add column for difference between two group, confidence interval, and p-value", "`add_stat_label()`", "add label for the summary statistics shown in each row", "`add_stat()`", "generic function to add a column with user-defined values", "`add_q()`", "add a column of q values to control for multiple comparisons" ) |> gt::gt() |> gt::fmt_markdown(columns = Function) |> gt_compact_fun()
The {gtsummary} package comes with functions specifically made to modify and format summary tables.
dplyr::tribble( ~Function, ~Description, "`modify_header()`", "update column headers", "`modify_footnote_header()`", "update column header footnote", "`modify_footnote_body()`", "update table body footnote", "`modify_spanning_header()`", "update spanning headers", "`modify_caption()`", "update table caption/title", "`bold_labels()`", "bold variable labels", "`bold_levels()`", "bold variable levels", "`italicize_labels()`", "italicize variable labels", "`italicize_levels()`", "italicize variable levels", "`bold_p()`", "bold significant p-values" ) |> gt::gt() |> gt::fmt_markdown(columns = Function) |> gt_compact_fun()
Example adding tbl_summary()
-family functions
trial |> tbl_summary(by = trt, includ = c(age, grade)) |> add_p(pvalue_fun = label_style_pvalue(digits = 2)) |> add_overall() |> add_n() |> modify_header(label ~ "**Variable**") |> modify_spanning_header(c("stat_1", "stat_2") ~ "**Treatment Received**") |> modify_footnote_header("Median (IQR) or Frequency (%)", columns = all_stat_cols()) |> modify_caption("**Table 1. Patient Characteristics**") |> bold_labels()
The {gt} package is packed with many great functions for modifying table output---too many to list here. Review the package's website for a full listing.
To use the {gt} package functions with {gtsummary} tables, the summary table must first be converted into a gt
object.
To this end, use the as_gt()
function after modifications have been completed with {gtsummary} functions.
trial |> tbl_summary(by = trt, include = c(age, grade), missing = "no") |> add_n() |> as_gt() |> gt::tab_source_note(gt::md("*This data is simulated*"))
There is flexibility in how you select variables for {gtsummary} arguments, which allows for many customization opportunities!
For example, if you want to show age and the marker levels to one decimal place in tbl_summary()
, you can pass digits = c(age, marker) ~ 1
.
The selecting input is flexible, and you may also pass quoted column names.
Going beyond typing out specific variables in your data set, you can use:
All {tidyselect} helpers available throughout the tidyverse, such as starts_with()
, contains()
, and everything()
(i.e. anything you can use with the dplyr::select()
function), can be used with {gtsummary}.
Additional {gtsummary} selectors that are included in the package to supplement tidyselect functions.
statistic = all_continuous() ~ "{mean} ({sd})"
.r
all_continuous()
all_categorical()
Dichotomous variables are, by default, included with all_categorical()
.
Continuous variables may also be summarized on multiple lines---a common format in some journals.
To update the continuous variables to summarize on multiple lines, update the summary type to "continuous2"
(for summaries on two or more lines).
trial |> tbl_summary( by = trt, include = age, type = all_continuous() ~ "continuous2", statistic = all_continuous() ~ c( "{N_nonmiss}", "{median} ({p25}, {p75})", "{min}, {max}" ), missing = "no" ) |> add_p(pvalue_fun = label_style_pvalue(digits = 2))
The information in this section applies to all {gtsummary} objects.
The {gtsummary} table has two important internal objects:
dplyr::tribble( ~`Internal Object`, ~Description, "`.$table_body`", "data frame that is printed as the gtsummary output table", "`.$table_styling`", "contains instructions for styling `.$table_body` when printed" ) |> gt::gt() |> gt::fmt_markdown(columns = everything()) |> gt_compact_fun()
When you print output from the tbl_summary()
function into the R console or into an R markdown document, the .$table_body
data frame is formatted using the instructions listed in .$table_styling
.
The default printer converts the {gtsummary} object to a {gt} object with as_gt()
via a sequence of {gt} commands executed on .$table_body
.
Here's an example of the first few calls saved with tbl_summary()
:
tbl_summary(trial) |> as_gt(return_calls = TRUE) |> head(n = 4)
The {gt} functions are called in the order they appear, beginning with gt::gt()
.
If the user does not want a specific {gt} function to run (i.e. would like to change default printing), any {gt} call can be excluded in the as_gt()
function.
In the example below, the default alignment is restored.
After the as_gt()
function is run, additional formatting may be added to the table using {gt} functions.
In the example below, a source note is added to the table.
tbl_summary(trial, by = trt, include = c(age, grade)) |> as_gt(include = -cols_align) |> gt::tab_source_note(gt::md("*This data is simulated*"))
The {gtsummary} tbl_summary()
function and the related functions have sensible defaults for rounding and presenting results.
If you, however, would like to change the defaults there are a few options.
The default options can be changed using the {gtsummary} themes function set_gtsummary_theme()
.
The package includes prespecified themes, and you can also create your own.
Themes can control baseline behavior, for example, how p-values and percentages are rounded, which statistics are presented in tbl_summary()
, default statistical tests in add_p()
, etc.
For details on creating a theme and setting personal defaults, review the themes vignette.
The {gtsummary} package also supports survey data (objects created with the {survey} package) via the tbl_svysummary()
function.
The syntax for tbl_svysummary()
and tbl_summary()
are nearly identical, and the examples above apply to survey summaries as well.
To begin, install the {survey} package and load the apiclus1
data set.
install.packages("survey")
# loading the api data set data(api, package = "survey")
Before we begin, we convert the data frame to a survey object, registering the ID and weighting columns, and setting the finite population correction column.
svy_apiclus1 <- survey::svydesign( id = ~dnum, weights = ~pw, data = apiclus1, fpc = ~fpc )
After creating the survey object, we can now summarize it similarly to a standard data frame using tbl_svysummary()
.
Like tbl_summary()
, tbl_svysummary()
accepts the by=
argument and works with the add_p()
and add_overall()
functions.
It is not possible to pass custom functions to the statistic=
argument of tbl_svysummary()
.
You must use one of the pre-defined summary statistic functions (e.g. {mean}
, {median}
) which leverage functions from the {survey} package to calculate weighted statistics.
svy_apiclus1 |> tbl_svysummary( # stratify summary statistics by the "both" column by = both, # summarize a subset of the columns include = c(api00, api99, both), # adding labels to table label = list(api00 = "API in 2000", api99 = "API in 1999") ) |> add_p() |> # comparing values by "both" column add_overall() |> # adding spanning header modify_spanning_header(c("stat_1", "stat_2") ~ "**Met Both Targets**")
tbl_svysummary()
can also handle weighted survey data where each row represents several individuals:
Titanic |> as_tibble() |> survey::svydesign(data = _, ids = ~1, weights = ~n) |> tbl_svysummary(include = c(Age, Survived))
Use tbl_cross()
to compare two categorical variables in your data.
tbl_cross()
is a wrapper for tbl_summary()
that:
percent = "cell"
by default.margin
argument).missing
argument). trial |> tbl_cross( row = stage, col = trt, percent = "cell" ) |> add_p()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.