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

如何让R语言中第二个表格拥有第一个表格的网格格式?

问题:让第二个表格匹配第一个的网格格式

参考表格格式:带完整单元格网格线,列表头和行表头背景为灰色,行列标题清晰对齐。

第一个表格的R代码

datos1 = c(12 , 8 , 4 , 6, 4 , 2, 3, 2, 1)
datos11 = c(12 , 8 , 4 ,  3, 2, 1, 6, 4 , 2)
datos2 = c(3 , 6 , 12 , 2, 4 , 8, 1, 2, 4)
datos12 = c(3 , 6 , 12 , 1, 2, 4,2, 4 , 8)
datos3 = c(2 , 1 , 4 , 4, 2 , 8, 6, 3, 12)
datos13 = c(2 , 1 , 4 , 6, 3, 12, 4, 2 , 8)
datos4 = c(4 , 2 , 1 , 8, 4 , 2, 12, 6, 3)
datos14 = c(4 , 2 , 1 , 12, 6, 3, 8, 4 , 2)
datos5 = c(1 , 2 , 4 , 2, 4 , 8, 3, 6, 12)
datos15 = c(1 , 2 , 4 , 3, 6, 12, 2, 4 , 8)
datos6 = c(4 , 8 , 12 , 2, 4 , 6, 1, 2, 3)
datos16 = c(4 , 8 , 12 , 1, 2, 3, 2, 4 , 6)
datos7 = c(1 , 2 , 3 , 2, 4 , 6, 4, 8, 12)
datos17 = c(3 , 2 , 1 , 6, 4 , 2, 12, 8, 4)
datos8 = c(1 , 2 , 3 , 4, 8 , 12, 2, 4, 6)
datos18 = c(2 , 1 , 3 , 4, 2 , 6, 8, 4, 12)
datos9 = c(2 , 4 , 6 , 4, 8 , 12, 1, 2, 3)
datos19 = c(6 , 4 , 2 , 12, 8 , 4, 3, 2, 1)
datos10 = c(2 , 4 , 6 , 1, 2, 3, 4, 8 , 12)
datos20 = c(6 , 2 , 4 , 12, 4, 8, 3, 1 , 2)

w9 <- sample(list(datos1, datos2, datos3, datos4, datos5,datos6,datos7,datos8,datos9,datos10,datos11,datos12,datos13,datos14,datos15,datos16,datos17,datos18,datos19,datos20), 1) 
w9 <- unlist(w9)
tabla = cbind (expand.grid (list (Y = c ("[2,4)","[4,6)","[6,8]"),
X = c ("6","8","9.5" ))), count = w9)
p1<- ftable (xtabs(count ~ Y + X, tabla ))

grid.table(tabla, rows = NULL)

第二个表格的现有代码及问题

初始代码:

p1

添加以下代码后:

colnames(p1) = c("6","8","9.5")
rownames(p1) = c("[2,4)","[4,6)","[6,8]")

以及:

myt <- ttheme_default(
        colhead = list(fg_params=list(col="black"),
                     bg_params=list(fill="grey")),
         rowhead = list(fg_params=list(col="black", fontface="bold",hjust=0.5, x=0.5),
                       bg_params=list(fill="grey")))

grid::grid.newpage()
grid.table(p1[1:3,1:3], theme=myt)

rs = tableGrob(cbind("Y\\X"=rownames(p1)[1:3]), 
               rows=NULL, 
               theme=ttheme_default(core=list(bg_params=list(fill="grey"), 
                                              fg_params=list(fontface="bold"))))

grid::grid.draw(gtable_combine(rs, tableGrob(p1[1:3, 1:3], rows=NULL)))

当前生成的表格缺少完整的网格线,表头对齐和边框样式与参考表格不一致。

解决方案

问题出在gtable_combine拆分组合表格的方式未统一处理网格,且theme未配置完整边框参数。修改后的代码会统一表格样式,添加完整网格线,并正确设置行列表头:

library(gridExtra)
library(grid)

# 确保p1的结构正确
colnames(p1) = c("6","8","9.5")
rownames(p1) = c("[2,4)","[4,6)","[6,8]")

# 构建带行列标题的完整数据框,让grid.table一次性处理
table_data <- cbind(`Y\\X` = rownames(p1), as.data.frame(p1))

# 定义包含完整网格线的自定义theme
custom_theme <- ttheme_default(
  # 列表头样式
  colhead = list(
    fg_params = list(col = "black", fontface = "bold"),
    bg_params = list(fill = "grey90"),
    padding = unit(c(2, 2), "mm")
  ),
  # 行表头样式
  rowhead = list(
    fg_params = list(col = "black", fontface = "bold"),
    bg_params = list(fill = "grey90"),
    padding = unit(c(2, 2), "mm")
  ),
  # 单元格样式,添加网格线
  core = list(
    fg_params = list(col = "black"),
    bg_params = list(fill = "white"),
    padding = unit(c(2, 2), "mm"),
    border = list(col = "black", lwd = 1)
  ),
  # 统一表头边框样式
  colhead_border = list(col = "black", lwd = 1),
  rowhead_border = list(col = "black", lwd = 1)
)

# 绘制符合要求的表格
grid.newpage()
grid.table(table_data, rows = NULL, theme = custom_theme)

说明

  1. 将行标题合并到数据框中,避免拆分组合表格导致的网格错位,让grid.table一次性渲染完整表格。
  2. 在theme中明确配置border参数,给所有单元格、表头添加黑色网格线。
  3. 统一表头的背景色和字体样式,与参考表格保持一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 22:48:13