如何使用tab_style()
将背景颜色添加到“整体”列?
library(gtsummary)
trial %>% select(trt, age, grade, response) %>%
tbl_summary(by=trt) %>%
add_overall() %>%
as_gt()
首先,我将使用show_header_names()
打印“Overall”列的真实列名。您可以使用下面的代码为单元格着色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))
)