Announcement

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

  • xline for graph with 2 y-axis variables in a loop

    Hi there,

    I'm looking for help editing my graphs by adding a vertical line. I've looked at others posts regarding xlines and I haven't found an example applicable to my own yet.
    My x-axis variable is year, but I haven't set it as a time-series variable because I have multiple observations for each year and I'm doing the same analysis on multiple countries at once. I have variables called "breakdate_n" that are also years, and I want vertical lines at those dates but I don't think that's an option. Just as an FYI, some countries have 2, 3, or 4 breakdates. Is there a way to have a an xline(=exp) where the expression is based on the values of other variables?

    Even if I have to change my code manually for each country, I'm not even sure where to put xline in my code. Since I have 2 y-axis variables, I'm just confused about the syntax.
    I keep getting errors like
    Code:
    command xline is unrecognized
    or
    Code:
    invalid xline
    Here's my code:
    Code:
    levelsof country_name, local(countries)
            local i = 1
            foreach cntr of local countries {
                graph twoway line log_gdp year if country_name=="`cntr'" & year>=1960, yaxis(1) || line frag_index year if country_name=="`cntr'" & year>=1960, yaxis(2) ||, xline(1988) ||, xtitle("Year") ||, ytitle("Log GDP pc", axis(1)) ytitle("Fragility Status", axis(2)) ||, title("State Fragility vs Economic Growth:" "`cntr'")||, graphregion(fcolor(white))
            }

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str3 country_text_id float(log_gdp frag_index break_date1 break_date2 break_date3 break_date4)
    "AGO"  10.45542  1.2152677 1988 1994 2008    .
    "AGO" 10.731858   1.234922 1988 1994 2008    .
    "AGO" 10.881923   .6269374 1988 1994 2008    .
    "AGO" 10.643327   1.274072 1988 1994 2008    .
    "AGO"  10.74615   1.316786 1988 1994 2008    .
    "AGO"  10.91239    .665549 1988 1994 2008    .
    "AGO" 10.201345  1.2118758 1988 1994 2008    .
    "AGO" 10.723347  1.2559683 1988 1994 2008    .
    "AGO"  10.54481   .9980748 1988 1994 2008    .
    "AGO" 10.619946  1.2760165 1988 1994 2008    .
    "AGO" 10.609888    .613845 1988 1994 2008    .
    "AGO"  10.71222  1.3177582 1988 1994 2008    .
    "AGO" 10.441825  1.2087827 1988 1994 2008    .
    "AGO" 10.877696   .5086081 1988 1994 2008    .
    "AGO" 10.972347   .6135298 1988 1994 2008    .
    "AGO" 10.220302  1.2073386 1988 1994 2008    .
    "AGO"    10.786   .4398476 1988 1994 2008    .
    "AGO" 10.824695   .4323459 1988 1994 2008    .
    "AGO"  10.40382  1.2171367 1988 1994 2008    .
    "AGO" 10.450012  1.1468837 1988 1994 2008    .
    "AGO" 10.526987  1.2335123 1988 1994 2008    .
    "AGO" 10.308938  1.2144686 1988 1994 2008    .
    "AGO" 10.928567   .6122073 1988 1994 2008    .
    "AGO"  10.99731   .6132653 1988 1994 2008    .
    "AGO" 10.787613   .6219251 1988 1994 2008    .
    "AGO" 10.714526   .6182091 1988 1994 2008    .
    "AGO"  10.72149  1.2623475 1988 1994 2008    .
    "AGO" 10.444508  1.2205286 1988 1994 2008    .
    "AGO" 10.441865   1.217461 1988 1994 2008    .
    "AGO" 10.716618  1.2629957 1988 1994 2008    .
    "AGO" 10.724424  1.2556443 1988 1994 2008    .
    "AGO" 10.922184   .6294964 1988 1994 2008    .
    "AGO"  10.69633   1.253873 1988 1994 2008    .
    "AGO" 10.701797  1.2545965 1988 1994 2008    .
    "AGO" 10.985185   .6087564 1988 1994 2008    .
    "AGO" 10.926356   .6090902 1988 1994 2008    .
    "AGO" 10.540483   .6111767 1988 1994 2008    .
    "AGO" 10.950673   .6250567 1988 1994 2008    .
    "AGO" 10.812092   1.234598 1988 1994 2008    .
    "AGO" 10.972312   .6169119 1988 1994 2008    .
    "AZE"   7.98687   .6839319 1995 2009    .    .
    "AZE"  6.437909   .7808939 1995 2009    .    .
    "AZE"  7.963167     .68226 1995 2009    .    .
    "AZE"  7.085763   .3712458 1995 2009    .    .
    "AZE"  7.957169   .6806318 1995 2009    .    .
    "AZE"  7.998396   .6960278 1995 2009    .    .
    "AZE"  7.808698   .6802654 1995 2009    .    .
    "AZE"  7.357097  .56476307 1995 2009    .    .
    "AZE"  8.029163   .6197111 1995 2009    .    .
    "AZE"  7.992052   .6796435 1995 2009    .    .
    "AZE"  7.978592   .6425329 1995 2009    .    .
    "AZE"   6.48746   .5928091 1995 2009    .    .
    "AZE"  6.635893   .6290216 1995 2009    .    .
    "AZE"  6.902326   .6941924 1995 2009    .    .
    "AZE"  6.991951   .7058945 1995 2009    .    .
    "AZE"  7.379645   .7176771 1995 2009    .    .
    "AZE"  6.807907   .6960438 1995 2009    .    .
    "AZE"  7.984822   .6580858 1995 2009    .    .
    "AZE"  8.015525   .6020746 1995 2009    .    .
    "AZE"  6.732938   .6751069 1995 2009    .    .
    "AZE"  6.573279   .6133147 1995 2009    .    .
    "AZE"  7.071701   .7011274 1995 2009    .    .
    "AZE"  7.888372   .6783569 1995 2009    .    .
    "AZE"  7.971669    .684173 1995 2009    .    .
    "AZE"  8.030635   .6132265 1995 2009    .    .
    "AZE"  7.308037    .759943 1995 2009    .    .
    "AZE"  6.574989   .7698617 1995 2009    .    .
    "AZE"  7.593194    .761682 1995 2009    .    .
    "AZE"  6.440726   .6224696 1995 2009    .    .
    "AZE"  6.819591   .6791531 1995 2009    .    .
    "BDI" 11.894114   .3613694 1965 1971 1991 1997
    "BDI" 11.947257  1.1612384 1965 1971 1991 1997
    "BDI"  12.02927   .8222827 1965 1971 1991 1997
    "BDI"  12.11195  1.0639845 1965 1971 1991 1997
    "BDI"  12.23499 .004734943 1965 1971 1991 1997
    "BDI" 12.085053   .9748418 1965 1971 1991 1997
    "BDI" 12.065687  1.0987519 1965 1971 1991 1997
    "BDI" 12.323843 .009325538 1965 1971 1991 1997
    "BDI" 12.207047 .071428366 1965 1971 1991 1997
    "BDI" 12.075459   .4417831 1965 1971 1991 1997
    "BDI"  12.07026    .702015 1965 1971 1991 1997
    "BDI"  11.96029  1.1691483 1965 1971 1991 1997
    "BDI" 12.157218   .3474681 1965 1971 1991 1997
    "BDI" 12.405495  .19319917 1965 1971 1991 1997
    "BDI" 12.287065 .014639793 1965 1971 1991 1997
    "BDI"  12.04373   .8425399 1965 1971 1991 1997
    "BDI"  12.00956   .4847721 1965 1971 1991 1997
    "BDI"  12.39248  .04916275 1965 1971 1991 1997
    "BDI" 11.873043   .4891188 1965 1971 1991 1997
    "BDI" 11.950957   .5131734 1965 1971 1991 1997
    "BDI" 12.179134    .712879 1965 1971 1991 1997
    "BDI"  12.38418  .05729524 1965 1971 1991 1997
    "BDI" 11.975986  1.1727219 1965 1971 1991 1997
    "BDI"  12.18396   .4607017 1965 1971 1991 1997
    "BDI"   11.9343   .3522184 1965 1971 1991 1997
    "BDI" 12.083623   .6955443 1965 1971 1991 1997
    "BDI" 12.007958   .8903823 1965 1971 1991 1997
    "BDI" 12.402332  .17397664 1965 1971 1991 1997
    "BDI"  12.27145  .02277323 1965 1971 1991 1997
    "BDI" 12.276467   .7411436 1965 1971 1991 1997
    end
    Let me know if I can clarify anything.
    Thank you!

  • #2
    Your code mentions year but there is no such variable in the example dataset.

    The code looks pretty tangled to me. In essence the syntax || is needed if and only if it precedes a new twoway command, but it is best avoided otherwise.

    I got as far as this code and feel moderately optimistic that it is close to what you want, but it won't run in the absence of a
    year variable.

    Some of the differences between my code and yours are matters of personal style.

    I've cut out the word versus. There is (to me) surprising inconsistency on whether one should say y versus x or x versus y but either way I don't think it's standard to say either when both variables concerned are shown on the y axis. More at https://stats.stackexchange.com/ques...-data-analysis

    Code:
    preserve 
    
    keep if year >= 1960 
    
    egen G = group(country_name), label 
    su G, meanonly 
    
    forval g = 1/`r(max)' {
       local cntr : label (G) `g'
       line log_gdp year if G == `g', yaxis(1) || line frag_index year if G == `g', yaxis(2) xline(1988) xtitle("Year") ytitle("Log GDP pc", axis(1)) ytitle("Fragility Status", axis(2)) title("State Fragility and Economic Growth:" "`cntr'") graphregion(fcolor(white)) name(G`g', replace)
    }
            
    restore

    Comment


    • #3
      Ah, thank you! This is really great and very helpful! and thanks for clarifying the meaning of
      Code:
      ||
      I was confused by that.

      Comment

      Working...
      X