Previous: Adding Points, Lines, and Up: Graphing Commands Next: Examples


Saving Graphs to Files

By default, R displays graphs in a window on your screen. To save R plots to file (to include them in a paper, for example), preface your plotting commands with:

> ps.options(family = c("Times"), pointsize = 12)
> postscript(file = "mygraph.eps", horizontal = FALSE, paper = "special",
             width = 6.25, height = 4)
where the ps.options() command sets the font type and size in the output file, and the postscript command allows you to specify the name of the file as well as several additional options. Using paper = special allows you to specify the width and height of the encapsulated postscript region in inches (6.25 inches long and 4 inches high, in this case), and the statement horizontal = FALSE suppresses R's default landscape orientation. Alternatively, you may use pdf() instead of postscript(). If you wish to select postscript options for .pdf output, you may do so using options in pdf(). For example:
> pdf(file = "mygraph.pdf", width = 6.25, height = 4, family = "Times", 
+     pointsize = 12)

At the end of every plot, you should close your output device. The command dev.off() stops writing and saves the .eps or .pdf file to your working directory. If you forget to close the file, you will write all subsequent plots to the same file, overwriting previous plots. You may also use dev.off() to close on-screen plot windows.

To write multiple plots to the same file, you can use the following options:



Gary King 2011-11-29