【问题标题】:How do I add subscripts to labels in tables using the gtsummary package in R?如何使用 R 中的 gtsummary 包为表中的标签添加下标?
【发布时间】:2020-06-17 10:14:59
【问题描述】:

我想在使用 R 包 gtsummary 中的 tbl_regression 生成的回归汇总表的变量名称中引入下标。任何人都可以提供有关如何执行此操作的指导吗?用于生成表格的代码和生成的表格如下所示。我希望标签“NO2”显示为“NO2”。

library(tidyverse)
library(gtsummary)

case <- c(0,1,0,0,0,0)
no2 <- c(17, 14, 8, 9, 9, 7)
df <- data.frame(case, no2)

mod_adj <- glm(case~no2,data=df, family="binomial")

regression_table_adj <- mod_adj %>% 
  tbl_regression(exponentiate = TRUE,  label = list(no2~"NO2"))

regression_table_adj

reprex package (v0.3.0) 于 2020 年 3 月 4 日创建

A table produced using tbl_regression in the R package gtsummary showing a coefficient name that I want to contain a numeric subscript

【问题讨论】:

    标签: r gtsummary


    【解决方案1】:

    这不是我的,但我在这里找到了答案并对其进行了调整。 https://community.rstudio.com/t/subscripts-to-annotate-gt-table/87089

    基本上,您会将其转换为 gt 对象并修改标签,使其打印为下标。

    library(tidyverse)
    library(gtsummary)
    library(gt)
    
    case <- c(0,1,0,0,0,0)
    no2 <- c(17, 14, 8, 9, 9, 7)
    df <- data.frame(case, no2)
    
    mod_adj <- glm(case~no2,data=df, family="binomial")
    
    regression_table_adj <- mod_adj %>% 
      tbl_regression(exponentiate = TRUE,  label = list(no2 = "NO@2~")) %>%
      as_gt() %>%
      text_transform(
        locations = cells_body(),
        fn = function(x) {
          str_replace_all(x,
                          pattern = "@",
                          replacement = "<sub>") %>% 
            str_replace_all("~",
                            "</sub>") }
      )
    
    

    Image of Table

    是的,符号可以轻松替换。我认为使用 '~' 以外的其他东西会更好,因为它很容易意味着其他东西。

    【讨论】:

      【解决方案2】:

      gtsummary 包默认使用 gt 包来打印表格。我查看了他们的文档,但没有看到在表格正文中包含下标的方法。

      好消息是 gtsummary 还支持通过 as_kable() 函数使用 knitr::kable() 打印表格。您可以在两个波浪号之间换行以使其成为下标(例如label = list(no2 ~ "NO~2~"))。在 R 降价文件中使用下面的代码,你应该得到下标。使用 kable 的缺点是它不支持脚注、缩进和跨页眉。快乐编码!

      library(gtsummary)
      
      case <- c(0,1,0,0,0,0)
      no2 <- c(17, 14, 8, 9, 9, 7)
      df <- data.frame(case, no2)
      
      mod_adj <- glm(case~no2,data=df, family="binomial")
      
      mod_adj %>% 
        tbl_regression(
          exponentiate = TRUE,  
          label = list(no2 ~ "NO~2~")
        ) %>% 
        as_kable()
      

      reprex package (v0.3.0) 于 2020 年 3 月 4 日创建

      【讨论】:

      • 是否可以在同一张表中组合多个单变量逻辑回归模型(即年龄(连续)、性别(因子)等......
      • 嗨@sar!是的,有一个功能就是为了这个。在此处查看文档:danieldsjoberg.com/gtsummary/reference/tbl_uvregression.html
      • 是否可以垂直或水平堆叠多个具有相同变量但结果不同的模型?
      猜你喜欢
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      • 2021-07-19
      • 1970-01-01
      • 2021-02-15
      • 1970-01-01
      • 2021-08-06
      • 2022-11-25
      相关资源
      最近更新 更多