grid.newpage()
grid.segments(.01, .5, .99, .5, gp=gpar(col="grey"))
metrics <- calcStringMetric(letters)
grid.rect(x=1:26/27,
width=unit(metrics$width, "inches"),
height=unit(metrics$ascent, "inches"),
just="bottom",
gp=gpar(col="red"))
grid.rect(x=1:26/27,
width=unit(metrics$width, "inches"),
height=unit(metrics$descent, "inches"),
just="top",
gp=gpar(col="red"))
grid.text(letters, x=1:26/27, just="bottom")
test <- function(x) {
grid.text(x, just="bottom")
metric <- calcStringMetric(x)
if (is.character(x)) {
grid.rect(width=unit(metric$width, "inches"),
height=unit(metric$ascent, "inches"),
just="bottom",
gp=gpar(col=rgb(1,0,0,.5)))
grid.rect(width=unit(metric$width, "inches"),
height=unit(metric$descent, "inches"),
just="top",
gp=gpar(col=rgb(1,0,0,.5)))
} else {
grid.rect(width=unit(metric$width, "inches"),
y=unit(.5, "npc") + unit(metric[2], "inches"),
height=unit(metric$ascent, "inches"),
just="bottom",
gp=gpar(col=rgb(1,0,0,.5)))
grid.rect(width=unit(metric$width, "inches"),
height=unit(metric$descent, "inches"),
just="bottom",
gp=gpar(col=rgb(1,0,0,.5)))
}
}
tests <- list("t",
"test",
"testy",
"test\ntwo",
expression(x),
expression(y),
expression(x + y),
expression(a + b),
expression(atop(x + y, 2)))
grid.newpage()
nrowcol <- n2mfrow(length(tests))
pushViewport(viewport(layout=grid.layout(nrowcol[1], nrowcol[2]),
gp=gpar(cex=5, lwd=.5)))
for (i in 1:length(tests)) {
col <- (i - 1) %% nrowcol[2] + 1
row <- (i - 1) %/% nrowcol[2] + 1
pushViewport(viewport(layout.pos.row=row, layout.pos.col=col))
test(tests[[i]])
popViewport()
}
Run the code above in your browser using DataLab