1
votes

I want to draw a time series plot specifying reference lines for both X & Y axes. I could get the plot showing reference lines for the time (X) axis only (as shown in the following graph).

The command I used is twoway (tsline egg_prod), tline(2004 2007 2012)

Now I want to show the mean lines for each segment. i.e. the average egg production within 2004-2007 & 2008-2012.

I'm posting a minimal dataset for your reference. Following is the code I used with dataex.

clear  
input int year long egg_production  
2000 918000  
2001 941000  
2002 886000  
2003 885012  
2004 874596  
2005 864552  
2006 901176  
2007 915600  
2008 1.0e+06   
2009 1.1e+06  
2010 1.1e+06  
2011 1.2e+06  
2012 1.2e+06  
2013 1.9e+06  
end

Can someone suggest the way I should follow?

enter image description here

EDIT:

I now want to shade the area corresponding to each identified time period.

I tried the recast(area) option but encountered some problems.

1) I want the shaded area to touch the top & bottom margins of the plot. I couldn’t find a way for that.

2) I don’t want to see a legend for shaded areas. So I used legend(off) but that means that the legend related to the mean values is omitted too. Could you please suggest a way to figure out these issues?

graph twoway scatteri 2 2004 2 2007, recast(area) fcolor(gs14) lcolor(maroon) legend(off) /// 
|| scatteri 2 2008 2 2012, recast(area) fcolor(gs14) lcolor(maroon) legend(off) /// 
|| connected egg year, tline(2004 2007 2008 2012) /// 
|| scatteri `mean1' 2004 `mean1' 2007, recast(line) /// 
|| scatteri `mean2' 2008 `mean2' 2012, recast(line) /// 
ytitle(Egg production (millions)) xtitle("") xla(2000(5)2010 2013) xtic(2001/2012) /// 
scheme(s2color) yla(, ang(h)) /// 
legend(order(2 "2004-07 mean `text1' m" 3 "2008-12 mean `text2' m") pos(11) ring(0) col(1))
1

1 Answers

4
votes

The main trick used here is add each line segment by plotting two pairs of points and then joining them using twoway scatteri and recast(line). You naturally need to calculate the means first.

clear 
input int year long egg_production 
2000 918000 
2001 941000 
2002 886000 
2003 885012 
2004 874596 
2005 864552 
2006 901176 
2007 915600 
2008 1.0e+06 
2009 1.1e+06 
2010 1.1e+06 
2011 1.2e+06 
2012 1.2e+06 
2013 1.9e+06 
end

replace egg_production = egg_p/1e6 

su egg if inrange(year, 2004, 2007), meanonly 
local mean1 = r(mean) 
local text1 : di %3.2f `mean1' 
su egg if inrange(year, 2008, 2012), meanonly 
local mean2 = r(mean) 
local text2 : di %3.2f `mean2'

twoway connected egg year ///
|| scatteri `mean1' 2004 `mean1' 2007, recast(line) /// 
|| scatteri `mean2' 2008 `mean2' 2012, recast(line) /// 
ytitle(Egg production (millions)) xtitle("") xla(2000(5)2010 2013) xtic(2001/2012) ///
scheme(s1color) yla(, ang(h)) ///
legend(order(2 "2004-07 mean `text1' m" 3 "2008-12 mean `text2' m") pos(11) ring(0) col(1)) 

enter image description here

Small points:

  1. The default Stata scheme s2color with its blue background is awkward unless you use it for all your graphics. Of various alternatives, using a different scheme is simplest.

  2. I've worked on the dopey units of measurement (who prefers to see numbers like 1.0e+06 on graphs?) and the axis titles and labels. (Who needs the explanation "Year" when it's labelled 2000 to 2013?)

  3. Using a legend to explain the means is far from the only or even the best option. You might well prefer adding text with text().

  4. Your periods 2004-2007 and 2007-2012 overlap and I am presuming you didn't mean what you said. If you did, changing the code is easy enough.

  5. As the data are annual totals, a bar chart might also appeal, at the cost of being obliged to start from zero (which you might prefer on other grounds).

  6. If you have the horizontal line segments, the vertical lines seem redundant, but you know how to put them back in.

EDIT: Response to new questions.

Use plotregion(margin(zero)) to insist that the shaded area extends through the entire plotregion.

The legend() should be on but you just select which elements you want shown, which are now #4 and #5. You may now want to move the legend.

graph twoway scatteri 2 2004 2 2007, recast(area) fcolor(gs14) lcolor(maroon) /// 
|| scatteri 2 2008 2 2012, recast(area) fcolor(gs14) lcolor(maroon) /// 
|| connected egg year, tline(2004 2007 2008 2012) /// 
|| scatteri `mean1' 2004 `mean1' 2007, recast(line) /// 
|| scatteri `mean2' 2008 `mean2' 2012, recast(line) /// 
ytitle(Egg production (millions)) xtitle("") xla(2000(5)2010 2013) xtic(2001/2012) /// 
scheme(s2color) yla(, ang(h)) plotregion(margin(zero)) /// 
legend(order(4 "2004-07 mean `text1' m" 5 "2008-12 mean `text2' m") pos(11) ring(0) col(1))