如何移除gtsummary表格列标题与脚注的默认上标1?
解决gtsummary表格列标题与脚注的上标1问题
问题背景
使用tbl_reg_summary()生成表格时,列标题自动带上标1,设置exponential = FALSE无法消除该标记,同时需要移除脚注首行的上标1。
解决步骤
1. 移除列标题的上标1
gtsummary默认会为分组列标题添加脚注关联上标,通过modify_table_styling()配合字符串替换可直接移除:
modify_table_styling( columns = all_stat_cols(), label = ~ stringr::str_remove(.$label, "\\^1") )
原理:用stringr::str_remove()匹配并删除列标签中的^1(正则转义后为\\^1)。
2. 移除脚注首行的上标1
默认脚注会自动带上标关联标记,先清除默认脚注再添加自定义无标内容即可:
# 清除默认带标的脚注 modify_footnote(all_stat_cols() ~ NA) %>% # 添加自定义无标脚注 modify_footnote(all_stat_cols() ~ "Date of Data Cut: DD-MONTH NAME-YY, N = number of patients in each group; n = number of patients with data available; percentages are based on N.")
完整修改后代码
library(gtsummary) library(stringr) dm_shell <- dm %>% select(-USUBJID) %>% mutate(AGEGR1 = factor(AGEGR1, levels = c("18 - < 65", "65 - < 75", "> = 75"))) %>% tbl_reg_summary( digits = everything() ~ style_xxx, by = TRTPG4, hide_n = TRUE, exponential = FALSE, statistic = all_continuous() ~ c("{N_nonmiss}","{mean}({sd})","{median}","{min},{max}","{N_miss}") ) %>% add_overall(last = TRUE, col_label = "All Patients") %>% bold_labels() %>% modify_header( list( all_stat_cols(stat_0 = FALSE) ~ "**{level}** \n(N=xx)", stat_0 ~ "**All Patients** \n(N=xx)" )) %>% # 移除列标题上标1 modify_table_styling( columns = all_stat_cols(), label = ~ str_remove(.$label, "\\^1") ) %>% add_stat_label(label = AGE ~ c("N","Mean (SD)","Median","Min,Max", "Missing")) %>% # 清除默认脚注并添加自定义内容 modify_footnote(all_stat_cols() ~ NA) %>% modify_footnote(all_stat_cols() ~ "Date of Data Cut: DD-MONTH NAME-YY, N = number of patients in each group; n = number of patients with data available; percentages are based on N.")
内容的提问来源于stack exchange,提问作者Keerthi Konjety
相关产品推荐
相关产品推荐

