Statalist


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

st: RE: Linking legend text to the value of a variable


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Linking legend text to the value of a variable
Date   Thu, 1 Oct 2009 19:16:20 +0100

It seems you need to "look up" string values. One way to approach this
is as follows:

Outside your loop: 

gen long obsno = _n 

Inside your loop you find the smallest observation numbers satisfying
various conditions, e.g. 

su obsno if idvar == "`i'" & idvar2 == 1 & idvar3 == 1, meanonly 
local name1 = idvar2_name[`r(min)'] 
su obsno if idvar == "`i'" & idvar2 == 2 & idvar3 == 1, meanonly 
local name2 = idvar2_name[`r(min)']

... legend(order(1 "`name1'" 2 "`name2'"))  

The largest observation numbers will work as well. (By the way, I never
use repeated -label()- calls given scope to call -order()- once.) 

Nick 
[email protected] 

Dorothy Bridges

I have a loop creating twoway graphs along the lines of the following:

levels idvar, local(ID)
foreach i of local ID{
#delimit;
di "`i'";
twoway (line yvar xvar if idvar=="`i'" & idvar2==1 & idvar3==1,
lpattern(dash))
(line yvar xvar if idvar=="`i'" & idvar2==2 & idvar3==1, lpattern(dash))
(line yvar2 xvar2 if idvar=="`i'" & idvar2==1 & idvar3==1, color(navy)
yaxis(2) lpattern(dash))
title("`i')
};

And I would like the legend to be: legend(label(1 =idvar2_name if
idvar=="`i'" & idvar2==1 & idvar3==1) label(2 =idvar2_name if
idvar=="`i'" & idvar2==2 & idvar3==1))

How might I accomplish this?  I found this
(http://www.stata.com/statalist/archive/2004-03/msg00129.html) in the
archive but couldn't quite get from there to where I would like to be.

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



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