Search code examples
gtsummary

Setting the Overall column to report column percentage in gtsummary


I would like to have the Overall have column percentage whereas the other columns report row percentages. How would one go about that. Thanks in advance.

library(gtsummary)
library(dplyr)


trial %>%
  select(age, grade, response, trt) %>%
  tbl_summary(by=trt, percent = "row",
              type = response ~ "categorical",
              missing = "no") %>%
  add_overall()

Solution

  • The results from add_overall() will always be consistent with the call in tbl_summary(). To get what you're after, build two tables and merge them. Example below!

    library(gtsummary)
    
    tbl_row_percent <- 
      trial %>%
      select(age, grade, response, trt) %>%
      tbl_summary(by=trt, percent = "row",
                  type = response ~ "categorical",
                  missing = "no") 
    
    tbl_col_percent <- 
      trial %>%
      select(age, grade, response) %>%
      tbl_summary(type = response ~ "categorical",
                  missing = "no") %>%
      modify_header(all_stat_cols() ~ "**Overall**, N = {N}")
    
    tbl_final <-
      tbl_merge(list(tbl_col_percent, tbl_row_percent)) %>%
      modify_spanning_header(everything() ~ NA)
    

    enter image description here Created on 2021-08-24 by the reprex package (v2.0.1)