Using Stata Graphs in Documents

Stata can easily produce "publication quality" graphics, but it takes a few more steps to actually put them in a publication. This article will discuss how to save Stata graphs in various formats. It will also show you step-by-step how to insert a Stata graph into a Word document or PDF file.

Saving a Graph

When you create a graph in Stata, the result is simply a picture on the screen. If you want to be able to view it after your current program finishes, you'll need to save it.

graph save

The graph save command tells Stata to save your current graph (the most recently created) in Stata's gph format. The syntax is simply

graph save filename, replace

where filename should be replaced by the name you want Stata to give your file. Stata will add .gph to the end of the filename automatically. The replace option tells Stata to overwrite the current version of that graph if one exists--without that your do file will fail if you try to run it more than once.

The disadvantage of graph save is that the .gph files it produces can only be read by Stata. To create files usable by other programs you'll need graph export. However, graph save can be useful if you want to move graphs between computers--for example from a Linux server to a PC.

graph use

The graph use command opens a .gph file previously saved on disk. The graph will be displayed, and it also becomes the current graph that subsequent commands will act on. The syntax is

graph use filename

The filename will be assumed to end in .gph if you don't include an extension.

graph export

The graph export command saves your current graph in the format you choose. The syntax is

graph export filename.extension, replace

Again, filename should be replaced by the name you want. The extension you choose will determine the file format. What formats are available depends on how you're running Stata:

 

Extension File Format Where Available
Windows Linux (Interactive) Linux (Batch Mode)
.ps PostScript Yes Yes Yes
.eps Encapsulated Postscript Yes Yes Yes
.tif Tagged Image Format Yes Yes No
.png Portable Network Graphic Yes Yes No
.wmf Windows Metafile Yes No No
.emf Windows Enhanced Metafile Yes No No
.pdf Portable Document Format Yes No No

Stata's commands for creating graphs tend to take a lot of computing power, so making a lot of graphs can take a long time. Keep in mind that the fastest way to run a Stata job at the SSCC is to submit it to Condor so it will be run using Stata/MP. But Condor jobs must be run in batch mode, and batch mode has the fewest file format options. This is where graph save comes in: create your graphs using Condor, and save them using graph save. Then you can open the resulting .gph files in Windows Stata or interactive Linux Stata with graph use, and finally use graph export to convert them to the format you need.

See An Introduction to Condor for details about submitting jobs to Condor.

Inserting a Graph into a Word Document

We've found that for best results in Word you generally want to use emf (Enhanced Metafile) format. So first create your graph, then save it in emf format using graph export:

graph export graph.emf, replace

Next open the document into which you want to insert the graph using Word. Put the cursor at the desired location. Then click Insert, Picture, From File. Locate the directory where you saved the graph, select it, and click Insert.

Inserting the Graph into Word

Inserting a Graph into a PDF file

If you want to put your graph in a PDF file, PostScript works well. Create your graph, then save it in ps format using graph export:

graph export graph.ps, replace

Next open Adobe Acrobat (not Adobe Reader, which only reads PDF files--Adobe Acrobat is available on Winstat). If you already have a PDF file and you want to add the graph to it, open the file, then click Document, Insert Pages. Set Files of Type to PostScript/EPS. Locate the proper directory, click on your graph, and click Select.

Inserting a Graph into a PDF file

If you want the graph to be a PDF file all by itself, click File, Create PDF, From File. Then locate and select your graph.

From Linux you can do the same thing using the ps2pdf program. At the Linux prompt type:

ps2pdf graph.ps graph.pdf

Using Graphs Elsewhere

What format will work in other situations is difficult to predict, and you may need to try several alternatives.

PostScript is a standard format for printing across all different kinds of computers. This makes it a good choice for situations that involve printing, or for use on computers other than PCs.

Enhanced Metafile is an excellent choice if you want to use the graph in a Windows program, as tools for working with emf files are embedded in Windows. However, computers that are not running Windows will probably have no way to open emf files at all.

PNG is a standard picture format, but a relatively recent one. Older programs may not recognize it (try TIF with those programs). However, PNG is a great choice for web pages. It's also a good format for just sending a graph to someone—most computers will open it with some sort of picture viewer or editor.

Last Revised: 6/7/20013