gtable_add_grob():linesGrob()未显示 [英] gtable_add_grob(): linesGrob() not displayed
本文介绍了gtable_add_grob():linesGrob()未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么在下图中未绘制线(通过linesGrob
)?
Why are the lines (via linesGrob
) not drawn in the following plot?
require(gtable)
base <- gtable(widths=unit(rep(1, 2), "null"),
heights=unit(rep(1, 3), "null"))
grid.newpage()
g <- 1
for(i in 1:3) {
for(j in 1:2) {
base <- gtable_add_grob(base,
grobs=list(linesGrob(x=1:4, y=4:1),
rectGrob(gp=gpar(fill="#FF0000")),
textGrob(label=g)), i, j, name=1:3)
g <- g+1
}
}
grid.draw(base)
推荐答案
两个原因:
-
坐标落在视口之外
the coordinates fall outside the viewport
rectGrob被绘制在顶部并对其进行遮盖
the rectGrob is drawn on top and masks it
require(gtable)
# let's fix this name before it's too late
gtable_add_grobs <- gtable_add_grob
base <- gtable(widths=unit(rep(1, 2), "null"),
heights=unit(rep(1, 3), "null"))
grid.newpage()
g <- 1
for(i in 1:3) {
for(j in 1:2) {
base <- gtable_add_grobs(base,
grobs=list(rectGrob(gp=gpar(fill="#FF0000")),
linesGrob(x=1:4, y=4:1, def="native",
vp=dataViewport(1:4, 1:4)),
textGrob(label=g)), i, j, name=1:3)
g <- g+1
}
}
grid.draw(base)
注意 gtable_add_grobs
是矢量化的,这意味着理想情况下不必使用循环.如果将给定单元格中的所有杂项首先分组到gTree中,将更容易.这是一个简化的版本,
Note gtable_add_grobs
is vectorised, which means you shouldn't have to use for loops, ideally. It's easier if you group all the grobs together first, for a given cell, in a gTree. Here's a simplified version,
library(gtable)
g <- gtable(widths = unit(c(1,1), "null"),
heights = unit(c(1,1), "null"))
cell <- function(ii)
grobTree(rectGrob(),
linesGrob(1:4, 1:4, default.units="native"),
textGrob(ii),
vp=dataViewport(c(1,4), c(1,4)))
gl <- lapply(1:4, cell)
xy <- expand.grid(1:2, 1:2)
g <- gtable_add_grobs(g, gl,
l=xy[,1],
r=xy[,1],
t=xy[,2],
b=xy[,2])
grid.newpage()
grid.draw(g)
这篇关于gtable_add_grob():linesGrob()未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文