| Previous: Adding Points, Lines, and | Up: Graphing Commands | Next: Examples |
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:
> pdf(file = "mygraph.pdf", width = 6.25, height = 4, family = "Times", + pointsize = 12, onefile = TRUE)
par(mfrow = c(2, 4))This creates a grid that has two rows and four columns. Your plot statements will populate the grid going across the first row, then the second row, from left to right.