Search code examples
rgtsummaryflextablequarto

Specify Column With in R's gtsummary with tbl_cross?


I am creating a cross-tabulation table in R with the tbl_cross funtion using @daniel-d-sjoberg's gtsummary package. I am trying to figure out how to specify the output column widths with gtsummary so I can modify the width of the first column in the table. In addition, I want to take advantage of gtsummary's formatting options like bolding and breaking table captions over two lines (by specifying " \n" in the modify_caption statement). The problem is, I can't seem to break a caption over multiple lines AND specify a column width. My starting point was to use the following code, which breaks the caption, correctly, onto two lines:

library(tidyverse)
library(flextable)
library(gtsummary)

mytable <- iris %>% 
          mutate(Long.Petal = ifelse(Petal.Width > .2, "Yes", "No")) %>% 
        tbl_cross(
          row =  Long.Petal,
          col = Species,
          percent = "cell"
        ) %>% 
         modify_caption("This is line 1  \n  This is lin 2")

This outputs the following table:

enter image description here

After reviewing the documentation, it looks like the only way I can find to modify the column widths is by converting the table to a flextable using gtsummary's as_flex_table and then specifying the column widths. So, to do this, I modified the code above to change the width of the first column to 3 inches by adding two additional lines of code as indicated in the comments in the revised code below:

library(tidyverse)
library(flextable)
library(gtsummary)

mytable <-   iris %>% 
      mutate(Long.Petal = ifelse(Petal.Width > .2, "Yes", "No")) %>% 
    tbl_cross(
      row =  Long.Petal,
      col = Species,
      percent = "cell"
    ) %>% 
  modify_caption("This is line 1  \n  This is lin 2") %>% 
  as_flex_table() %>%   #NEW CODE LINE 1
  width(., 1, 3)        #NEW CODE LINE 2

mytable

This code produces the output below, which has now incorrectly placed lines 1 and 2 of the table caption onto a single line.

enter image description here

Is there a way, preferably in gtsummary with tbl_cross or its options to specify the column widths AND break a table caption across multiple lines?


Solution

  • Thanks to the hint of @DanielD.Sjoberg now we can do it this way. We first must transfer the gt_summary tbl to a gt object using as_gt() (and not only gt() -> this won't work) then we can use cols_width() function:

    What is also important to select the correct column we could do: taken from @DanielD.Sjoberg comment above:

    • "first column is named label. You can see all the underlying colnames with show_header_names(). There are a bunch of hidden columns ,so we can reference the column by their index number.

    See here https://gt.rstudio.com/reference/cols_width.html

      
    library(tidyverse)
    library(gtsummary)
    library(gt)
    
    iris %>% 
      mutate(Long.Petal = ifelse(Petal.Width > .2, "Yes", "No")) %>% 
      tbl_cross(
        row =  Long.Petal,
        col = Species,
        percent = "cell"
      ) %>% 
      modify_caption("This is line 1  \n  This is lin 2") %>% 
      as_gt() %>% 
      cols_width(
        "label"~ px(250)
      ) 
    

    enter image description here