【问题标题】:add a background color to the "Overall" column in gtsummary table为 gtsummary 表中的“Overall”列添加背景颜色
【发布时间】:2020-12-07 02:52:32
【问题描述】:

如何使用tab_style() 为“整体”列添加背景颜色?

library(gtsummary)
trial %>% select(trt, age, grade, response) %>%
tbl_summary(by=trt) %>%
add_overall() %>%
as_gt()

【问题讨论】:

    标签: r gtsummary gt


    【解决方案1】:

    首先,我将使用show_header_names() 打印“总体”列的真实列名。您可以使用下面的代码使用tab_style() 为单元格着色。

    您可能还想查看 gt 函数 data_color()...我发现语法更易于使用。

    快乐编程

    library(gtsummary)
    #> #BlackLivesMatter
    tbl <-
      trial %>% select(trt, age, grade, response) %>%
      tbl_summary(by=trt) %>%
      add_overall()
    
    # print gtsummary column names to get overall column name
    show_header_names(tbl)
    #> i As a usage guide, the code below re-creates the current column headers.
    #>   modify_header(update = list(
    #>     label ~ "**Characteristic**",
    #>     stat_0 ~ "**Overall**, N = 200",
    #>     stat_1 ~ "**Drug A**, N = 98",
    #>     stat_2 ~ "**Drug B**, N = 102"
    #>   ))
    #> 
    #> 
    #> Column Name   Column Header        
    #> ------------  ---------------------
    #> label         **Characteristic**   
    #> stat_0        **Overall**, N = 200 
    #> stat_1        **Drug A**, N = 98   
    #> stat_2        **Drug B**, N = 102
    
    
    # print table with shaded Overall column
    as_gt(tbl) %>%             # convert gtsummary table to gt
      gt::tab_style(           # use gt::tab_style() to shade column
        style = list(gt::cell_fill(color = "grey")),
        locations = gt::cells_body(columns = vars(stat_0))
      )
    

    【讨论】:

    • 感谢您抽出宝贵时间来回答和教导。 {gtsummary} 确实令人印象深刻。最近才知道。
    猜你喜欢
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多