Domanda

Why does determining the size (in mm, for example) of a points grob (pointsGrob) fail, but not for a text grob (textGrob)?

### puzzled about grobs (not) knowning how big they are:
require(grid)
convertUnit(grobWidth(textGrob("some tex")), "mm") # 16.93mm => fine
convertUnit(grobWidth(pointsGrob(0.5, 0.5)),  "mm") # 0mm => ?
convertUnit(grobWidth(pointsGrob(0.5, 0.5, size=unit(3, "mm"))), "mm") ## still 0mm...

The reason why I am asking is: If you place a text grob and a points grob side-by-side, and change the value of cex, then suddenly the two grobs overlap (unwanted behavior).

Here is an example showing a similar prolem:

gt <- grobTree(pointsGrob(x=.5, y=.5,  gp=gpar(cex=4)),
               linesGrob(x=0:1, y=.5,  gp=gpar(cex=4)),
               pointsGrob(x=.5, y=.5,  gp=gpar(cex=1)))
pg <- packGrob(frameGrob(vp=NULL), gt,
               width = unit(1, "char"),
               height = unit(1, "char"))
grid.newpage()
grid.draw(pg)
grid.rect(width=grobWidth(pg), height=grobHeight(pg), gp=gpar(col="red"))

The rectangle reveals that the grob width and height are not correct; pg does not "see" the size of the point with large cex. How can this be achieved?

È stato utile?

Soluzione

I do not know how to solve the problem of zero point size, presumably it would have to be defined in the internals of the grid source code at the C level.

However, I want to point out that regardless of the pointsGrob issue, the grobWidth and grobHeight are not defined for your packGrob / gTree and the approach would fail even if pointsGrob were replaced by a textGrob. You probably want to define a gTree of a new class, say "mygrob", and define your own widthDetails.mygrob and heightDetails.mygrob methods,

library(grid)

gt <- grobTree(linesGrob(x=c(0.2, 0.8), y=.5,  gp=gpar(col="grey", lwd=10)),
               textGrob("some label",  gp=gpar(cex=2)),
               cl = "mygrob")

widthDetails.mygrob <- function(x)
  do.call(max, lapply(x$children, grobWidth))


heightDetails.mygrob <- function(x)
  do.call(max, lapply(x$children, grobHeight))

grid.newpage()
grid.draw(gt)
grid.rect(width=grobWidth(gt), height=grobHeight(gt), gp=gpar(col="red"))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top