Announcement

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

  • Combining graphs for many countries in a loop

    I am using Stata 13 and panel data over four years in long format. I would like to plot employment rates for 25 countries, each with its own line and in one graph, on a common x and y axis and would like to do this in a loop to save space. My x axis should be the year and y axis the employment rate.My code, however, generates 25 different mini-graphs within one graph box, each however on its own x and y axis. I have done some searching but cannot seem to combine these 25 graphs into one.

    My code is below, where c refers to country (string), ER_w is the employment rate variable and y is the year. I have excluded labels and scales here for simplicity.

    Any advice is much appreciated!
    Cortnie Shupe
    PhD Candidate

    set graphics off
    qui levelsof c, loc(levels)
    foreach lev of loc levels {
    preserve
    keep if c=="`lev'"
    tw (line ER_w y, sort), name(gr`lev', replace)
    loc grnames "`grnames' gr`lev'"
    restore
    }
    set graphics on
    gr combine `grnames', altshrink nocopies xcommon
    Last edited by Cortnie Shupe; 09 Dec 2015, 11:32.

  • #2
    I am puzzled that the result you are getting has different x-axes. For one thing, you specified -xcommon-, which should force a common axis in all 25 panels. Also, you describe your data as covering a four year period, so even without forcing -xcommon- I would expect every panel to use the same x-axis. Is your Stata fully up-to-date? To force them all to use the same y-axis, add the -ycommon- option to your -graph combine- command.

    Or, by "a single graph" do you mean you want a single panel with 25 lines on it? If that's what you're looking for, the approach is different:

    Code:
    reshape wide ER_w, i(year) j(country) string
    graph twoway line ER_w* y, sort
    Of course, you may well have a rather messy spaghetti-like graph with 25 overlaid lines, and the legend will probably be larger than the graph itself. Labeling the ER_w* variables with something short might make the legend more tractable, but it will likely still be huge. Unless the 25 lines come out really separated from each other, I think you will end up preferring the 25-panel graph you already have.

    Comment


    • #3
      Thank you, Clyde for your quick response!

      Indeed, I was hoping to have the single panel with 25 lines on it and somehow separate them well enough that they could be legible. Probably, that hope is too optimistic, but I will give it a try with the code you provided.

      Many thanks again!
      Cortnie

      Comment

      Working...
      X