17.2 With the console
# Open the file that will contain your plot (the name is up to you)
pdf(file="myplot.pdf")
# execute the plot
plot(1:10)
# Close the file that will contain the plot
dev.off()
Formats
R supports a variety of file formats for figures: pdf, png, jpeg, tiff, bmp, svg, ps.
They all come with their own function, for example:
# TIFF
tiff(file="myfile.tiff")
plot(1:10)
dev.off()
# JPEG
jpeg(file="myfile.jpeg")
plot(1:10)
dev.off()
# etc.
The size of the output file can be changed:
# Default: 7 inches (both width and height) for svg, pdf, ps.
svg(file="myfile.svg", width=8, height=12)
plot(1:10)
dev.off()
# Default: 480 pixels (both width and height) for jpeg, tiff, png, bmp.
png(file="myfile.png", width=500, height=600)
plot(1:10)
dev.off()
Note that pdf is the only format that supports saving several pages:
Plot several figures in one page
You can output more than one plot per page using the par() function (sets graphical parameters) and the mfrow argument.