Producing Multiple Graphs at Once

Updated: Aug 27, 2020

You can use the forvalues{} or foreach{} loops in Stata to create a set of graphs all at once. If you are generating and saving a large number of graphs in this way, I suggest turning graphics off while it is running. You can do this with the set graphics off command. This will stop Stata from printing each graph to the screen, which vastly speeds up processing time. Don’t forget to turn graphics back on at the end with set graphics on.

Worked Example 1 – Using the foreach loop to make many graphs

In this example I use the Stata example dataset auto.dta. I am going to use the foreach{} loop to create a scatter graph of the variables weight, length and displcement, against the price variable. In the command pane:

This produces the following three graphs, saved as price_weight.png, price_length.png and price_displacement.png:

Worked Example 2 – Using the forvalues loop for unknown variables

There are times when you have a list of variables that are unknown. This situation may arise due to the use of another command which generates a list of variables based on a set of criteria. You want to loop through this list of variables using the first variable as the base variable for the loop. This is difficult to achieve with the foreach{} method shown in Example 1.

In this example we use the forvalues{} loop along with some Stata macros to set the loop up. The advantage of this method is that it does not require you to know anything about the variables you are looping through. It can be applied to a completely unknown set of variables and the loop will still work. For this example I make use of the ds command to get a complete list of all variables in my dataset that are not strings. I then loop through this varlist, taking the first variable as the y-axis variable for all my scatter graphs. In the command pane:

This generates the following set of graphs, combined in the above code using graph combine:

There are a few things to note with this code. Firstly, I took the varlist generated by the ds command and saved it in my own local macro myvarlist. This is necessary because when the scatter command is first run in my forvalues{} loop, it replaces the previous varlist in r(varlist) with the one run for the scatter graph. My new local macro will not be replaced in this way.

The local string macro names is used to collect the name of each graph, so that the macro can be used with graph combine. Note, when this macro is added to within the forvalues{} loop, there is a space at the end of the string. This prevents all the graph names getting stuck together in one long string, which graph combine would not recognise.

The forvalues{} loop starts at 2 rather than 1. This is because the numbers being looped through are used as the variable positions in the local macro myvarlist. The first variable in this list is our y-variable, and so if we started from 1 we would get scatter price price. Stata will create this scatter successfully if you run it, but here we only wanted to compare each variable’s relationship with price. Starting at 2 excludes this single-variable scatter from being generated.

Finally, I used the ds command to get a list of all the numeric variables quickly and easily to facilitate this example. I wanted to exclude string variables from my loop because when a string variable is run through the scatter command it creates an error and will break the loop. However, string variables are not always so easily removed from a varlist, and you may not even know there are any string variables there. In these scenarios, the easiest way to deal with an error such as this is to add an if{} loop within your forvalues{} loop. Below I have changed our previous example slightly to illustrate:

In this code I use command order price to make sure the first variable in r(varlist) will still be price. I then remove the option not(type string) from the ds command so that the string variable make will be included in the varlist. Then, within the forvalues{} loop, I use the confirm command to check the type of variable in local macro xvar. If the variable is numeric no error is generated (error code 0). However, if the variable is not numeric the confirm fails and an error code of 7 is given. The command capture is added to the start of the confirm command so that if an error arises due to a string variable, the error code will be saved in _rc but the loop will continue without stopping. Finally, the if{} loop ensures that the scatter command will only be run if the error code, saved in _rc, is equal to 0. This means only numeric variables, which did not generate an error, will be run through the scatter command.

For a comprehensive exploration of the many Stata graph commands and options, check out the book A Visual Guide to Stata Graphics, 3rd Edition.

You can also have a look at the book An Introduction to Stata Programming, 2nd Edition to learn more about using programming loops in Stata.

0 views0 comments