Create graph legend inside loop in Stata -


i generate graphs different datasets, same variables, in years change. far can generate graphs, without legend, doing this:

levelsof year, local(lyear) foreach l of local lyear {     local fig `fig' scatter mpg time if year==`l', connect(l) ||     } graph twoway `fig'  

i want add legend showing each year. example, doing without loop, add option:

graph twoway `fig',  legend(lab(1 "2010") lab(2 "2011") lab(3 "2012")) 

is there way have years appear in legend part of loop? years, , number of years, change dataset dataset , helpful.

my short answer not draw such graph loop @ all.

here 1 way it:

separate mpg, by(year) veryshortlabel  twoway connected `r(varlist)' time, sort  

here way it:

ssc inst sepscatter  sepscatter mpg time, sep(year) recast(connected) sort 

you need install sepscatter once.

detail:

legend(lab(1 "2010") lab(2 "2011") lab(3 "2012")) 

is easier

legend(order(1 "2010" 2 "2011" 3 "2012")) 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -