Announcement

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

  • Graph combine with local macro

    Dear Stata users,

    I want to plot several variables across time by 4 regions. This requires 4 separates graphs, which I need to combine into 1. I use the following code:


    Code:
    foreach var in Midwest West South Northeast {
    twoway line econm year if region=="`var'" || line socialm year if region=="`var'" ///
     || line infrstrm year if region=="`var'" || line pubservm year if region=="`var'", ///
      xlabel(2007 (2) 2017)  title("`var'")
    
     local graphs "`graphs' `var'"
    }
    
    graph combine `graphs'
    The last line of code gives me the following error:

    Code:
    Midwest is not a memory graph
    r(198);
    Could you please help me figure out what I am doing wrong?

    Thank you



    Last edited by Michael Foreman; 26 May 2018, 11:41.

  • #2
    Stata gives you that error message because you are trying to combine graphs that do not exist.
    You can use the option - name()- of twoway to temporarliy store them.
    In your case - name(`var') -

    Comment


    • #3
      Thank you, Raffaele. I added - name(`var') - and was able to combine all 4 graphs. However, when I run the loop again, i get -graph Midwest already exists- error even after i use -macro drop _all-. the are no graphs saved in my working directory either. What am i doing wrong?

      Comment


      • #4
        Hi Michael,
        Graphs are not macros, so -macro drop _all- doesn't serve the purpose. Moreover, -name()- does not save the graphs to disk so you won't find them in your working directory.
        To solve your problem, you can either close the graph editor and the graphs will disappear or add the option -replace- in -name()-, i.e. name(`var', replace)

        Comment


        • #5
          Thank you for your help, Raffaele, it worked.

          Comment


          • #6
            Not the main question, but your code can be simplified to one line without any combining of graphs at all:

            Code:
            twoway line econm socialm infrstrm pubservm year, by(region) xlabel(2007(2)2017)
            assuming that Midwest West South Northeast are the only regions. This sounds like the United States. If there is some other region then you need a condition such as

            Code:
            if inlist(region, "Midwest", "West", "South", "Northeast")
            If you want the regions in a particular order, just encode to that order.

            In general, by() will usually give a better graph than graph combine when they are alternatives. You can ask for different y axis scales.

            I can't guess how well that the graph will work as you don't give a data example. I make the example 11 years x 4 regions x 4 responses, so it will go nicely through dataex (FAQ Advice #12).
            Last edited by Nick Cox; 27 May 2018, 04:29.

            Comment


            • #7
              This is very helpful, Nick, thank you for the advice!

              Comment

              Working...
              X