将grid.draw npc scale映射到native

时间:2017-11-27 14:49:23

标签: r r-grid

我在textGrob中创建重叠标签

txt <- rep("Hi there",2)
grid.newpage()
fg <- frameGrob()
tg <- textGrob(label = txt,x = c(0.25,0.4),y=c(0.5,0.5))
fg <- packGrob(fg, tg)
grid.draw(fg)

enter image description here

然后我采用相同的textGrob并找到文本边界,convertUnit并在文本周围绘制紧密框并再次绘制我得到的框不会重叠。

为了重现重叠,定义tg_bounds的正确方法是什么?

tg_bounds <- function(tg,lab,theta){
 bounds <- grid:::grid.Call(grid:::L_textBounds, 
                         grDevices::as.graphicsAnnot(tg$label[lab]),
                         tg$x[lab],
                         tg$y[lab], 
                         resolveHJust(tg$just, tg$hjust), 
                         resolveVJust(tg$just,tg$vjust), 
                         tg$rot, theta)
 bounds <- unit(bounds, "inches")
 convertUnit(bounds,unitTo = attr(tg$x,'unit'),valueOnly = TRUE)
}

bounds1 <- do.call('rbind',lapply(seq(1,360,1),tg_bounds,tg = tg,lab = 1))
bounds2 <- do.call('rbind',lapply(seq(1,360,1),tg_bounds,tg = tg,lab = 2))

plot(bounds1[,1],bounds1[,2],type='line',xlim = c(0,.5),ylim = c(0.4,.5))
lines(bounds2[,1],bounds2[,2])

enter image description here

0 个答案:

没有答案