Announcement

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

  • Graph export multiple using loops

    I have been trying to create and export multiple graphs using the following code, instead I am getting a single graph that essentially does what I want but crams all my graphs into one:

    Code:
    local variables a b c d e f
    
    foreach x in `variables'{
    gr hbar `x', over(year) by(treat) title("fancy title") ytitle ("Years")
    gr export "$graphdir/`x'.png", replace
    }
    Instead of creating individual graphs I am getting one big graph with all my variables. Is there anyway to split this within the loop? Is there anyway of affixing what Nick Cox said in this post to my code https://www.statalist.org/forums/for...s-using-a-loop?

    Thanks in advance!

    Lorien

  • #2
    Sorry, but I don't understand this at all. My understanding is that the code should produce 6 graphs. As you are showing neither data nor graphs it's a tussle between your statement and my failure to follow it.

    Comment


    • #3
      Sorry for the confusion, but I have village level panel data with treatment and control groups. I wanted to see the means in public goods over time in bar graph format. Instread of the 6 graphs, I am getting a single massive graph that looks like this:
      Click image for larger version

Name:	sample.png
Views:	2
Size:	31.4 KB
ID:	1498333

      Attached Files

      Comment


      • #4
        I expect that the code in post #1 is not the code used to produce post #3 (certainly "Fancy Title" is different). Experimenting with the code from post #1 and some sample data, I get results similar to post #3 when I replace (the equivalent of)
        Code:
        gr hbar `x', over(year) by(treat) title("fancy title") ytitle ("Years")
        with
        Code:
        gr hbar `variables', over(year) by(treat) title("fancy title") ytitle ("Years")
        When I do not make that change, I get each variable in a separate PNG.

        Always be alert to the possibility that the PNGs you are looking at saved on disk are those output by the latest run of the do-file. For instance, the code in post #1 does not show the global $graphdir being set. Was it set, and if so is that where you are looking for your PNGs?
        Last edited by William Lisowski; 14 May 2019, 12:10.

        Comment


        • #5
          Sorry for the mix up but here is the full code I have been using:
          Code:
          global graphdir "C:/Users/lorien/Dropbox/MY STUFF"
            
           local variables a b c d e f
          
          foreach x in `variables'{
          gr hbar `x', over(year) by(treat) title("Fancy title") ytitle ("Years")
          gr export "$graphdir/`x'.png", replace
          }
          I still get a single graph (below) in my global graph directory that combines all the variables in $variables global
          Click image for larger version

Name:	sample.png
Views:	1
Size:	68.5 KB
ID:	1498497

          Last edited by Lorien Nair; 15 May 2019, 11:32.

          Comment


          • #6
            You are creating and then using a local macro called variables, not a global macro. That alone doesn't explain anything, unfortunately.

            If you accept that we can't see or otherwise access your computer, it also follows that where you put these graphs is irrelevant to what we can say.

            That being so, and since use of the local isn't needed your code can be slimmed down to

            Code:
            foreach x in a b c d e f {    
                gr hbar `x', over(year) by(treat) title("Fancy title") ytitle ("Years")    
                gr export "`x'.png", replace
            }
            I can't see that the code would does anything but produce six separate graphs. I did what I could is simulate your problem with complete, self-contained code that fakes a dataset

            Code:
            clear
            set obs 6  
            gen treatment = cond(_n <= 3, 1, 2)  
            label def treatment 1 Control 2 Treatment  
            label val treatment treatment  
            gen year = cond(_n <= 3, 1988 + 5 * _n, 1988 + 5 * (_n - 3))  
            local y = 1  
            foreach v in a b c d e f {      
                gen `v' = `y'    
                graph hbar (asis) `v', over(treat) over(year)  name(G`y')      
                local ++y  
            }


            and indeed -- as you should check for yourself -- six graphs are created. So, sorry, but I don't understand why you're saying what you say. On the evidence you give it makes no sense to me.
            You can show us the results the graphs use by

            Code:
            collapse a b c d e f , by(year treat)
            dataex
            (See FAQ Advice #12 for dataex.)

            On the other hand, if you are really typing

            Code:
            foreach x in "`variables'" {
            then (1) omit the " " (2) see that telling us exactly what you typed is really important.

            Comment

            Working...
            X