Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

forcing R PDF to new page after variable number of plots

I can have an even or odd number of plots on each page, depending on the input data size/shape.

I set par(mfrow) to values such that it is the smallest grid possible to hold all of the plots, but now I want to make sure it moves to the next page on the next loop iteration in the case where the total number of plots ends up odd or does not fit into a grid.

How can I force a new page? Google has shown me grid.newpage(), but I get the error

> grid.newpage()
Error: could not find function "grid.newpage"

When I try to use it. Also, unless I am mistaken, base plotting does not use grid anyways so that wouldn't help me.

like image 728
Ian Fiddes Avatar asked Sep 21 '14 18:09

Ian Fiddes


1 Answers

Here's a suggested strategy: fill the empty cells with plot.new() until the layout is filled; next plot will automatically push a new page.

n = 5
rc = n2mfrow(n)
par(mfrow=rc, mar=c(0,0,0,0)) 
for(ii in seq_len(n))
    plot(rnorm(10))
for(ii in seq_len(prod(rc) - n))
    plot.new()

plot(1,1) # on its own new page

Alternatively, you could try

while(!par('page')) plot.new()
like image 147
baptiste Avatar answered Nov 20 '22 03:11

baptiste