1

Consider the example below:

library(tidyverse)
library(gtsummary)

    t1 <- trial %>%
      na.exclude() %>%
      lm(marker ~ age + ttdeath, .) %>%
      tbl_regression()
    t2 <- trial %>%
      na.exclude() %>%
      lm(marker ~ age + response + death, .) %>%
      tbl_regression()
    t3 <- trial %>%
      na.exclude() %>%
      lm(marker ~ age + stage + death, .) %>%
      tbl_regression()
    t4 <- trial %>%
      na.exclude() %>%
      lm(marker ~ stage + grade + death, .) %>%
      tbl_regression()
    
    tbl_merge(list(t1, t2, t3 ,t4))

enter image description here

In the characteristic tab, variables are only alphabetically within a particular model. For example, when looking only at Table 1 is fine, it's A-M-T.

However, when looking at mutiple tables at once, the order is not alphabetical. Is there a way to sort variables alphabetically within all the tables?

E.g. Age, Grade, Months to Death/Censor, Patient Died...

I assume the way to do this is by gtsummary::modify_table_body into dplyr::arrange, but I don't know how to do it exactly.

Any help would be appreciated. Thanks in advance.

Edit: actually, anyway to manually modify the order of variables would be helpful as well, ignore this if it's too difficult.

Edit2: I forgot to mention that there's glance statistics involved as well, such as add_glance_table(adj.r.squared) %>% modify_table_body(~.x %>% arrange(row_type == "glance_statistic")) (which is not in this example), so I imagine there is a possibility that the sorting of variables messes with the order of glance statistics as well. Excuse me for making this complicated, but it would be extremely helpful.

r_noobie
  • 127
  • 6

2 Answers2

3

You're correct that modify_table_body() is the way to go. You'll first want to arrange the order to put the glance table at the bottom, then you arrange by "var_label" to put the variables in alphabetical order. "var_label" is a column in .$table_body. Example below!

library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.5.0'

t1 <- trial %>%
  lm(marker ~ age + ttdeath, .) %>%
  tbl_regression() %>%
  add_glance_table(adj.r.squared)
t2 <- trial %>%
  lm(marker ~ age + response + death, .) %>%
  tbl_regression() %>%
  add_glance_table(adj.r.squared)
t3 <- trial %>%
  lm(marker ~ age + stage + death, .) %>%
  tbl_regression() %>%
  add_glance_table(adj.r.squared)
t4 <- trial %>%
  lm(marker ~ stage + grade + death, .) %>%
  tbl_regression() %>%
  add_glance_table(adj.r.squared)

tbl <- 
  tbl_merge(list(t1, t2, t3 ,t4)) %>%
  modify_table_body(
    ~.x %>% 
      dplyr::arrange(
        row_type == "glance_statistic", # sort glance table to bottom
        var_label                       # sort by the variable label (a hidden column) 
      )
  )

enter image description here Created on 2021-12-19 by the reprex package (v2.0.1)

Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28
3

It is obvious that the solution by @Daniel D. Sjoberg is best. But here is an alternative strategy that is I think more flexible.

Daniel D. Sjoberg provided the hint using tbl_stack() Supress table header in tbl_regression of gt_summary

Here is how it works:

  1. First use tbl_merge as you did
  2. Then use tbl_split in each variable row of the table. You will get a list.
  3. Assign names to the list elements
  4. then use tbl_stack in the order you wish!
library(tidyverse)
library(gtsummary)

x <- tbl_merge(list(t1, t2, t3 ,t4))

y <- x %>% 
  tbl_split(variables = c(age, ttdeath, response, death, stage, grade)) 

age <- y[[1]]
ttdeath <- y[[2]]
response <- y[[3]]
death <- y[[4]]
stage <- y[[5]]
grade <- y[[6]]

tbl_stack(list(age, grade, ttdeath, death, response, stage))

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • 1
    I've found a possibly better way to do this (in conjunction with Daniel), which is taken from [link]https://stackoverflow.com/questions/46129322/arranging-rows-in-custom-order-using-dplyr/46129338. You can `modify_table_body( ~.x %>% dplyr::arrange( match(var_label, c("variable_name_1", "variable_name_2", [...])) )` to custom modify your variable orders by hand! – r_noobie Dec 20 '21 at 04:47
  • That was my first solution ever. But the thing is that you can not get a gt_summary table from a tibble or dataframe. – TarJae Dec 20 '21 at 04:50