Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Showing years on graph axes

From   "Nick Cox" <>
To   <>
Subject   st: Showing years on graph axes
Date   Tue, 16 Mar 2004 12:50:09 -0000

Here is some minor trickery which may be useful 
to those who draw graphs of time series. 

In spirit it is close to
but the problem is different and the solution is for 
Stata 8. 

I was playing with about 10 years' worth of 
environmental data: the variables are for various 
daily dates and  all show strong seasonality. I wanted 
therefore to show both the years "1987 ... 1997" and 
the year ends. That is more labels than might otherwise
be justified aesthetically, but often with this kind of
data you want the detail for look-up, as you 
can bring in other knowledge ("1993 was a very dry year, 
so that peak makes sense", etc.). No doubt much the 
same could be said of many analyses of economic or 
other time series. 

This loop puts all the 1 January dates and the 1 July 
dates into local macros: 

forval y = 1987/1997 { 
	local jan "`jan' `=mdy(1,1,`y')'" 
	local jul "`jul' `=mdy(7,1,`y')'" 

Then the -graph- call is say 

. twoway connected <whatever> <date>, 
	xla(`jul', format(%dCy) noticks) xtic(`jan', grid) 

That is, text labels appear at 1 July (close enough to 
the middle of the year for graphical purposes) formatted 
so that only the years are shown, but with ticks 
suppressed, while ticks and a grid appear at year ends. 

Clearly this could be extended in various ways, e.g. 
to non-calendar years, and it could be automated. 
Just make sure that the graph code can see the local 
macros. (For example, the graph code in a -doedit- 
window cannot see the local macros in your main 
interactive session.) 


*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index