Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: Recursive graph with parallel lists


From   Roberto Ferrer <[email protected]>
To   Stata Help <[email protected]>
Subject   Re: st: Recursive graph with parallel lists
Date   Mon, 8 Jul 2013 16:39:51 +0100

Thank you for answering (and clarifying the recursion/iteration issue).

Following Nick's advice now I have at the beginning of my code:

global years 1975 1985 1995 2005
global linet solid shortdash dash longdash
global marker tiny // Marker size for graphs
global numy : word count $years

allowing me to control more easily graphs within other parts of the code.

The code for the graph is similar to what was suggested, only adding
the syntax for the legend.

forvalues i = 1/$numy {
    local y : word `i' of $years
    local ll : word `i' of $linet
    local call  `call'  || connected college_mean size_b if college ==
0 & yearobs == `y'
    local call  `call' , msize(${marker}) lpattern(`ll') sort
    local leg `leg' label(`i' "`y'") // syntax for legend
}
twoway `call' legend(`leg') ///
title("Proportion with at least college by city size")




>Date: Sun, 7 Jul 2013 18:29:35 +0100
>From: Nick Cox <[email protected]>
>Subject: Re: st: Recursive graph with parallel lists
>
>I see nothing recursive here, just iteration. You want superimposed
>curves for different subsets of the data on one graph. It follows that
>you need to fire up -graph twoway- just once, so outside the loop. The
>purpose of the loop is to build up the syntax.
>
>Try something more like this.
>
>local years 1975 1985 1995 2005
>local linet   solid shortdash dash longdash
>local n : word count `years'
>local call
>
>forvalues i = 1/`n' {
>    local y : word `i' of `years'
>    local ll : word `i' of `linet'
>    local call  `call'  || connected college_mean size_b if college ==
>0 & yearobs == `y'
>    local call  `call' , msize(tiny) lpattern(`ll') sort
>}
>
>twoway `call'
>
>Nick
>[email protected]
>
>
>On 7 July 2013 14:02, Roberto Ferrer <[email protected]> wrote:
>> I want to customize the way in which I draw connected (line) graphs
>> for different years of a particular variable. Now I have this and for
>> only four years it works just as intended:
>>
>> twoway connected college_mean size_b if college == 0 & yearobs == 1975, ///
>> msize(tiny) lpattern(solid) sort ///
>> || connected college_mean size_b if college == 0 & yearobs == 1985, ///
>> msize(tiny) lpattern(shortdash) sort ///
>> || connected college_mean size_b if college == 0 & yearobs == 1995, ///
>> msize(tiny) lpattern(dash) sort ///
>> || connected college_mean size_b if college == 0 & yearobs == 2005, ///
>> msize(tiny) lpattern(longdash) xlabel(0(1)10) sort ///
>> title("Proportion with at least college by city size") ///
>> legend(label(1 "1975") label(2 "1985") label(3 "1995") label(4 "2005"))
>>
>> But I would like to be able to easily add/delete as many years as I'd
>> like (I have aprox. 40 years). I would like something along the lines:
>>
>> y = list(1975 1996 2000)
>> t = list(dash longdash shortdash)
>> l = list(label1 label2 label3) [which could be simply the elements of y]
>> Graph xvariable yvariable using y
>> labels using l
>> options (line types, e.g.) using l
>>
>> At the moment I have this code using parallel lists:
>>
>> local years = "1975 1985 1995 2005"
>> local linet = "solid shortdash dash longdash"
>> local marker = "tiny"
>> local n : word count `years'
>>
>> forvalues i = 1/`n' {
>>     local y : word `i' of `years'
>>     local ll : word `i' of `linet'
>>     twoway connected college_mean size_b if college == 0 & yearobs == `y', ///
>>     msize(tiny) lpattern(`ll') sort ||
>> }
>>
>> but this doesn't even give me a desired result because in the loop,
>> the previous graph gets substituted by the new one. Also, no line
>> patterns are included, nor the legend, which is vital for me.
>>
>> Ideally, I would be able to also specify the year list as, e.g.
>> (1975-1986 1999 2002). Of course there would not be enough line
>> patterns for all these years but in that case, I would do with the
>> default colors.
>>
>> Thank you for your consideration.
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>> *   http://www.ats.ucla.edu/stat/stata/
>*
>*   For searches and help try:
>*   http://www.stata.com/help.cgi?search
>*   http://www.stata.com/support/faqs/resources/statalist-faq/
>*   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index