You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言gt包自定义行内水平线绘制问题求助

解决gt包指定行绘制自定义水平线的问题

问题1:自定义水平线不显示的修复

你的代码未显示线条的核心原因是未明确指定边框颜色——全局设置table_body.hlines.color = "transparent"会让未单独指定颜色的边框继承透明属性。只需在cell_borders中添加color参数即可解决:

require(gt)

gt(iris) %>% 
  tab_spanner(label = "Sepal", columns = c(Sepal.Length, Sepal.Width)) %>% 
  tab_spanner(label = "Petal", columns = c(Petal.Length, Petal.Width)) %>%   
  cols_label(Sepal.Length = "Length", Sepal.Width = "Width") %>% 
  tab_options(table_body.hlines.color = "transparent") %>% 
  tab_style(
    style = cell_borders(
      sides = "bottom", 
      weight = px(0.5),
      color = "black"  # 明确指定线条颜色
    ),
    locations = cells_body(rows = c(3, 4, 7))
  )

问题2:指定行仅前三列绘制水平线

直接在cells_body中添加columns参数限定范围即可,支持列索引或列名两种写法:

require(gt)

gt(iris) %>% 
  tab_spanner(label = "Sepal", columns = c(Sepal.Length, Sepal.Width)) %>% 
  tab_spanner(label = "Petal", columns = c(Petal.Length, Petal.Width)) %>%   
  cols_label(Sepal.Length = "Length", Sepal.Width = "Width") %>% 
  tab_options(table_body.hlines.color = "transparent") %>% 
  tab_style(
    style = cell_borders(
      sides = "bottom", 
      weight = px(0.5),
      color = "black"
    ),
    locations = cells_body(
      rows = c(3, 4, 7),
      columns = 1:3  # 用索引指定前三列,也可写列名如c("Length", "Width", "Petal.Length")
    )
  )

内容的提问来源于stack exchange,提问作者Markm0705

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 17:20:31