Visualisation - Creating a Moving Stata Graphic
Visualisation is an important part of the communication of results and ideas from all areas of study. It is an especially useful tool when communicating with the general public, as it allows complex ideas to be communicated and understood more easily than mere words can manage. One of the visual tools commonly used are videos, which can come in many forms including short videos with accompanying sound, or GIF images which are very short repeatable videos.
For this tech tip I am going to look at creating a moving Stata graphic from a set of images. To do this, I first set up a loop in Stata to generate a progression of still images. To create my gif I upload the images generated from Stata to this website: https://ezgif.com/maker. This is just one of many free and paid websites and computer programs capable of generating GIFs from still images.
How to Use:
Make sure your current working directory is the one you want to save all your image files to.
Generating a GIF from the Images:
Go to https://ezgif.com/maker
Click “Choose Files” under “Select images:”, navigate to the folder containing the images, select them all and click OK
Click “Upload and make a GIF!” button (this may take some time depending on the number of images you have)
Scroll down past all your images to the options section, select an appropriate delay time
Click “Make a GIF!” button
To save, right click GIF and select “Save image as…” save it as a GIF on your computer
In this example I am going to use the Stata example dataset uslifeexp.dta, which contains data sourced from the CDC website. I am going to plot the life expectancy of white and black people from the year 1900 to 1999. For your information, in the below code “le” stands for “life expectancy”. The dataset uses it to name some variables, and I use it to prefix the name of each image I save. In the command pane, I type the following:
This loop command generates 100 images, each showing life expectancy from year 0 (1900) to year x (the first still shows only 1900, the next 1900 and 1901, the next 1900 to 1902, etc.).
To create my GIF, I now load the website https://ezgif.com/maker and upload all my photos using the shift-click select method. I then click the “Upload and make a GIF!” button and wait for my 100 still images to finish uploading. Once fully uploaded, I scroll past all the images to the options at the bottom and change my frame speed from 20ms to 7ms. I then click the “Make a GIF!” button and the following GIF is generated:
To save the GIF, I simply right-click the image and click “Save image as…” making sure to save it as a GIF file.