1

I am trying to arrange base and grid plots. Looking at this post I thought I had it but it appears that calling the same code several times in a row is breaking it.

Here is a reproducible example

library(grid)                                                                                       
library(gridBase)                                                                                   
library(gridExtra)                                                                                  

pdf('test.pdf')                                                                                     
for(i in 1:11){                                                                                     
    layout(matrix(c(1,3, 2,3, 4,3), nrow = 3, ncol = 2, byrow = TRUE))                              
    # First base plot                                                                               
    plot(1:10)                                                                                      
    # second base plot                                                                              
    plot.new()                                                                                      
    # Grid regions of current base plot (ie from frame)                                             
    vps <- baseViewports()                                                                          
    pushViewport(vps$inner, vps$figure, vps$plot)                                                   
    # Table grob                                                                                    
    grob <-  tableGrob(iris[1:2,1:2])                                                               
    grid.draw(grob)                                                                                 
    popViewport(3)                                                                                  
    # third base plot                                                                               
    plot(1:10)                                                                                      
    # fourth                                                                                        
    plot.new()                                                                                      
    vps <- baseViewports()                                                                          
    pushViewport(vps$inner, vps$figure, vps$plot)                                                   
    grid.draw(grob)                                                                                 
}                                                                                                   
graphics.off()   

I's expect the plots to be composed of 11 equivalent pages but the grid are all over the place:

page1 is ok: enter image description here page2 and >2 are not: enter image description here page3: enter image description here

What can I do to get a proper 10 pages document. I happen to call a ploting routine several times with slightly different data so I expect to get something consistent across calls

Community
  • 1
  • 1
statquant
  • 13,672
  • 21
  • 91
  • 162
  • 1
    You need to add `popViewport(3)` after the final `grid.draw(grob)` in the loop to get to the root viewport again after drawing the second table (you could also change the `3` to a`0` to get to the root) – user20650 Jan 28 '15 at 06:37
  • why don't you put that as an answer, I worked perfectly – statquant Jan 28 '15 at 20:02
  • Hi, please feel free to write it as answer, and ill update my previous answer at the link – user20650 Jan 29 '15 at 05:12

0 Answers0