5

I am trying to use the tbl_stack function from the gtsummary package, but the output text from using the group_header argument looks too visually similar to the rest of the labels. Ideally I would like to format the text with either bold, underline, or change the size to make it clear that I am displaying a separate section. Is there any way to accomplish group_header text formatting in the gtsummary package?

In the below example (taken from this issue: https://github.com/ddsjoberg/gtsummary/issues/669), I would ideally like the words "Demographics" and "Lab Values" to be in bold.

library(gtsummary)

tt <-
  trial %>%
  select(trt, response, age) %>%
  tbl_summary(by = trt, missing = "no") 

# stacking two tbl_summary objects with header between
tbl_stack(list(tt, tt), group_header = c("Demographics", "Lab Values"))

enter image description here

andrewsris
  • 65
  • 4

1 Answers1

7

Those kind of style changes are available within the {gt} package. Convert the {gtsummary} table to {gt} using as_gt(), then make use of the {gt} styling functions. Example bolding below!

library(gtsummary)

tt <-
  trial %>%
  select(trt, response, age) %>%
  tbl_summary(by = trt, missing = "no") 

# stacking two tbl_summary objects with header between
tbl_stack(list(tt, tt), group_header = c("Demographics", "Lab Values")) %>%
  as_gt() %>%
  gt::tab_style(
    style = gt::cell_text(weight = "bold"),
    locations = gt::cells_row_groups(groups = everything())
  )

enter image description here

Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28
  • Thank you very much! This exactly solves my issue. I wanted to ask how you had saved the table as png. Using the function gt::gtsave("tt.png") deleted all of the formatting and led to an image very similar to the one I originally posted. `tbl_stack(list(tt, tt), group_header = c("Demographics", "Lab Values")) %>% as_gt() %>% gt::tab_style( style = gt::cell_text(weight = "bold"), locations = gt::cells_row_groups(groups = everything()) ) %>% gt::gtsave("tt.png")` – andrewsris Jul 07 '21 at 23:59
  • I use `gt::gt:save()` as well, and tested the code on this example....the images save with no issues on my end. If you can create a reproducible example, I suggest filing an GitHub issue on the gt GitHub site. – Daniel D. Sjoberg Jul 08 '21 at 01:15
  • Thank you very much! I guess this is a known issue in the gt package that requires a workaround for now until future iterations of the gt package fix this issue. https://github.com/rstudio/gt/issues/621 Thanks for your help! – andrewsris Jul 08 '21 at 01:53
  • Ah, i see! I have webshot2 installed! – Daniel D. Sjoberg Jul 08 '21 at 12:49
  • Does anyone have advice on using this technique while maintaining the ident? As soon as I go to `as_latex` from here I lose all the indentation. – John-Henry Aug 18 '23 at 21:23