0

How can I use tab_style() to add a background color to the "Overall" column?

library(gtsummary)
trial %>% select(trt, age, grade, response) %>%
tbl_summary(by=trt) %>%
add_overall() %>%
as_gt()

Eric Green
  • 7,385
  • 11
  • 56
  • 102

1 Answers1

1

First, I would use show_header_names() to print the true column name of the "Overall" column. You can use the code below to color the cells using tab_style().

You may also want to look into the gt function data_color()...I find the syntax easier to use.

Happy Programming

library(gtsummary)
#> #BlackLivesMatter
tbl <-
  trial %>% select(trt, age, grade, response) %>%
  tbl_summary(by=trt) %>%
  add_overall()

# print gtsummary column names to get overall column name
show_header_names(tbl)
#> i As a usage guide, the code below re-creates the current column headers.
#>   modify_header(update = list(
#>     label ~ "**Characteristic**",
#>     stat_0 ~ "**Overall**, N = 200",
#>     stat_1 ~ "**Drug A**, N = 98",
#>     stat_2 ~ "**Drug B**, N = 102"
#>   ))
#> 
#> 
#> Column Name   Column Header        
#> ------------  ---------------------
#> label         **Characteristic**   
#> stat_0        **Overall**, N = 200 
#> stat_1        **Drug A**, N = 98   
#> stat_2        **Drug B**, N = 102


# print table with shaded Overall column
as_gt(tbl) %>%             # convert gtsummary table to gt
  gt::tab_style(           # use gt::tab_style() to shade column
    style = list(gt::cell_fill(color = "grey")),
    locations = gt::cells_body(columns = vars(stat_0))
  )

enter image description here

Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28
  • 1
    Thanks for taking the time to answer and teach. {gtsummary} is really impressive. Just learned about it recently. – Eric Green Dec 07 '20 at 14:04