0

Iv created two merged tables of quantiile regression as explained here

https://yuzar-blog.netlify.app/posts/2022-12-01-quantileregression/

tbl_merge(
    tbls = list(
      tbl_regression(l) %>% bold_p(),
      tbl_regression(q10, se = "nid") %>% bold_p(), 
      tbl_regression(q50, se = "nid") %>% bold_p(),
      tbl_regression(q90, se = "nid") %>% bold_p()
),
    tab_spanner = c("OLS", "QR 10%", "QR 50%", "QR 90%")
  )

Iv named one of the tables PROP since the 3 fits predicts proportions and the other table AREA since the additional 3 fits predict areas.

When merging PROP and AREA, the tab_spanner = c( "QR 10%", "QR 50%", "QR 90%") disappears


AREA <- tbl_merge(
  tbls = list(
    tbl_regression(fit0.10, se = "nid") %>% bold_p(),
    tbl_regression(fit0.50, se = "nid") %>% bold_p(), 
    tbl_regression(fit0.90, se = "nid") %>% bold_p()),
  tab_spanner = c("**QR 10%**", "**QR 50%**", "**QR 90%**"))



PROP <- tbl_merge(
  tbls = list(
    tbl_regression(fit2_0.10, se = "nid") %>% bold_p(),
    tbl_regression(fit2_0.50, se = "nid") %>% bold_p(), 
    tbl_regression(fit2_0.90, se = "nid") %>% bold_p()),
  tab_spanner = c("**QR 10%**", "**QR 50%**", "**QR 90%**"))

AREA_and_PROP <-  tbl_merge(list(AREA,PROP), 
                            tab_spanner = c("**Area**", "**Proportion**"))

I wish to have "**Area**", "**Proportion**" as main headers and "**QR 10%**", "**QR 50%**", "**QR 90%**" as sub headers in each of the main headers

I tried merging AREA and PROP with tab_spanner = FALSE

but it results in displaying the two **QR 10%** fits next to each other and than the two **QR 50%** and **QR 90%**

Thank you!

Humble Newbie
  • 98
  • 1
  • 11
  • gtsummary can't place multiple spanning headers. But you can convert a gtsummary table to gt, and use the gt function `tab_spanner()` to place additional headers. – Daniel D. Sjoberg Apr 10 '23 at 13:31
  • Thank ypu for your prompt response! – Hadar Klein Apr 11 '23 at 13:49
  • From what Iv read on tab_spanner(), to specify the location of the header I need to add the variables in column, but my columns aren't variables - they are the fits (each of the 2 merged table is a merge of 3 rq fits) – Hadar Klein Apr 11 '23 at 13:55

1 Answers1

0

Example below!

library(gtsummary)

tbl <-
  trial |> 
  tbl_summary(
    by = trt, 
    include = age, 
    missing = "no"
  ) |> 
  add_difference() |> 
  modify_spanning_header(
    all_stat_cols() ~ "**Treatment**",
    c(estimate, ci, p.value) ~ "**Comparison**"
  )

# add a second spanning header
# 1. first print the column names
show_header_names(tbl)
#> ℹ As a usage guide, the code below re-creates the current column headers.
#> modify_header(
#>   label = "**Characteristic**",
#>   stat_1 = "**Drug A**, N = 98",
#>   stat_2 = "**Drug B**, N = 102",
#>   estimate = "**Difference**",
#>   ci = "**95% CI**",
#>   p.value = "**p-value**"
#> )
#> 
#> 
#> Column Name   Column Header       
#> ------------  --------------------
#> label         **Characteristic**  
#> stat_1        **Drug A**, N = 98  
#> stat_2        **Drug B**, N = 102 
#> estimate      **Difference**      
#> ci            **95% CI**          
#> p.value       **p-value**

# 2. convert to gt and add second level spanning header
tbl2 <-
  tbl |> 
  as_gt() |> 
  gt::tab_spanner(
    label =  gt::md("**Treatment Summary and Comparison**"),
    columns = c(all_stat_cols(), estimate, ci, p.value),
    level = 2
  )

enter image description here Created on 2023-04-11 with reprex v2.0.2

Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28
  • Thank you very much, Daniel! I appreciate your time in helping me. This example worked wonderfully, creating level 2 headers. Is there a way to change existing level 1 headers? Those were the ones that originally got deleted when I merged the two gttables. – Hadar Klein Apr 12 '23 at 13:46
  • You can update the first level spanning headers with `gtsummary::modify_spanning_header()` or with `gt::tb_spanner()` after converting to gt. – Daniel D. Sjoberg Apr 12 '23 at 15:14