如何为gt表格中的Primary Localisation手动添加下划线
解决方案
要给Primary Localisation跨列标题添加下划线,只需调整跨列标题的样式设置,确保样式正确生效。以下是修改后的完整代码:
library(gt) library(gtsummary) tbl_trial <- trial |> tbl_summary(by = trt, includ = c(age, grade)) |> add_p(pvalue_fun = label_style_pvalue(digits = 2)) |> add_overall() |> add_n() |> modify_header(label ~ "**Variable**") |> modify_spanning_header(c("stat_1", "stat_2") ~ "**Treatment Received**") |> modify_footnote_header("Median (IQR) or Frequency (%)", columns = all_stat_cols()) |> bold_labels() |> modify_spanning_header(all_stat_cols() ~ "**Primary Localisation**") |> as_gt() |> # 移除所有默认线条 gt::opt_table_lines(extent = "none") tbl_trial |> gt::tab_header( title = gt::md("TABLE 2. Tumor characteristics") ) |> gt::tab_style( style = gt::cell_text(align = "left", weight = "bold", color = "white"), locations = gt::cells_title(groups = "title") ) |> gt::tab_style( style = gt::cell_fill(color = "#0066b3"), locations = gt::cells_title(groups = "title") ) |> # 为Primary Localisation添加细蓝底线 gt::tab_style( style = cell_borders( sides = "bottom", color = "#0066b3", weight = px(1) ), locations = gt::cells_column_spanners() ) |> # 列标签下方的粗蓝线 gt::tab_style( style = gt::cell_borders( sides = "bottom", color = "#0066b3", weight = px(3) ), locations = gt::cells_column_labels() ) |> # 表格底部的粗蓝线 tab_style( style = cell_borders( sides = "bottom", color = "#0066b3", weight = px(3) ), locations = gt::cells_body(rows = nrow(tbl_trial[["_data"]])) )
说明
cells_column_spanners()会选中所有跨列标题,包括外层的Primary Localisation和内层的Treatment Received。由于Treatment Received位于列标签上方,它的底线会被后续列标签的粗线覆盖,最终只会显示Primary Localisation下方的细蓝线,正好符合需求。
内容的提问来源于stack exchange,提问作者TarJae
相关产品推荐
相关产品推荐

