In this recipe, you will learn how to save and export our graphs to various useful formats.
To save a graph as an image file format such as PNG, we can use the png()
command:
png("scatterplot.png") plot(rnorm(1000)) dev.off()
The preceding command will save the graph as scatterplot.png
in the current working directory. Similarly, if we wish to save the graph as JPEG, BMP, or TIFF, we can use the jpeg()
, bmp()
, or tiff()
commands, respectively.
If you are working in Windows, you can also save a graph using the graphical user interface. First, make your graph, make sure that the graph window is the active window by clicking anywhere inside it, and then navigate to File | Save as | Png or the format of your choice, as shown in the following screenshot:
When prompted to choose a name for your saved file, type in a suitable name and click on Save. As you can see, you can choose from seven different formats.