Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • tsline option and combining several tsline graphs

    Dear all,

    I am currently using the following code:

    twoway (tsline var1_1) (tsline var1_2) (tsline var1_3) (tsline var1_4), legend(off)
    graph save var1
    twoway (tsline var2_1) (tsline var2_2) (tsline var2_3) (tsline var2_4), legend(off)
    graph save var2
    twoway (tsline var3_1) (tsline var3_2) (tsline var3_3) (tsline var3_4), legend(off)
    graph save var3
    twoway (tsline var4_1) (tsline var4_2) (tsline var4_3) (tsline var4_4), legend(off)
    graph save var4
    graph combine var1.gph var2.gph var3.gph var4.gph, note("Blue group 1" "Red group 2" "Green group 3" "Yellow group 4")


    This produces 4 graphs for respectively variables var1-var4. Each graph shows 4 tslines of the respective variable for 4 different groups/subsamples of my dataset.
    Because the legend (explaining which color stands for which of the four graphs) would be the same for all 4 graphs, I omit the legend and then add the explanations in the final combined graph.

    Now I have the following questions:

    (1) Rather than adding the legend substitute with "note" at the end, in which case it does not visually display the respective color, is there a better way to ensure that the legend is not shown in each separate graph, but is shown in the same way in which it would be shown there just in the final combined graph??

    (2) Groups 1 and 2 are related as are groups 3 and 4. Therefore I would like to show groups 1 and 2 in the same color, and show groups 3 and 4 in another common color (ideally one that will look different from color 1 when printed in black and white). Then I would like to display 1 and 3 in solid and 2 and 4 in closely dotted lines. How do I do this? I found the option -cl- for color but
    twoway (tsline var1_1, cl(blue)) (tsline var1_2) (tsline var1_3) (tsline var1_4), legend(off)
    would not work.

    Can anyone help here?

    Thank you so much and kind regards,
    Peter

  • #2
    If you get into a mess, back up and try something different.

    Here there are no data to play with and no graph to look at. So, let's fake one to give the flavour and then suggest what else to do instead. In short, if you restructure the data so that your four groups of variables become variables in four groups of another variable, then the by() option takes care of most of the scaffolding you need.

    Code:
    clear 
    set scheme s1color 
    set obs 50 
    gen time = _n + 1960 
    
    forval j = 1/4 { 
        forval k = 1/4 { 
            gen var`j'_`k' = (time - 2000) + (`j'^2) * rnormal() 
        }
    } 
    
    tsset time 
    
    forval j = 1/4 {  
        twoway (tsline var`j'_1) (tsline var`j'_2) (tsline var`j'_3) (tsline var`j'_4), legend(off)
        graph save var`j', replace 
    }
    graph combine var1.gph var2.gph var3.gph var4.gph, note("Blue group 1" "Red group 2" "Green group 3" "Yellow group 4")
    
    reshape long var, i(time) j(group) string 
    split group, parse(_) destring 
    drop group 
    separate var, by(group2) veryshortlabel 
    tsline var? , by(group1,  note("")) lp(solid dash solid dash) ///
    lc(blue red red blue) legend(row(1)) yla(, ang(h)) xtitle("") xla(1960(10)2010)
    Here is your kind of graph (the colours won't necessarily make sense) and below my alternative. I didn't pay close attention to your secondary questions, but I've been disappointed by dotted lines as copied to other software and tend to use dash and solid. Blue and red look different to most sighted people, but I think you need something else, or a different design altogether for printing in black and white.


    Click image for larger version

Name:	combine2.png
Views:	1
Size:	25.1 KB
ID:	1335234




    Click image for larger version

Name:	combine1.png
Views:	1
Size:	21.7 KB
ID:	1335233


    Comment


    • #3
      Dear Nick,

      thanks so much for those very helpful suggestions!

      I understand that reshaping the dataset as you suggest and then using the "by" option does the job quite neatly, but somehow I get the error message "option control not allowed". I presume this is because I'm still using Stata 11 (working on getting a newer version), but I found that -grc1leg- by Vince Wiggins does also allow me to just keep one legend for all graphs.

      I have however adopted your -lc- and -lp- options, which have been working nicely also on my old Stata.

      I still have one other problem though: I now create a -graph combine- (or rather -grc1leg-) panel of many graphs that I have previously created by looping over the variable names. Now I would like to add above each graph in the panel a title with the variable label. I would be happy to either add all "subtitles" (?) in the grc1leg command, or to add to each graph in the loop its label as a title.

      In a previous Forum post (whose link I forgot) I have found that I can add the labels as titles when instead of looping over variable names I loop over numbers. So instead of my initial

      foreach var of varlist variable1 variable2 variable3 variable4 {
      tsline `var'
      graph save
      `var'
      }
      grc1leg variable1.gph variable2.gph variable3.gph variable4.gph


      I now write:

      local varlist variable1 variable2 variable3 variable4
      local labellist label1 label2 label3 label4
      forval p = 1/4 {
      local var: word `p' of varlist
      local label: word `p' of labellist
      tsline `var', title(`label')

      graph save `var'
      }

      grc1leg variable1.gph variable2.gph variable3.gph variable4.gph

      but that yields the message that there is a syntax error.

      Would Nick, or someone else, happen to see how this can be made to run through, or how otherwise I can attach a title to each component of the combined graph?

      Thank you so much and best regards,
      Peter









      Comment


      • #4
        I can't comment on

        the error message "option control not allowed". I presume this is because I'm still using Stata 11 (working on getting a newer version)
        as I can't see your code, except to say that my example was done in Stata 10, so version is irrelevant.

        You don't show the current graph and you don't give code for your new question we can copy and use straight off.

        #2 was an example of how to do that.

        Your syntax error is however that you need (e.g.)

        Code:
        local var: word `p' of `varlist'

        Comment


        • #5
          Hi Nick,

          thanks, fair point. I have saved your example on how to create an example dataset and will use that for my next question!

          For the current one your advice on the syntax error has been very helpful and (after some more errors, which I could luckily solve myself through trial and error) the code seems to be working now.

          All best,
          Peter

          Comment

          Working...
          X