## Line charts for time series

There are two animations to demonstrate here, and these are the simplest ones to start with because they are intended for datasets where we have lots of time points, such as weather records or stock market prices. We could make every frame in our video a time point, but with 25 frames per second, we might even want to throw some data away. For the time being, for simplicity, let's just stick to one frame per time point.

I have made some artifical data (random "drunkard's walk with reflection") which you can download here, comprising 100 time points. There are four variables: time, a measured value, an upper and lower limit. We are going to draw the values as a line chart and have a shaded region around it from the lower to the upper limits. I assume you know how to do that for a standard graph - now we are going to animate it so that the data are drawn in from left to right as if time was passing and the data were accumulating.

Click here to download the Stata do-file

Click here to download the R code

The steps our program will go through are as follows:

- save a common set of graph options to control the scale and labelling of axes; in Stata these are best put in a -global- macro, in R they would be a character vector; this will prevent your graphs from changing shape and size as the data move about, which will be annoying at best and misleading at worst
- start the
**loop**to run as many times as you want frames (100 in this case); set frame number to 0 - draw the line and shaded region for those data where time is less than or equal to the frame number
- save this graph as an image format with the frame number at the end of the file name
- close the graph if frame number is greater than 0
- return to the start of the
**loop** - when finished, call FFMPEG and ask it to make a video

You might have noticed that the first graph is empty, showing no data at all, by setting the first frame number to zero. This is just an aesthetic choice. More importantly, in Stata it is a good idea to leave the first graph open in the Graph Window, but after that to get rid of the clutter using -graph drop-. This means you don't waste time opening and closing the window, and you don't get bloated with 100 graphs open at the same time!

## A longer time series that scrolls with the data

What if you have more data than would clearly fit onto one graph? We can make an animated graph that will scroll with the data. This is the first glimpse of something beyond our normal way of thinking about graphs - the axes themselves become dynamic and can move just as easily as the data.

The longer dataset with 1000 points can be downloaded here.

There are some added challenges to this animation. We need to change the axis range and labelling as the data accumulate, and we need to do so in a way that looks smooth on the screen. Above all, we cannot allow the x- or y-axis range to increase or decrease as this wil distort the graph. The x-axis is 100 time points wide so for the first 100, this will be identical to the filling line above, and after that point we will draw from time=(100+i) to time=(199+i). To allow for this change at time=100, we could have two graph commands inside the loop and a conditional -if- to choose between them, but I have included a more mathematical approach in the do-file (particularly in defining the rightmost time point to display) to show how you can also reduce it to a single command that changes at time=100. You should use whatever approach you are most comfortable with, and don't be tempted to write parsimonious code if it is hard to follow what it is doing. Here I shall quote R core developer Uwe Ligges, not, I suspect, for the last time:

RAM is cheap, and thinking hurts