如何修改gt表格中分组行样式(将分组标题设为粗体)
实现gt表格中特定分组标题的加粗效果
你需要针对newname列里的Consumer Non-Durables、Healthcare、Hitech等分组标题单独设置粗体,可通过gt包的tab_style配合行匹配实现,修改后的完整代码如下:
# Create a table using gt table <- wide_summary_df %>% gt() %>% tab_header( title = "Summary Table", subtitle = "Total Acqprop by Year, Newname, and Decile" ) %>% cols_label( newname = "Newname", decile = "Deciles per sector" ) %>% fmt_percent(decimals = 1, columns = c("1985", "1990", "1995", "2001", "2008", "2020" )) %>% # 给decile列设置粗体(保留原有逻辑) tab_style( style = cell_text(weight = "bold"), locations = cells_body( columns = decile )) %>% # 新增:给指定分组标题设置粗体 tab_style( style = cell_text(weight = "bold"), locations = cells_body( columns = newname, rows = newname %in% c("Consumer Non-Durables", "Healthcare", "Hitech") )) # Print the table table
关键说明:
- 新增的
tab_style块通过rows = newname %in% c(...)精准定位需要加粗的分组行 - 若后续需要添加更多加粗的分组名称,直接在向量中补充即可
内容的提问来源于stack exchange,提问作者Ayoze Alfageme
相关产品推荐
相关产品推荐

