Announcement

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

  • Using macro to plot multiple lines in a single graph

    Hi,

    I have a panel dataset with a single variable:
    Year Country Variable_X Rank
    2000 ARG 64 3
    ... ... ... ...
    2015 ARG 34 3
    2000 AUS 67 1
    ... ... ... ...
    2015 AUS 54 1
    I created the Rank variable because I need to get the top/bottom 3 countries according the 2015 value of Variable X and plot them in the same graph, preferably with the top in the primary y axis and the bottom in the secondary y axis. I have to repeat this with multiple versions of Variable X so the top/bottom countries are random. I started with this:

    Code:
    preserve
    collapse (mean) Rank, by(Country)
    levelsof(Country) if Rank <= 3, local(bottom)
    levelsof(Country) if Rank >= [_N-3], local(top)
    restore


    Is there a way to "unpack" the members of the macros to plot them in a single graph? I had two ideas:
    Pseudocode 1
    twoway (tsline Variable_X if Country is in top, yaxis(1))
    (tsline Variable_X if Country is in bottom, yaxis(2))

    Pseudocode 2
    xtline Variable X if Country is in top or if Country is bottom, overlay

    But I couldn't implement neither of them. Any suggestions?

    ​​​​​​​Thank you.

  • #2
    I don't follow all of this but I think

    Code:
     Rank >= [_N-3]
    is wrong. At best that is the number of observations in the dataset minus 3, which doesn't seem what you want.

    This shows some technique. FWIW although xtline or tsline are convenient helper commands I often find it easiest to work with more fundamental graph commands.
    Click image for larger version

Name:	trailing.png
Views:	1
Size:	37.5 KB
ID:	1429250



    Code:
    webuse grunfeld, clear
    
    egen rank = rank(mvalue) if year == 1940
    egen Rank = max(rank), by(company)
    
    su Rank, meanonly
    local n = r(max)
    
    sort company year
    
    twoway line mvalue year if Rank <= 3, c(L) lcolor(red) ///
    || scatter mvalue year if y == 1954 & Rank <= 3, ms(i) mlabcolor(red) mla(company)  ///
    || line mvalue year if Rank > `n'-3, c(L) lcolor(blue)  ///
    || scatter mvalue year if y == 1954 & Rank > `n'-3, ms(i) mla(company) mlabcolor(blue) ///
    legend(off) xtitle("") ysc(log) yla(5000 2000 1000 500 200 100)
    In this example, trailing marker labels work well. You won't always be so lucky. The commonest and easiest fix needed if you use them is that you may need to extend the x axis using xsc() to accommodate longer names.

    Conversely, one of my prejudices is that legends often work badly in taking up valuable space and in obliging the reader of the graph to go back and forth to interpret the graph correctly. I tell my students and advise my colleagues: Lose the legend! Kill the key! (if you can).

    Comment


    • #3
      Dear Nick,

      Thank you for your answer, I tried to adapt your code and got the results I needed.

      Well I thought that since I was collapsing by the observation identifier (Country), _N would be equal to the maximum of Rank, but I guess I can create a variable with the maximum of Rank just to make sure.

      Your comment about the labels is excellent, I'll try to implement it.

      Best regards,
      Steven

      Comment


      • #4
        Fair point on the ranks except that >= max - 3 usually yields 4 countries not 3. With 10 countries you get 7, 8, 9, 10 . I got this wrong when I did it (I copied that part of your code without thinking!).

        Your approach and mine may need care if there are ties.

        Comment


        • #5
          You're right, I'l have to double check id there are any ties, thanks.

          Comment

          Working...
          X