[英]Specify Column With in R's gtsummary with tbl_cross?
我正在使用@daniel-d-sjoberg 的 gtsummary package 使用 tbl_cross 函数在 R 中创建一个交叉制表表。我试图弄清楚如何使用 gtsummary 指定 output 列宽,以便我可以修改第一列的宽度桌子。 此外,我想利用 gtsummary 的格式化选项,例如在两行上加粗和断开表格标题(通过在 modify_caption 语句中指定“\n”)。 问题是,我似乎无法打破多行的标题并指定列宽。 我的出发点是使用以下代码,将标题正确地分成两行:
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")
这将输出下表:
查看文档后,看起来我能找到修改列宽的唯一方法是使用 gtsummary 的 as_flex_table 将表转换为 flextable,然后指定列宽。 因此,为此,我修改了上面的代码,通过添加两行额外的代码将第一列的宽度更改为 3 英寸,如下面修改后的代码中的注释所示:
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
此代码生成下面的 output,它现在错误地将表格标题的第 1 行和第 2 行放在一行中。
有没有一种方法,最好是在 gtsummary 中使用 tbl_cross 或其选项来指定列宽并跨多行拆分表格标题?
在 @akrun 和 @daniel-d-sjoberg 的有用建议之后,我能够使用以下代码在 Quarto 中获得此工作版本和 output 到 Word:
---
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
```
这产生了:
感谢大家的帮助!
更新
在@TarJae 的回答之后,我意识到这个解决方案不支持 gtsummary package 的标题中的汇总统计。因此,我决定稍微修改这个解决方案并提供一个允许用户包含这些的解决方案。 这可以通过以下方式完成:
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")
我在这里所做的就是使用 gtsummary 最初生成 label。 然后,我将表格转换为可伸缩表格,并从它生成的标题中提取可伸缩标题(即上例中的 mytemptable$table_styling$caption 1 )。
感谢@DanielD.Sjoberg 的提示,我们现在可以这样做了。 我们首先必须使用as_gt()
将 gt_summary tbl 传输到 gt object(不仅是gt()
-> 这不起作用)然后我们可以使用cols_width()
function:
对于 select 我们可以做的正确专栏也很重要:摘自上面的 @DanielD.Sjoberg 评论:
label
您可以使用show_header_names()
查看所有底层列名。有一堆隐藏的列,因此我们可以通过它们的索引号引用该列。看这里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)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.