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

st: looping over a series of line plots

From   "Sascha O. Becker" <>
Subject   st: looping over a series of line plots
Date   Mon, 01 Mar 2004 11:02:15 +0100

Dear statalisters,

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):


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);


twoway line wageocc* year,
       ytitle("Hourly Wage")
       title("Hourly Wage by type of occupation")
       legend(label (1 "Employees") (2 "Self-Employed") (3 "Not

******* 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,

       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
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.

Any suggestions if there is a work-around?

Best regards, Sascha Becker


Dr. Sascha O. Becker
Center for Economic Studies
University of Munich
Schackstr. 4 - 80539 Munich, Germany
Tel +49 (89) 2180-6252
Fax +49 (89) 39 73 03

*   For searches and help try:

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