Format caption of gtsummary tables

Viewed 292

Hopefully this is not something very obvious I missed, but I am having a hard time finding how to format the caption of gtsummary tables.

I am trying to reproduce the table found here. I love the left aligned grey caption: https://www.danieldsjoberg.com/gtsummary/articles/tbl_summary.html#gtsummary-functions-to-format-table

enter image description here

I am using the following code, but the caption Table 1. Patient Characteristics is shown centered and in black ink, but in the example is left aligned and grey.

How can you format the caption of the gtsummary tables?

library(gtsummary)
trial2 <- trial %>% select(trt, age, grade)

trial2 %>%
  tbl_summary(by = trt) %>%
  add_p(pvalue_fun = ~style_pvalue(.x, digits = 2)) %>%
  add_overall() %>%
  add_n() %>%
  modify_header(label ~ "**Variable**") %>%
  modify_spanning_header(c("stat_1", "stat_2") ~ "**Treatment Received**") %>%
  modify_footnote(
    all_stat_cols() ~ "Median (IQR) or Frequency (%)"
  ) %>%
  modify_caption("**Table 1. Patient Characteristics**") %>%
  bold_labels() 

enter image description here

1 Answers

Following @mike's advice, I'll post my answer here, so it's easier to find for others.

As you can see in the code below, you can use a bit of css styling in modify_caption().

library(gtsummary)
trial2 <- trial %>% select(trt, age, grade)

trial2 %>%
  tbl_summary(by = trt) %>%
  add_p(pvalue_fun = ~style_pvalue(.x, digits = 2)) %>%
  add_overall() %>%
  add_n() %>%
  modify_header(label ~ "**Variable**") %>%
  modify_spanning_header(c("stat_1", "stat_2") ~ "**Treatment Received**") %>%
  modify_footnote(
    all_stat_cols() ~ "Median (IQR) or Frequency (%)"
  ) %>%
  modify_caption("**Table 1. Patient Characteristics**") %>%
  bold_labels() %>% 
  modify_caption("<div style='text-align: left; font-weight: bold; color: grey'> Table 1. Patient Characteristics</div>")

enter image description here

Related