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:
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 chane 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.
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?
CodePudding user response:
We may use set_caption
from flextable
iris %>%
mutate(Long.Petal = ifelse(Petal.Width > .2, "Yes", "No")) %>%
tbl_cross(
row = Long.Petal,
col = Species,
percent = "cell"
) %>%
as_flex_table() %>%
set_caption(caption = "This is line 1 <br/>This is line 2",
html_escape = FALSE)
-output
CodePudding user response:
After the helpful suggestions from both @akrun and @daniel-d-sjoberg, I was able to get a version of this working and output to Word in Quarto using the following code:
---
title: "Untitled"
format: docx
editor: visual
#filters:
# - docx-landscape.lua
---
```{r}
#| echo: false
#| message: false
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"
) %>%
as_flex_table() %>% #NEW CODE LINE 1
width(., 1, 1) %>% #NEW CODE LINE 2
set_caption(
as_paragraph(
as_chunk("caption \n caption 2", props = fp_text_default(font.family = "Cambria"))
), word_stylename = "Table Caption")
mytable
```
This produced:
Thanks for everyone's help!
UPDATE
After @TarJae's answer, I realized this solution doesn't support summary statistics in the captions from the gtsummary package. So, I've decided to slightly modify this solution and provide one that will allow the user to include these. This can be accomplished as follows:
library(gtsummary)
library(tidyverse)
library(flextable)
#| echo: false
mytemptable<-iris %>%
mutate(Long.Petal = ifelse(Petal.Width > .2, "Yes", "No")) %>%
tbl_cross(
row = Long.Petal,
col = Species,
percent = "cell"
) %>%
modify_caption("First Line of the Caption \n Second Line with the total sample size is {N}")
mytemptable %>%
as_flex_table() %>%
set_caption(
as_paragraph(
as_chunk(mytemptable$table_styling$caption[1], props = fp_text_default(font.family = "Cambria"))
), word_stylename = "Table Caption")
All I've done here is to initially generate the label using gtsummary. Then I convert the table to a flextable and pull in the flextable caption from the caption generated by by it (i.e., mytemptable$table_styling$caption