2

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

Gorka
  • 3,555
  • 1
  • 31
  • 37
  • 1
    Of course, after posting, I found out that you can do it using css styles: modify_caption("
    Table 1. Patient Characteristics
    "). Is this the "canonical" way to do it?
    – Gorka Feb 09 '22 at 10:21
  • if you found a solution you can post it as the answer so it can help others – Mike Feb 09 '22 at 14:12

1 Answers1

1

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

Gorka
  • 3,555
  • 1
  • 31
  • 37