[Rd] incremental plots with pdf and dev.copy

Hin-Tak Leung hin-tak.leung at cimr.cam.ac.uk
Wed Feb 28 16:58:24 CET 2007

This is an r-help question - please post to the relevant list next time.

The problem is with your "dev.off(dev.prev())" lines - you should
have used just "dev.off()" plain, without the dev.prev() inside.
As for why, please read the help pages.

Giampiero Salvi wrote:
> Hi,
> I am trying to produce incremental plots directly using the pdf
> device. I mean that I want to produce a plot, save it in a pdf
> file, then add details and save the new plot in a new file, and
> so on. Before I used to do this with x11, by just adding graphics to
> a plot and then using dev.copy2eps at the right times for each eps
> figure. Now I want to do this in batch mode and the x11 device is not
> available.
> This is how I go about it:
> pdf(file="file1.pdf")          # create the first plot
> dev.control(displaylist='enable') # allow copying print devices
> plot(1:10)                     # first plot
> dev.copy(pdf,file="file2.pdf") # create the second plot
> dev.control(displaylist='enable')
> dev.off(dev.prev())            # close the first plot
> points(2,3)                    # add to the second plot
> dev.copy(pdf,file="file3.pdf") # create the third plot
> dev.off(dev.prev())            # close the second plot
> points(3,2)                    # add to the third plot
> dev.off()
> The problem is this works only for the first two plots. For the
> third I get an error at "dev.copy(pdf,file="file3.pdf")"
> Error in dev.copy(pdf, file = "file3.pdf") :
>         invalid graphics state
> I also tried to remove the second dev.control statement, but in
> that case the same error is delayed to the "points(3,2)" line
> (plotting in the third plot).
> Is this behaviour expected? Is there a better way to do what I'm
> trying to do?
> Thank you!
> Giampiero

More information about the R-devel mailing list