gtable:在表格主体中的所有单元格周围画一条黑线 [英] gtable: Put a black line around all cells in the table body

查看:221
本文介绍了gtable:在表格主体中的所有单元格周围画一条黑线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用gridExtra在表的主体上放置一条较粗的线(例如lwd = 2尺寸).这是根据本文档页面略微修改的MWE. gtable正在后台进行工作,但是gtable的文档很薄;查看代码并没有多大帮助.

I'm trying to put a relatively heavy line (say lwd = 2 size) around the body of a table using gridExtra. Here's a MWE slightly modified from this documentation page. gtable is doing the work under the hood but the documentation for gtable is thin; looking at the code didn't help much.

g <- tableGrob(iris[1:4, 1:3], rows = NULL)
separators <- replicate(1, 
    segmentsGrob(x1 = unit(0,"npc")), 
    simplify=FALSE)
g <- gtable::gtable_add_grob(g, grobs = separators, 
    t = 1, b = nrow(g), l = 1)
g <- gtable::gtable_add_grob(g, grobs = separators, 
    t = 1, b = nrow(g), r = 3) # error: no default for l
grid.draw(g)

我正在尝试获得这样的效果(不同的数据):

I'm trying to get an effect like this (different data):

gtable::gtable_add_grob的第二次调用给出了一个错误,因此很显然,我简单的想法r = 3将把该行放在第三列的右侧是天真的.关于如何使桌子周围的线条更粗的任何建议?如果我能理解左右边缘的想法,我认为可以类似地绘制顶线和底线.

The 2nd call to gtable::gtable_add_grob gives an error, so obviously my simple notion that r = 3 will put the line on the right of the 3rd column is naive. Any suggestions as to how to get a heavier line around the body of the table? If I can grok the idea for the left and right edges I assume the top and bottom lines can be drawn analogously.

旁注:gridExtra最近被修改为使用gtable.以前,我使用与下面类似的命令制作了类似于上图的图形.我想尽可能地接近相同的设置(新方法的行以灰度级交替显示,这样更好;我真的很喜欢旧的show.box功能).

Side note: gridExtra was recently revised to use gtable. Previously I had made graphics like the one above with commands similar to those below; I'd like to get pretty close to the same settings if possible (the new method has rows alternating in grey level, that's nicer; I'm really after the old show.box feature).

myt <- gridExtra::tableGrob(aov1, show.box = TRUE,
                show.rownames = TRUE, show.colnames = TRUE,
                show.csep = TRUE, show.rsep = TRUE,
                separator = "black", gp = grid::gpar(cex = table[3]))

推荐答案

您可以添加一个rectGrob,跨越要关进笼子的所有单元格,

You could add a rectGrob spanning all the cells you want caged,

library(gridExtra)
library(gtable)
library(grid)
g <- tableGrob(iris[1:4, 1:3])
g <- gtable::gtable_add_grob(g, 
                             grobs = rectGrob(gp=gpar(fill=NA, 
                                                      lwd=2)), 
                             t = 2, b = nrow(g), l = 2, r = ncol(g))
grid.newpage()
grid.draw(g)

这篇关于gtable:在表格主体中的所有单元格周围画一条黑线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