tableplot
作为VisCollin
包的一部分
使用网格图形生成表格的半图形显示,其中代表每个单元格
通过与单元格值成比例的符号,具有各种属性(形状、填充颜色......)。
它使用网格图形视口和布局来工作,为每个单元格绘制适当的标记。 这在 R 或 RStudio 控制台中运行良好。然而,当我在
.Rmd
文档中使用它时,
knitr
为表格中的每个单元格生成一个图形,我被迫使用
块选项 fig.keep = "last"
以便只有最终的数字出现在输出中。
我想知道是否可以在函数中进行一些更改来防止这种行为。
下面显示了功能示意图,省略了大部分细节。 (如果你对这个问题感兴趣的话,看看实际的功能吧,
tableplot
)
tableplot.default <- function( ... ) {
grid.newpage()
#---Create Layout 1 and write main title.
L1 <- grid.layout(2,1,heights=unit(c(3,1),c("lines","null")))
pushViewport(viewport(layout=L1, width=0.95, height=0.98))
pushViewport(viewport(layout.pos.row=1)) ## Push row 1 of Layout 1.
grid.text(title, x=0.02, just=c("left", "bottom"))
upViewport()
#---Create Layout 2.
L2 <- grid.layout(1,2,widths=unit(c(1,1),c("char","null")))
pushViewport(viewport(layout.pos.row=2, layout=L2)) ## Push row 2 of Layout 1.
#---Create Layout 3.
L3 <- grid.layout(dim(values)[1],dim(values)[2],respect=T,just=c("left","top"))
pushViewport(viewport(layout.pos.col=2)) ## Push col 2 of Layout 2;
#---Push Layout 3, but with adjustments to accommodate possible partitions.
pushViewport(viewport(layout=L3, x=0, y=1,
just=c(0,1),
width =unit(1,"npc")-unit(gap,"mm")*(length(v.parts)-1),
height=unit(1,"npc")-unit(gap,"mm")*(length(h.parts)-1)))
#---Draw cellgrams.
for (i in 1:dim(values)[1]){
for (j in 1:dim(values)[2]){
pushViewport(viewport(layout.pos.row=i, layout.pos.col=j))
pushViewport(viewport(just=c(0,1), height=1, width=1,
x=unit(gap,"mm")*v.gaps[j],
y=unit(1,"npc")-unit(gap,"mm")*h.gaps[i]))
cellgram(cell = values[i,j,], ... ) # draw values in cell(i,j)
if ((j==1) && (table.label == TRUE)) {
if (side.rot==0) {grid.text(side.label[i], x=-0.04, just=1, gp=gpar(cex=label.size))}
else {grid.text(side.label[i], x=-0.1, just=c("center"), rot=side.rot, gp=gpar(cex=label.size))}
}
if ((i==1) && (table.label == TRUE)) {grid.text(top.label[j], y=1.05, vjust=0, gp=gpar(cex=label.size))}
upViewport()
upViewport()
}
}
popViewport(0)
}
}
我承认我不太了解网格视口,所以我看不出要更改什么。
有人可以帮忙吗?
正如 Paul Murrell 所说,问题在于函数
cellgram()
称为 grid.edit()
(填充形状),对于表格中的每个单元格,这导致了整个绘图
到那时要重新绘制。
解决方案是删除对
grid.edit()
的调用,并通过填充形状
gp = gpar()
、grid.circle()
等的 grid.polygon()
参数。
修订版位于 cellgram.R
之前有一个这样的代码块显示了这一点:
if (this.shape==0 || this.shape=="circle")
grid.circle(name=paste(shape.name, k, sep=""),
r=abs(s.cell[k]/2),
gp=gpar(col=this.col, lty=this.lty, lwd=0.1) )
现在是:
if (this.shape==0 || this.shape=="circle")
grid.circle(name=paste(shape.name, k, sep=""),
r=abs(s.cell[k]/2),
gp=gpar(col=this.col, lty=this.lty, lwd=0.1,
fill = if(cell[k] == min(cell)) cell.fill else NULL) )
代码有点复杂,因为
tableplot()
函数允许显示最多 4 个值
在每个单元格中。