Announcement

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

  • Creating PDF of Multiple Graphs using Loops

    Dear all,

    I would like to create a PDF of multiple graphs.
    Below is the code that I have and works. I'd basically like to run it for an entire variable list, for example by using a loop, and ideally combine it into a single PDF.

    Code:
    xtline income if inlist(stateabbr, "RI", "VT", "MD", "NV")), overlay title(income) plot1(lc(sienna)) plot2(lc(dknavy)) plot3(lc(forest_green)) plot4(lc(orange)) plot5(lc(sand)) plot6(lc(edkblue)) xtitle("year") legend(order(1  4 2  3))
        graph export "/Results/Figures/graphs.pdf", replace
    The variables I'd like to run this code for are: income, gdp, and employment. I'd like the title of each graph to have the title of the variable name.

    The code sample I have is as follows:

    Code:
    if "`varlist'" == "" local varlist "income gdp employment"
    
    tokenize `varlist' 
    local varlist_vlist 
    local i = 1
        while "``i''" != "" {
            local varlist_vlist `varlist_vlist' ``i''
            local i = `i' + 1
        }
    
    foreach var of `varlist_vlist' {
    
    xtline `v' if inlist(stateabbr, "RI", "VT", "MD", "NV"), overlay title("`v'"") plot1(lc(sienna)) plot2(lc(dknavy)) plot3(lc(forest_green)) plot4(lc(orange)) plot5(lc(sand)) plot6(lc(edkblue)) xtitle("Year") legend(order(1 4 2 3))
    
     graph export "/Results/Figures/graphs.pdf", replace
    }
    Right now, I'm getting the error of "varlist required" so I think I did something wrong in setting it up. I can't tell if naming each graph according to the variable name (e.g. the income graph titled "income" would work either.

    I've also tried
    Code:
    foreach var in `varlist_vlist' {
        preserve
            local label: var label `var'
            
        xtline `var' if inlist(stateabbr, "RI", "VT", "MD", "NV"), overlay title(log_real_pinc_pc) plot1(lc(sienna)) plot2(lc(dknavy)) plot3(lc(forest_green)) plot4(lc(orange)) plot5(lc(sand)) plot6(lc(edkblue)) xtitle("year") legend(order(1  4 2  3))
        graph export "Results/Figures/graphs2.pdf", replace
        
        restore 
    }
    Which doesn't lead to any error codes, but doesn't export anything either.

    I'd appreciate any advice and insights you have!

  • #2
    I think you are trying to write code (or use found code) that you are not understanding. For instance, study the output of the following.

    Code:
    local varlist "income gdp employment"
    
    tokenize `varlist'
    local varlist_vlist
    local i = 1
    while "``i''" != "" {
      local varlist_vlist `varlist_vlist' ``i''
      local i = `i' + 1
    }
    
    mac list _varlist _varlist_vlist
    Results in

    Code:
    . mac list _varlist _varlist_vlist
    _varlist:       income gdp employment
    _varlist_vlist: income gdp employment
    Both local macros contain identical contents, by definition. The tokenization and loop do nothing to the desired variable list.

    Skipping over that, the for loop has a number of errors, so I'm not surprised Stata gave you errors there. The immediate issue is that the syntax of the -foreach- loop is wrong (see -help foreach-), and withing are references to local macro -v- which doesn't exist.

    This is a minimal example to show how to use looping to make graphs, using the -auto- dataset that is included with every Stata installation so that we can have a reproducible example. Each graph has the variable label as a title.

    Code:
    sysuse auto, clear
    
    local varlist price length
    foreach v of varlist `varlist' {
      local vlab : var lab `v'
      hist `v', title("`vlab'") name(`v', replace)
    }
    Lastly, I take your question to ask for a single PDF that contains multiple graphs. Each iteration through the loop overwrites the entire file, and contains only the most recently produced graph. -graph export- will not be useful here, and a different approach using -putdocx- or -putpdf- is called for. Perhaps something like the below can be a useful starting point.

    Code:
    sysuse auto, clear
    
    putpdf begin , pagesize(letter)
    
    local varlist price length
    
    foreach v of varlist `varlist' {
      local vlab : var lab `v'
      hist `v', title("`vlab'") name(`v', replace)
      graph export "`v'.png", name(`v') as(png) replace
      putpdf paragraph, halign(center)
      putpdf image "`v'.png"
    }
    
    putpdf save "graphs.pdf", replace

    Comment


    • #3
      Dear Leonardo,
      Thank you for your help!
      Yes you are correct in your assumption in me not understanding the code.
      The one you provided works, except I am getting the error "could not find graph window" (r(693)) even when the window is open. I tried your code with the auto data and it worked fine and I can't tell what the difference is with what I have below. I think it could be an issue with the
      Code:
       graph export "`v'.png", name(`v') as(png) replace
      line because when I took out the putpdf commands, I got the same error. Do you have any ideas?
      Thank you!

      Code:
       putpdf begin, pagesize(letter)
         
       local varlist income gdp employment
      
       foreach v of varlist `varlist'{
       local vlab: var lab `v'
       xtline `v' if inlist(stateabbr,"RI", "VT", "MD", "NV"), overlay title("`vlab'") plot1(lc(sienna)) plot2(lc(edkblue)) plot3(lc(forest_green)) plot4(lc(orange)) xtitle("year") legend(order(1  4 2  3))
        graph export "`v'.png", name(`v') as(png) replace
        putpdf paragraph, halign(center)
        putpdf image "`v'.png"
       }  
       
       putpdf save "/Results/Figures/graphs.pdf" , replace

      Comment


      • #4
        Ah I think it's an issue with the title("`vlab'"). Maybe there is incompatibility with overlay, but the graphs are not being named -- that's why STATA can't find them by that name.

        Edit: I think I was close. It's because my version doesn't have the name(`v', replace) after xtline. Let me check.
        Edit2: Yes that was the issue! Works now!! Thank you again for your help.
        Last edited by John Singer; 12 Oct 2021, 10:44.

        Comment

        Working...
        X