A pair of alternative approaches, for side-by-side or stacked bars:
clear all
set seed 1
set obs 1000
gen id=_n
gen month=1 + int(12*uniform())
gen year=2008 + int(2*uniform())
gen subject_type=1 + int(3*uniform())
la def lab_subjects 1 "no reply" 2 "screened" 3 "sampled", modify
la val subject_type lab_subjects
g m=(year-1960)*12+month-1
format m %tmm_CY
la var m "Calendar month"
levelsof m, loc(vs)
foreach v of loc vs {
la def m `v' "`:di `:format m' `v''", modify
}
la val m m
ssc inst byhist, replace
byhist m, by(subject_type) disc tw(xla(576/599,angle(90) val))
contract m subject_type, z f(one)
reshape wide one, j(subject_type) i(m)
g noreply=max(0,one1)
g screened=max(0,one1)+max(0,one2)
g sampled=max(0,one1)+max(0,one2)+max(0,one3)
sc sampled screened noreply m, recast(bar) xla(576/599,angle(90)) name(s)
On Thu, Nov 26, 2009 at 6:00 AM, Nick Cox <[email protected]> wrote:
> Note that the alternative approach of assigning a date format to a
> monthly date variable doesn't seem to help enough.
>
> gen mydate = ym(year, month)
> format mydate %tmm_CY
>
> followed by a graph with -by(mydate)- just uses the numbers and not the
> format. Mapping that to a string causes display in alphabetical order
> (all Aprils first) which is unlikely to be helpful.
>
> -catplot- is a user-written command from SSC, but is not causing this
> behaviour. -graph bar, by()- is responsible.
>
> Perhaps there is a work-around.
>
> Nick
> [email protected]
>
> Martin Weiss
>
> *************
> clear*
> set obs 100
> gen id=_n // subject id
> gen month=1 + int(12*uniform())
> gen year=2008 + int(2*uniform())
> gen subject_type=1 + int(3*uniform())
> la def lab_subjects 1 "no reply" 2 "screened" 3 "sampled", modify
> la val subject_type lab_subjects
> list, noo
>
> compress
>
> //from http://www.stata-journal.com/sjpdf.html?articlenum=pr0013
> //follow special sequences
>
> //generate label for months
> tokenize `c(Months)'
>
> forvalues i = 1/12 {
> label def month `i' "``i''" , modify
> }
>
> label val month month
>
> decode month, generate(mon)
> gen yearmonth= mon + " " + string(year)
>
> catplot bar subject_type, by(yearmonth) stack asyvars ///
> bar(1, bcolor(red)) ///
> bar(2, bcolor(yellow)) ///
> bar(3, bcolor(green))
> *************
>
> Michael McCulloch
>
> I'd appreciate any feedback on this question: I'd like to create a
> variable m_y, which I can sort in order of month over several years.
>
> I've simulated data with:
> . clear
> . set obs 100
> . gen id=_n // subject id
> . gen month=1 + int(12*uniform())
> . gen year=2008 + int(2*uniform())
> . gen subject_type=1 + int(3*uniform())
> . la def lab_subjects 1 "no reply" 2 "screened" 3 "sampled", modify
> . la val subject_type lab_subjects
> . list, noo
>
> But, I'd like guidance on how to combine month and year into one
> ordered variable, to appear with labels such as:
> Jan 2008
> Feb 2008
> ...
> Jan 2009
> Feb 2009
>
> That ordered variable would then be displayed in the -by(month)-
> option in this graph:
> . catplot bar subject_type, by(month) stack asyvars ///
> bar(1, bcolor(red)) ///
> bar(2, bcolor(yellow)) ///
> bar(3, bcolor(green))
>
> I could use both -over- and -by-, but that looks awkward:
> . catplot bar subject_type, by(year) over(month) stack asyvars
> ///
> bar(1, bcolor(red)) ///
> bar(2, bcolor(yellow)) ///
> bar(3, bcolor(green))
>
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/