knitr::opts_chunk$set(
  collapse = TRUE,
  eval = TRUE,
  warning = FALSE,
  comment = "#>"
)

FAQ and Gallery showing various tables possible with the {gtsummary} package.

library(gtsummary)

Frequently Asked Questions

Data Summary Tables

Headers, Labels and Formatting

  1. How do I modify column headers in a table?

  2. How do I change the value of the by levels?

  3. How do I added a spanning header row to a table?

  4. How do I change variable labels?

  5. How do I italicize or bold labels in a table?

  6. How do I italicize or bold levels in a table?

  7. How do I reduce font size and cell padding in a table?

Adding and Modifying Statistics

  1. How do I add the number of observations to a summary table?

  2. How do I show additional summary statistics as a new row?

  3. How do I include a column for missing values of a grouping variable?

  4. How do I add a column with the confidence around the mean?

  5. How do I add a column for the difference between groups?

  6. How do I summarize a continuous variable by one, two (or more) categorical variables?

  7. How do I stratify a summary table by more than one variable?

  8. How do I change the p-values format?

  9. How do I add a p-value for each group compared to a single reference group?

  10. How do I add a correction for multiple testing?

  11. How do I combine a summary table with a regression table?

Statistical Tests

  1. How do I do a paired t-test or McNemar's test?

Regression Tables

Headers, Labels and Formatting

  1. How do I change the text in a footnote?

  2. How do I add significance stars for low p-values?

  3. How do I reduce font size and cell padding in a table?

Creating and Combining Tables

  1. How do I combine the results of two related regression models into one table?

  2. How do I combine a regression table with a summary table?

  3. How do I create a regression table for multiple models with the same covariate(s) and different outcomes?

Adding and Modifying Statistics

  1. How do I add total event numbers to a regression table?

  2. How do I add event numbers for each level of a categorical covariate?

  3. How do I format a Wald confidence interval?

  4. How do I use robust standard errors?


Summary Tables

Add a spanning header over the group columns for increased clarity, and modify column headers. Using bold_labels() formats the labels as bold, but labels can also be italicized using italicize_labels(), or combined to format with both bold and italics.

trial |> 
  tbl_summary(
    by = trt,
    include = c(age, grade),
    missing = "no",
    statistic = all_continuous() ~ "{median} ({p25}, {p75})"
  ) |> 
  modify_header(all_stat_cols() ~ "**{level}**  \nN = {n} ({style_percent(p)}%)") |> 
  add_n() |> 
  bold_labels() |> 
  modify_spanning_header(all_stat_cols() ~ "**Chemotherapy Treatment**")


Show continuous summary statistics on multiple lines. Levels are italicized here using the italicize_levels() function, but the bold_levels() function can be used instead to create bold text, or both functions can be used together to get text that is both bold and in italics.

trial |> 
  tbl_summary(
    by = trt,
    include = c(age, marker),
    type = all_continuous() ~ "continuous2",
    statistic =
      all_continuous() ~ c("{N_nonmiss}",
                           "{mean} ({sd})",
                           "{median} ({p25}, {p75})",
                           "{min}, {max}"),
    missing = "no"
  ) |> 
  italicize_levels()


Modify the function that formats the p-values, change variable labels, updating tumor response header, and add a correction for multiple testing.

trial |> 
  mutate(response = factor(response, labels = c("No Tumor Response", "Tumor Responded"))) |> 
  tbl_summary(
    by = response,
    include = c(age, grade),
    missing = "no",
    label = list(age ~ "Patient Age", grade ~ "Tumor Grade")
  ) |> 
  add_p(pvalue_fun = label_style_pvalue(digits = 2)) |> 
  add_q()


Include missing tumor response as column using forcats::fct_na_value_to_level().

trial |> 
  mutate(
    response = 
      factor(response, labels = c("No Tumor Response", "Tumor Responded")) |> 
      forcats::fct_na_value_to_level(level = "Missing Response Status")
  ) |> 
  tbl_summary(
    by = response,
    include = c(age, grade),
    label = list(age ~ "Patient Age", grade ~ "Tumor Grade")
  )


Report treatment differences between two groups. This is often needed in randomized trials. In this example, we report the difference in tumor response and marker level between two chemotherapy treatments.

