Statalist


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

Re: st: changing the legend in twoway function


From   Maarten buis <[email protected]>
To   [email protected]
Subject   Re: st: changing the legend in twoway function
Date   Mon, 10 Aug 2009 13:25:58 +0000 (GMT)

--- On Mon, 10/8/09, Miranda Kim wrote:
> Can anyone help me with this?
> I am trying to write a program that plots the functions a +
> bx + cx2 (on the same graph), where a, b and c are stored
> variables. I want to label the legend with the set of values
> a, b and c for each graph.
> For example if I have:
> a   b   c
> 1   3   4
> 2   5   8
> then I want to have "a=1, b=3, c=4" and "a=2, b=5, c=8"
> assigned to the relevant graph in the legend. How can I do
> this? Also , how complicated would it be to control the
> colours or line types of the graphs?

Below is how I would do that.
Hope this helps,
Maarten

clear*

inp byte(a   b   c)
1   3   4
2   5   8
end

program define polysameplot, sortpreserve
   syntax varlist(min=3 max=3 numeric) [if] [in], ///
   [                                              ///
   range(numlist min=2 max=2 ascending)           ///
   LPattern(string)                               ///
   LWidth(string)                                 ///
   LColor(string)                                 ///
   *                                              ///  
   ]
   

   // get a, b, and c
   tokenize `varlist'
   args a b c

   // parse lpattern() option
   local kp : word count `lpattern'
   tokenize `lpattern'
   forvalues i = 1/`kp' {
      local opt`i' "lpattern(``i'')"
   }

   // parse lwidth() option
   local kw : word count `lwidth'
   tokenize `lwidth'
   forvalues i = 1/`kw' {
      local opt`i' "`opt`i'' lwidth(``i'')"
   }

   // parse lcolor() option
   local kc : word count `lcolor'
   tokenize `lcolor'
   forvalues i = 1/`kc' {
      local opt`i' "`opt`i'' lcolor(``i'')"
   }

   // finding which obs to loop over
   marksample touse
   tempvar sort
   qui count if `touse'
   local k = r(N)

   gen byte `sort' = !`touse'
   sort `sort'

   // parse range() option
   if "`range'" == "" {
      local range "-5 5"
   }

   // create the graph command
   local j = 1
   forvalues i = 1/`k' {
       local gr "`gr' function y=`a'[`i']+`b'[`i']*x+`c'[`i']*x^2, range(`range') `opt`i'' ||"
       local leg `"`leg' `j' "`a'=`=`a'[`i']', `b'=`=`b'[`i']', `c'=`=`c'[`i']'" "'
       local j = `j' + 1
   }
   local leg `"legend(order(`leg'))"'

   // graph (the local options collects all the other options 
   // as captured by "*" in -syntax-)
   twoway `gr', `leg' `options'

end

polysameplot a b c in 1/2, lpattern(dash dot)  ///
                           lcolor(red blue)    ///
                           lwidth(vthin thick) ///
                           range(-4 3)         ///
                           xline(0)            ///
                           ytitle("bla")



-----------------------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany

http://home.fsw.vu.nl/m.buis/
-----------------------------------------





      

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