问题
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