trial |> 
  tbl_summary(
    by = trt,
    include = c(response, marker),
    statistic = list(
      all_continuous() ~ "{mean} ({sd})",
      all_categorical() ~ "{p}%"
    ),
    missing = "no"
  ) |> 
  add_difference() |> 
  add_n() |> 
  modify_header(all_stat_cols() ~ "**{level}**")


Paired t-test and McNemar's test. The data is expected in a long format with 2 rows per participant.

# imagine that each patient received Drug A and Drug B (adding ID showing their paired measurements)
trial_paired <-
  trial |> 
  select(trt, marker, response) |> 
  mutate(.by = trt, id = dplyr::row_number())

# you must first delete incomplete pairs from the data, then you can build the table
trial_paired |> 
  # delete missing values
  tidyr::drop_na() |> 
  # keep IDs with both measurements
  dplyr::filter(.by = id, dplyr::n() == 2) |> 
  # summarize data
  tbl_summary(by = trt, include = -id) |> 
  add_p(
    test = list(marker ~ "paired.t.test",
                response ~ "mcnemar.test"),
    group = id
  )


Include p-values comparing all groups to a single reference group.

# table summarizing data with no p-values
small_trial <- trial |> select(grade, age, response)
t0 <- small_trial |> 
  tbl_summary(by = grade, missing = "no") |> 
  modify_header(all_stat_cols() ~ "**{level}**")

# table comparing grade I and II
t1 <- small_trial |> 
  dplyr::filter(grade %in% c("I", "II")) |> 
  tbl_summary(by = grade, missing = "no") |> 
  add_p() |> 
  modify_header(p.value ~ "**I vs. II**") |> 
  # hide summary stat columns
  modify_column_hide(all_stat_cols())

# table comparing grade I and II
t2 <- small_trial |> 
  dplyr::filter(grade %in% c("I", "III")) |> 
  tbl_summary(by = grade, missing = "no") |> 
  add_p() |> 
  modify_header(p.value = "**I vs. III**") |> 
  # hide summary stat columns
  modify_column_hide(all_stat_cols())

# merging the 3 tables together, and adding additional gt formatting
tbl_merge(list(t0, t1, t2)) |> 
  modify_spanning_header(
    all_stat_cols() ~ "**Tumor Grade**",
    starts_with("p.value") ~ "**p-values**"
  )


Add 95% confidence interval around the mean as an additional column

trial |> 
  tbl_summary(
    include = c(age, marker),
    statistic = all_continuous() ~ "{mean} ({sd})", 
    missing = "no"
  ) |> 
  modify_header(stat_0 = "**Mean (SD)**") |> 
  remove_footnote_header(stat_0) |> 
  add_ci()


It's often needed to summarize a continuous variable by one, two, or more categorical variables. The example below shows a table summarizing a continuous variable by two categorical variables. To summarize by more than two categorical variables, use tbl_continuous in conjunction with tbl_strata (see an example of tbl_strata here).

trial |> 
  tbl_continuous(variable = marker, by = trt, include = grade) |> 
  modify_spanning_header(all_stat_cols() ~ "**Treatment Assignment**")


Build a summary table stratified by more than one variable.

trial |> 
  select(trt, grade, age, stage) |> 
  mutate(grade = paste("Grade", grade)) |> 
  tbl_strata(
    strata = grade,
    ~ .x |> 
      tbl_summary(by = trt, missing = "no") |> 
      modify_header(all_stat_cols() ~ "**{level}**")
  )

Regression Tables

Include number of observations and the number of events in a univariate regression table.

trial |> 
  tbl_uvregression(
    method = glm,
    y = response,
    include = c(age, grade),
    method.args = list(family = binomial),
    exponentiate = TRUE
  ) |> 
  add_nevent()


Include two related models side-by-side with descriptive statistics. We also use the compact table theme that reduces cell padding and font size.

gt_r1 <- glm(response ~ trt + grade, trial, family = binomial) |> 
  tbl_regression(exponentiate = TRUE)
gt_r2 <- survival::coxph(survival::Surv(ttdeath, death) ~ trt + grade, trial) |> 
  tbl_regression(exponentiate = TRUE)
