默认情况下,gtsummary 将by组中的观察数量放在该组的标签旁边。这增加了表格的宽度......如果有很多组或很大的 N,表格会很快变得很宽。
是否可以让 gtsummary 在标签下方的单独行上报告 N?例如
> data(mtcars)
> mtcars %>%
+ select(mpg, cyl, vs, am) %>%
+ tbl_summary(by = am) %>%
+ as_tibble()
# A tibble: 6 x 3
`**Characteristic**` `**0**, N = 19` `**1**, N = 13`
<chr> <chr> <chr>
1 mpg 17.3 (14.9, 19.2) 22.8 (21.0, 30.4)
2 cyl NA NA
3 4 3 (16%) 8 (62%)
4 6 4 (21%) 3 (23%)
5 8 12 (63%) 2 (15%)
6 vs 7 (37%) 7 (54%)
Run Code Online (Sandbox Code Playgroud)
会成为
# A tibble: 6 x 3
`**Characteristic**` `**0**` `**1**`
<chr> <chr> <chr>
1 N = 19 N = 13
2 mpg 17.3 (14.9, 19.2) 22.8 (21.0, 30.4)
3 cyl NA NA
4 4 3 (16%) 8 (62%)
5 6 4 (21%) 3 (23%)
6 8 12 (63%) 2 (15%)
7 vs 7 (37%) 7 (54%)
Run Code Online (Sandbox Code Playgroud)
(我只使用 as_tibble 以便通过手动编辑它很容易显示我的意思......)
任何的想法?
谢谢!
这是您可以执行此操作的一种方法:
library(tidyverse)
library(gtsummary)
mtcars %>%
select(mpg, cyl, vs, am) %>%
# create a new variable to display N in table
mutate(total = 1) %>%
# this is just to reorder variables for table
select(total, everything()) %>%
tbl_summary(
by = am,
# this is to specify you only want N (and no percentage) for new total variable
statistic = total ~ "N = {N}") %>%
# this is a gtsummary function that allows you to edit the header
modify_header(stat_by = "**{level}**")
Run Code Online (Sandbox Code Playgroud)
total)gtsummary::modify_header()从标题中删除 N此外,如果您使用flextable 打印引擎,则可以在标题本身中添加换行符:
mtcars %>%
select(mpg, cyl, vs, am) %>%
# create a new variable to display N in table
tbl_summary(
by = am
# this is to specify you only want N (and no percentage) for new total variable
) %>%
# this is a gtsummary function that allows you to edit the header
modify_header(stat_by = "{level}\nN = {N}") %>%
as_flex_table()
Run Code Online (Sandbox Code Playgroud)
祝你好运!
| 归档时间: |
|
| 查看次数: |
505 次 |
| 最近记录: |