Introduction to Statistics (MAT/SST 115.03 2008S)

Saving Graphs to Files

At times, you will find it useful to save the graphs you create, so that you can print them or copy them to other applications. You begin by creating the graph as you normally would. Then, you use a series of arcane commands to build the file. The most important of these commands is dev.copy, which copies the graph to a file. You specify the type of file (usually jpeg), the file name, and the width and height. When you are done, you follow the dev.copy command with

For example, the following saves the current image to the file graph.jpg on your desktop.

dev.copy(jpeg, filename="~/Desktop/graph.jpg", width=400, height=400)

You can also save to PostScript and a wide variety of other formats, although some of the commands are a bit different. For example, here's are commands to save the graph as PostScript and PDF.

dev.copy(postscript, "~/Desktop/")
dev.copy(pdf, "~/Desktop/graph.pdf")

You may find that you have to play a bit to get the output laid out the way you want.

Once you have a file that you like, you can email it to yourself for including in an electronic document or print it for attaching to a physical document.

Creative Commons License

Samuel A. Rebelsky,

Copyright (c) 2007-8 Samuel A. Rebelsky.

This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.