Reproducing Graphs Customised with the Graph Editor in Stata
Updated: May 20
Stata has a wide variety of graph options available to customise your graphs. Usually these options are applied to Stata graph commands run directly through the command pane. If you use the Stata menu boxes to create graphs, these boxes convert the options you pick into a Stata command that is then run by Stata when you click ok/submit. Using Stata commands, either directly or through menu boxes, is the primary way you interact with Stata for your analyses.
However, for customising Stata graphs there is a third option called the graph editor. To access the graph editor, first you make a graph (either using Stata commands or menu boxes), then edit it using the Stata graph editor mode once the graph is generated. This mode can be enabled one of two ways. You can engage the graph editor through the graph menu (File > Start Graph Editor), or by clicking the start graph editor button at the top of the graph. The graph editor button is shown below:
If you want your graphs to be easily reproducible it is best to use either Stata commands or menu boxes to generate your graphs. This is because the Stata graph editor does not automatically track changes you make to the graph. While there is a record option for the graph editor, it does not save changes as regular Stata graph options.
If you need to replicate graph editor changes, for example if someone shares with you a graph edited in this way, you can find the special graph editor commands in one of two ways. This will require you to have either the graph saved in .gph format, or a special graph editor recording saved in a .grec file.
Worked Example 1 – The graph is saved in .gph format
For this example I use the Stata example dataset auto.dta. To make my graph, in the command pane I type:
This produces the following graph:
I then use the graph editor to change the graph so it looks like this:
Now I save the graph in Stata graph format .gph. To find the special graph editor commands used to edit my graph, I need to open the .gph file in Stata’s do-file editor.
The Stata do-file editor can be accessed in one of three ways.
Through the Stata menus. Go to Window > Do-file Editor > New Do-file Editor
Use the keyboard shortcut Ctrl+9
Run command doedit in the command pane
Once you have opened the do-file editor you need to open your .gph file. Make sure to change the files from Do files (*.do, *.ado) to All files (*.*) when you are trying to find the file to open.
Click File > Open > Open at the top of the do-file editor
Use the keyboard shortcut Ctrl+O (while on the do-file editor screen)
Click the "open" symbol in the do-file editor
With your .gph file open, you should see something like this:
If you scroll to the bottom of the file you should see the special graph editor commands that were used to edit this graph:
The graph editor commands are the ones that come after the file name and date. To run these commands in Stata, remove the period . at the beginning, and add gr_edit to the start of each command. For example, if I wanted to recreate the edited graph above, I would use the following set of commands:
Worked Example 2 – You have a graph editor recording in .grec format
To create a graph editor .grec recording, click the recording button at the top of the graph window after you have engaged graph editor mode. You can then edit your graph with the graph editor, and all your edits will be recorded. When you are finished, click the recording button again and you will be prompted to save the recording as a .grec file.
Once the recording is saved you can open it with the Stata do-file editor, in the same way you opened the .gph file in the first example. The .grec file in the do-file editor should look something like this:
This .grec recording contains only the changes that were made while it was recording. Unlike the .gph file from the first example, it does not contain any information about how the original graph was drawn. This means after the comments (in green) at the top, everything in black will be graph editor commands. To recreate the changes, you would make the graph and then apply the graph editor commands, the same as in the first example:
Stata's graph editor is powerful, however it is recommended that you make sure to either record the changes you make in a .grec file, or save your graph as a .gph file. If you do not make a recording and only save the graph as an image file there will be no record of the changes you have made to the graph, making it much more difficult for others to reliably reproduce your graph.