[R] Making use of names of viewports (grid)
Paul Murrell
p.murrell at auckland.ac.nz
Sun Aug 17 22:29:05 CEST 2008
Hi
Patrick Connolly wrote:
> The following code, though not brilliant, works on an A4 page. It
> might look odd on other devices of a very different size.
>
> =============X8------- cut here ----------------------------
> require(grid)
> wide <- 15
> vps <- grid.layout(nrow = 3, ncol = 4,
> widths = unit(rep(1, 4), rep("null", 4)),
> heights = unit(c(99, 1, 99),
> c("mm", "null", "mm")))
> pushViewport(viewport(layout = vps))
> for(k in 1:4){# label 4 viewports in top row
> cube.k <- ppaste("Cube", k)
> pushViewport(viewport(layout = vps, name = cube.k,
> layout.pos.row = 1,
> layout.pos.col = k))
> grid.rect(gp = gpar(lty = "dashed", lwd = .1))
> grid.text(cube.k, y = .9, gp = gpar(cex = .8))
> popViewport()
> }
> ## label first viewport in bottom row
> pushViewport(viewport(layout = vps, name = "Cube5",
> layout.pos.row = 3,
> layout.pos.col = 1))
> grid.rect(gp = gpar(lty = "dashed", lwd = .1))
> grid.text("Cube5", y = .95, gp = gpar(cex = .8))
> ## Set up a grid inside Cube5 viewport
> vpc <- grid.layout(nrow = 5, ncol = 4,
> widths = unit(rep(wide, 4), "mm"),
> heights = unit(rep(wide, 5), "mm"))
> pushViewport(viewport(layout = vpc))
> for(i in 1:5){
> for(j in 1:4){
> pushViewport(viewport(layout = vpc, clip = "on",
> layout.pos.row = i,
> layout.pos.col = j))
> grid.rect(gp = gpar(col = "red"))
> popViewport()
> }
> }
> ## wipe out the outer edges
> grid.rect(width = wide * 4, height = wide * 5, default.units = "mm",
> gp = gpar(col = "white"))
> ## cover lines from centre squares
> grid.rect(width = wide * 2, height = wide * 3, default.units = "mm",
> gp = gpar(col = "white", fill = "white"))
>
> =============X8------- cut here ----------------------------
>
> I've tried naming the viewports at the time they're pushed. I had
> hoped I could get back to them to do what I've done in 'Cube5' using
> something like seekViewport(), but I see that won't work if the
> viewport has been popped.
>
> Reading through 'R Graphics' (the blue-toned book), I see there's such
> a thing as vpList which I could make while I'm in that for loop,
> but I don't see how I could do something like vpL <- c(vpL, cube.k)
> sort of thing that I could do with a vector. It's even less clear how
> I could make use of vpStack or vpTree.
>
> What is a smarter way to get back to those upper viewports?
Don't pop them ? (use upViewport() instead)
Paul
--
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/
More information about the R-help
mailing list