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

st: RE: looping over a series of line plots


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: looping over a series of line plots
Date   Mon, 1 Mar 2004 13:23:39 -0000

Something like this may help. In this 
-labnoeq- is in -labutil- on SSC. 

foreach v of var <whatever> {
	preserve 
	collapse `v', by(year occ)
	separate `v', by(occ) short 
	local vars "`r(varlist)'" 
	labnoeq `vars' 
	local vlabel "`:variable label `v''"

      twoway line `vars' year, ///
		xtitle("Year") ytitle(`vlabel') /// 
		title("`vlabel'-type of occupation") 
	restore 
} 


Nick 
n.j.cox@durham.ac.uk 

Sascha O. Becker
> 
> I have micro data on individuals and the following variables:
> different wage and income measures, working status (3 categories) and
> year I and would like to plot trends in all of those wage and income
> measures across occupations and produce simple line plots.
> Since I want to produce a whole series of similar plots, I was
> looking for a smart way to save on code but I got stuck.
> Here is what I do to produce a single graph (my question follows below
> the code):
> 
> ******* BEGIN OF CODE
> 
> collapse wage, by(year occ);
> 
> g wageocc1=wage if occ==1;
> g wageocc2=wage if occ==2;
> g wageocc3=wage if occ==3;
> 
> collapse (sum) wageocc*, by(year);
> 
> list;
> 
> twoway line wageocc* year,
>         xtitle("Year")
>         ytitle("Hourly Wage")
>         title("Hourly Wage by type of occupation")
>         legend(label (1 "Employees") (2 "Self-Employed") (3 "Not
> employed");
> 
> ******* END OF CODE
> 
> 
> My question is how I might automatize a whole series of graphs.
> Here is what I thought I could do:
> 
> 
> +++++++++++
> 
> foreach var in varlist {
> 
>     collapse `varname', by(year occ);
>     ...
>     ...
> 
>     twoway line `varname'occ* year,
>         xtitle("Year")
> 
>         ytitle(-variable label-)  <<------- Here I would like to have
> the variable label of  that variable which I currently loop about
> 
>         title(-variable label-"type of occupation") <<------- Here I
> would like to have the variable label
>                           of that variable which I currently 
> looped about
> 
>         legend(label (1 "Employees") (2 "Self-Employed") (3 "Not
> employed"); <<------- Here I would like to have the
>                                                               
>     VALUE
> labels of the working
>                           status/occupation variable, so
> something like -legend (label occlabel)-
> 
>      }
> 
> +++++++++++
> 
> 
> However, it seems that I do not get this to work the way I want: e.g.
> the -legend- option only allows to change
> labels one by one, so it does not seem to be possible to attach
> pre-defined labels.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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