Announcement

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

  • Using for loop to plot multiple graphs.

    Hey Everyone,

    I am trying to plot multiple graphs using a for loop. At the time, I am trying to make the code change the title for each graph.

    I have data from 60 cities and 21 industries. The code I'm using is not generating the correct title.

    Code:
    egen city_group = group(msa)
    egen industry_group = group(industry_code)
    
    gen long obs = _n
    foreach i of num 1/60{
    foreach j of num 1/21{
    su obs if name_city == "`i'", meanonly
    local t = name_city[r(min)]
    su obs if industry_title == "`j'", meanonly
    local s = industry_title[r(min)]
    twoway line export year if city_group == `i' & industry_group == `j', xtitle("Year") ytitle("coeficient export") title("`t' `s'")
    graph save graph_`i'_`j', replace
    }
    }
    Thanks,
    Hussain

  • #2
    Your code rests, I think, on the assumption that city_group and name_city are in 1:1 correspondence as numeric and string versions of each other and that industry_group and industry_title are in similar 1:1 correspondence. I guess that is quite untrue.

    I am going to guess that you need with your approach

    Code:
     
     su obs if city_group == `i', meanonly  
     local t = name_city[r(min)]  
     su obs if industry_group == `j', meanonly     
     local s = industry_title[r(min)]
    But the approach would go more cleanly something like this

    Code:
    egen group = group(msa industry_code), label 
    
    su group, meanonly 
    
    forval k = 1/`r(max)' { 
         twoway line export year if group == `k', xtitle("Year") ytitle("coeficient export") title("`: label (group) `k''")
         graph save graph_`k', replace
    }
    although the implication that you want to look at ~1200 different graphs is a little alarming.

    Comment


    • #3
      Thank you Nick.

      I have to check the variable of interest in each industry and city over time. I figured that this is the easiest way to do it.

      Comment

      Working...
      X