gt_t1 <- trial |> 
  tbl_summary(include = c(trt, grade), missing = "no") |> 
  add_n() |> 
  modify_header(stat_0 = "**n (%)**") |> 
  remove_footnote_header(stat_0)

theme_gtsummary_compact()
tbl_merge(
  list(gt_t1, gt_r1, gt_r2),
  tab_spanner = c(NA_character_, "**Tumor Response**", "**Time to Death**")
)
reset_gtsummary_theme()


Include the number of events at each level of a categorical predictor.

trial |> 
  tbl_uvregression(
    method = survival::coxph,
    y = survival::Surv(ttdeath, death),
    include = c(stage, grade),
    exponentiate = TRUE,
    hide_n = TRUE
  ) |> 
  add_nevent(location = "level")


Regression model where the covariate remains the same, and the outcome changes.

trial |> 
  tbl_uvregression(
    method = lm,
    x = trt,
    show_single_row = "trt",
    hide_n = TRUE,
    include = c(age, marker)
  ) |> 
  modify_header(label = "**Model Outcome**",
                estimate = "**Treatment Coef.**") |> 
  modify_footnote_header("Values larger than 0 indicate larger values in the Drug B group.", columns = estimate)


Implement a custom tidier to report Wald confidence intervals. The Wald confidence intervals are calculated using confint.default().

my_tidy <- function(x, exponentiate = FALSE, conf.level = 0.95, ...) {
  dplyr::bind_cols(
    broom::tidy(x, exponentiate = exponentiate, conf.int = FALSE),
    # calculate the confidence intervals, and save them in a tibble
    dplyr::case_when(
      exponentiate ~ exp(confint.default(x)),
      .default = confint.default(x)
    ) |> 
      dplyr::as_tibble(.name_repair = "minimal") |> 
      rlang::set_names(c("conf.low", "conf.high"))
  )
}

lm(age ~ grade + marker, trial) |> 
  tbl_regression(tidy_fun = my_tidy)


Use significance stars on estimates with low p-values.

trial |> 
  tbl_uvregression(
    method = survival::coxph,
    y = survival::Surv(ttdeath, death),
    include = c(stage, grade),
    exponentiate = TRUE,
  ) |> 
  add_significance_stars()


To use robust standard errors in a regression model, the model is prepared use usual, and the variance-covariance matrix of the model is modified via an appropriate function, such as vcovCL from the sandwich package.

dat <- trial |> 
  mutate(subject_id = dplyr::row_number(), .by = trt)
lmod <- glm(response ~ trt + grade, data = dat, family = binomial(link = "logit"))

cov <- sandwich::vcovCL(lmod, cluster = ~ subject_id, vcov_type = "HC0")

Once you have the robust variance-covariance matrix, you can use it with tidy_robust to calculate adjusted confidence intervals and p-values.

Robust errors generally have only a small impact on the confidence intervals and p-values. For demonstration purposes, we therefore show 2 digits for p-values.

A standard, non-robust regression table can be made as follows:

tbl_standard <- 
  tbl_regression(
    lmod,
    pvalue_fun = label_style_pvalue(digits = 2),
    exponentiate = TRUE
  )
tbl_standard

In order to use the robust errors, pass the variance-covariance matrix to the tidy_robust function, as shown below.

tbl_robust <- 
  tbl_regression(
    lmod,
    pvalue_fun = label_style_pvalue(digits = 2),
    exponentiate = TRUE,
    tidy_fun = \(x, ...) tidy_robust(x, vcov = cov, ...))
tbl_robust

Comparing the tables side-by-side, we see that the confidence intervals and p-values are very similar.

tbl_merge(
  list(tbl_standard, tbl_robust), 
  tab_spanner = c("**Standard errors**", "**Robust errors**")
)

Global p-values can also be calculated with robust errors in the same manner via the tidy_wald_test function. Again, the following example demonstrates the non-robust approach and the robust approach side-by-side.

tbl_merge(
  list(
    tbl_standard |> add_global_p(anova_fun = tidy_wald_test),
    tbl_robust |>
      add_global_p(anova_fun = \(x, ...) tidy_wald_test(x, vcov = cov))
  ), 
  tab_spanner = c("**Standard errors**", "**Robust errors**")
)


ddsjoberg/gtsummary documentation built on March 1, 2025, 7:52 p.m.