Skip to content
Prev 346033 / 398502 Next

Using grid.layout inside grid.layout with grid package: naming of the viewports affects plotting

Hi

The essence of your problem is that seekViewport() is much more 
ambiguous than downViewport().

Your set up involves creating a hierarchy of viewports four deep, with 
eight viewport paths (vpPaths) in total ...

vp1::A::vp2::plot1
            ::plot2
    ::B::vp3::plot1
            ::plot2
    ::C::vp4::plot1
            ::plot2
    ::D::vp5::plot1
            ::plot2

... and an important feature of that set of vpPaths is that the viewport 
name "plot1" (and "plot2") occurs more than once.

That is not necessarily a problem because you can distinguish between 
the different instances of "plot1" with complete paths, like this ...

downViewport("vp1::B::vp3::plot1")

... or by navigating part way down the tree of viewports first to a 
point where there is only one "plot1" viewport below it ...

downViewport("B")
downViewport("plot1")

However, seekViewport("plot1") does NOT do either of those things.  It 
does ...

upViewport(0) # go right up to the ROOT viewport
downViewport("plot1")

... and that will just find the first "plot1" match, which is the one 
below viewport "A".

The seekViewport() function should not therefore be relied on in 
non-interactive settings.  In fact, if you are writing code for general 
use, you should probably be much more disciplined about how you navigate 
between your viewports and make use of upViewport() and unambiguous 
downViewport() calls so that you know which viewport you will end up in. 
  The code below demonstrates a more disciplined version of your 
viewport set up ...

library(grid)
stackedplot <- function(main=""){
   top.vp <- viewport(
     layout=grid.layout(2, 1))
   p1 <- viewport(layout.pos.col=1, layout.pos.row=1, name="plot1")
   p2 <- viewport(layout.pos.col=1, layout.pos.row=2, name="plot2")
   splot <- vpTree(top.vp, vpList(p1,p2))
   pushViewport(splot)
   upViewport()
   downViewport("plot1")
   grid.rect(width=unit(0.9, "npc"), height=unit(0.9, "npc"))
   grid.text(paste(main, 1))
   upViewport()
   downViewport("plot2")
   grid.rect(width=unit(0.9, "npc"), height=unit(0.9, "npc"))
   grid.text(paste(main, 1))
   upViewport(2)
  }

grid.newpage()
multitop.vp <- viewport(layout=grid.layout(2,2))
pl1 <- viewport(layout.pos.col=1, layout.pos.row=1, name="A")
pl2 <- viewport(layout.pos.col=1, layout.pos.row=2, name="B")
pl3 <- viewport(layout.pos.col=2, layout.pos.row=1, name="C")
pl4 <- viewport(layout.pos.col=2, layout.pos.row=2, name="D")
vpall <- vpTree(multitop.vp, vpList(pl1,pl2,pl3,pl4))
pushViewport(vpall)
upViewport()
downViewport("A")
stackedplot(main="A")
upViewport()
downViewport("B")
stackedplot(main="B")
upViewport()
downViewport("C")
stackedplot(main="C")
upViewport()
downViewport("D")
stackedplot(main="D")
upViewport(2)

Hope that helps

Paul
On 11/27/14 00:16, Helske Satu wrote: