Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

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

st: RE: How to label days of week and hours of day on x axis

From   Nick Cox <>
To   "''" <>
Subject   st: RE: How to label days of week and hours of day on x axis
Date   Wed, 2 Nov 2011 20:38:20 +0000

On your first question: I think you are misunderstanding what -week()- does. There is a discussion in 

SJ-10-4 dm0052  . . . . . . . . . . . . . . . . Stata tip 68: Week assumptions
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q4/10   SJ 10(4):682--685                                (no commands)
        tip on Stata's solution for weeks and on how to set up
        your own alternatives given different definitions of the

In essence, week 1 in any year starts on 1 January, and so forth. So the weeks start on whatever day of the week 1 January is. -week()- is thus quite independent of -dow()-'s idea of when the week starts. 

What the Tip discusses is summarised above. 

On your other questions, the trick is simply that you must spell out all the labels  and ticks that you want if the default does not provide them, so the answers lie in the help for -axis label options-. 


G. Dai

I have some difficulties to draw a graph with hour of days within a
week as a time series x axis.

To be more specific, please take the following example
***************************Example Begins************************
*****generate the date set as an example*****
set obs 744
gen year=2011
gen month=7
gen date=_n
replace date=mod(date,31)+1
gen hour=_n
replace hour=mod(hour,24)
gen price=100*runiform()
gen quant=5+2*abs(rnormal(1,5))

*from year, month and date to generate week and day of week.
gen week=week(mdy(month, date, year))
gen dow=dow(mdy(month, date, year))
gen myday=dhms(mdy(month, date, year), hour,0,0)
format myday %tc

***draw a graph showing price and quant on each day and each hour of
the given week
tsset myday,c
twoway (tsline price if year==2011&week==27, yaxis(1) )(tsline quant
if year==2011&week==27,yaxis(2)) ///
, tlabel(,format(%tcDay) grid ) xmlabel(,format(%tcHH))
ttitle("July,2011")ttitle("Day of the Week") ///
 ytitle("Market Price") ytitle("Quantity Bought",axis(2))

*********************Example Ends***************

The first problem with the graph is that the week in the graph starts
with  Saturday, not the one we expected as Sunday, which is the first
of a week. I know this is related to the way Stata defines week. But
how can I make it consistent with the "tradition" way?

Second, on the graph, only Sat, Mon, Wed and Sat are labeled, but I
definitely want to see all days of the week from Sat to Mon. How?

Third,  even though xmlabel(,format(%tcHH))  is used in the the
command, we don't see the minor labels showing the hours within each
day. How
to make it work? Of course, I'm looking for labeling all hours, but
hours like 0,6,12, 18 would be great.

*   For searches and help try:

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index