Output to a file
Problem
You want to save your graph(s) to a file.
Solution
There are several commands which will direct output to a file instead of the screen. You must use the dev.off()
command to tell R that you are finished plotting; otherwise your graph will not show up.
PDF is a vector file format. Vector files are generally preferred for print output because the resulting output can be scaled to any size without pixelation. The size of a vector file is usually smaller than the corresponding bitmap file, except in cases where there are many objects. (For example, a scatter plot with thousands of points may result in a very large vector file, but a smaller bitmap file.)
pdf("plots.pdf")
plot(...)
plot(...)
dev.off()
PDF’s are 7x7 inches by default, and each new plot is on a new page. The size can be changed:
# 6x3 inches
pdf("plots.pdf", width=6, height=3)
# 10x6 cm
pdf("plots.pdf", width=10/2.54, height=6/2.54)
If you want to edit your file in a vector editor like Inkscape or Illustrator, some of the plotting point objects might look like letters instead of circles, squares, etc. To avoid this problem:
pdf("plots.pdf", useDingbats=FALSE)
SVG
SVG is another vector format. The default settings for svg()
doesn’t allow for multiple pages in a single file, since most SVG viewers can’t handle multi-page SVG files. See the PNG section below for outputting to multiple files.
svg("plots.svg")
plot(...)
dev.off()
SVG files may work better with vector-editing programs than PDF files.
PNG/TIFF
PNG and TIFF are bitmap (or raster) formats. If they are magnified, the pixels may be visible.
png("plot.png")
# or tiff("plot.tiff")
plot(...)
dev.off()
By default, the graphs are 480x480 pixels in size, at a resolution of 72 dpi (6.66x6.66 inches).
Increasing the resolution will increase the size (in pixels) of the text and graph elements. This occurs because the size of these elements is relative to the physical dimension of the graph (e.g., 4x4 inches), not the pixel dimension of the graph. For example, a 12 point font is 12/72 = 1/6 inch tall; at 72 dpi, this is 12 pixels, but at 120dpi, it is 20 pixels.
This would create a graph that is 480x240 pixels at 120dpi, equivalent to 4x2 inches.
png("plot.png", width=480, height=240, res=120)
plot(...)
dev.off()
If you want to make more than one graph, you must either execute a new png()
command for each one, or put %d
in the filename:
png("plot-%d.png")
plot(...)
dev.off()
This will generate plot-1.png, plot2.png, and so on.
For import into PDF-incapable programs (MS Office)
Some programs which cannot import PDF files may work with high-resolution PNG or TIFF files. For example, Microsoft Office cannot import PDF files. For print publications, you may be required to use 300dpi images.
# Make a 6x6 inch image at 300dpi
ppi <- 300
png("plot.png", width=6*ppi, height=6*ppi, res=ppi)
plot(...)
dev.off()
ggplot2
If you make plots with ggplot2 in a script or function, you must use the print()
command to make the graphs actually get rendered.
# This will do nothing
pdf("plots.pdf")
qplot(...)
dev.off()
# This will do the right thing
pdf("plots.pdf")
print(qplot(...))
dev.off()
To save a ggplot2 graph from the screen to a file, you can use ggsave()
.
ggsave("plot.pdf")
ggsave("plot.pdf", width=4, height=4)
# This will save a 400x400 file at 100 ppi
ggsave("plot.png", width=4, height=4, dpi=100)
Saving a graph from the screen
If you have a graph on the screen, you can save it to a bitmap file.
This will save an exact pixel-for-pixel copy of what’s on screen, but it will probably only work in Linux and on Macs that use X11 for R graphing:
# Make a plot on screen
plot(...)
savePlot("myplot.png")
This will save the current graph from the screen, but it re-renders it for the device, which may have different dimensions, so it won’t look exactly the same unless you specify the exact same size in pixels.
# Make a plot on screen
plot(...)
dev.copy(pdf,"myplot.pdf", width=4, height=4)
dev.off()
# Same as doing:
# pdf("myplot.pdf", width=4, height=4)
# plot(...)
# dev.off()
dev.copy(png,"myplot.png", width=400, height=400)
dev.off()