Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: How do create a multi-line time series - alternative methods

From   "Allan Reese (Cefas)" <>
To   <>
Subject   st: How do create a multi-line time series - alternative methods
Date   Wed, 21 Sep 2011 17:36:16 +0100

> On 20 Sep 2011, at 20:04, Haillie Lee <> wrote:
> ... can't seem to figure out how to fit multiple series for
> different countries in one graph +label different series by country name.

Nick Cox suggested xtline, which is one good solution.

However, it's quite easy with the basic twoway scatter command, using the connect( L ) option.  
Here's an example using the xt system example data.

sysuse xtline1, clear      // you can copy this into the do file editor
sort person day            // these values already in order, but for general use ...
scatter calories day, c(L) ms(i)  // makes a plot similar to xtline, but all lines same colour

separate cal, by(person) g(cll)   // so split into variable per person
scatter cll1 cll2 cll3 day, c(L L L) ms(i i i) // and you can choose any subset of people ...
scatter cll? day, c(L L L) ms(i i i) // ... or the less-typing approach

With fewer data points you might want to mark each line with symbols.
It's also nice to put the line label at the end of the line, rather than in a legend.
One method is to position text boxes by hand, but an automated approach picks the last plotted value for each line.

gen namelab=person    // person is 1,2,3 with attached labels
replace namelab=0 if person==person[_n+1] & calories<.
label values namelab person
label define person 0 " ", modify  // may be preferred just to create a string variable 

scatter calories day, c(L) ms(i) mlab(namelab) xscale( range(15341 15750) )
I had to extend the X range to keep the label within the plot area. 

When subsets of the data cover different X ranges, you have to sort the subsets so the largest X in one group isn't smaller than the smallest X in the next, or c(L) will join them.  


*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index