mapping grid.draw npc scale to native

China☆狼群 提交于 2019-12-24 16:58:52

问题


i am creating overlapping labels in a 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)

then i take the same textGrob and find the text bounds, convertUnit and draw the tight boxes around the text and plot again i get boxes that dont overlap.

What is the correct way to define tg_bounds in order to reproduce the overlap?

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])

来源:https://stackoverflow.com/questions/47513710/mapping-grid-draw-npc-scale-to-native

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!