Adding customized options to gtsummary tables

2.5k Views Asked by At

I'm trying to figure out how to add customized options when using gtsummary--for example, stars for pvalues, captions, etc.

Here's a reproducible example using base mtcars data, in case that's more efficient...

library(tidyverse)
library(gtsummary)
#> Warning: package 'gtsummary' was built under R version 4.0.3
#> #Uighur

r1 <- lm(mpg ~ wt + cyl, data = mtcars) %>% 
  tbl_regression(exponentiate = TRUE)

r2 <- lm(hp ~ wt + cyl, data = mtcars) %>% 
  tbl_regression(exponentiate = TRUE)

r3 <- lm(qsec ~ wt + cyl, data = mtcars) %>% 
  tbl_regression(exponentiate = TRUE)


tbl_merge(list(r1, r2, r3), 
          tab_spanner = c("**MPG**", "**Horsepower**", "**Seconds**"))
1

There are 1 best solutions below

0
On BEST ANSWER

You can use the add_significance_stars() function to add stars to your estimates. To add titles and other formatting, convert the gtsummary object to gt with the as_gt() function and add them using gt functions.

Example below.

library(gtsummary)
library(tidyverse)
packageVersion("gtsummary")
#> [1] '1.4.0'

# create a tibble with one row per model
tbl <-
  tibble(outcome = c("mpg", "hp", "qsec")) %>%
  rowwise() %>%
  mutate(
    tbl = 
      lm(str_glue("{outcome} ~ wt + cyl"), data = mtcars) %>%
      tbl_regression() %>%
      add_significance_stars(
        hide_se = TRUE,
        hide_ci = FALSE
      ) %>%
      list()
  ) %>%
  # pull tbl_regression() objects into single merged table
  pull(tbl) %>%
  tbl_merge(tab_spanner = c("**MPG**", "**Horsepower**", "**Seconds**")) %>%
  # add table captions
  as_gt() %>%
  gt::tab_header(title = "Table 1. Car Regression Model",
                 subtitle = "Highly Confidential")

strong text Created on 2021-04-15 by the reprex package (v2.0.0)