Announcement

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

  • Multiple lines (>100) on a tsline plot?

    Hello,

    I have some panel data that spans 5 years (2000-2005) and 150 countries with LN GDP Per Capita. I know it'll look fairly messy, but is there a way to have all the lines on one graph? The only way I can think of doing it is by combining graphs in the following way:

    tsset country_code year
    graph twoway tsline ln_gdppc if country=="China"|| tsline ln_gdppc if country=="Germany"|| etc. etc.

    Is there a better way to do this? The final product I'm going for is grayed out lines for each country and then a dark line for the total/average.

    Thank you!

  • #2
    Please note our request to use full real names, not initials.

    tsline is not written to support panel data, and I often find that xtline is not what I want either.

    linkplot (SSC) offers one way forward. You don't give a data example, so I used the Grunfeld data. For reasons perhaps similar to yours I worked on logarithmic scale.

    Code:
    webuse grunfeld, clear
    gen ln_invest = ln(invest)
    egen gmean = mean(ln_invest), by(year)
    
    * ssc inst mylabels needed
    mylabels 1 10 100 1000, myscale(ln(@)) local(yla)
    
    set scheme s1color
    
    linkplot ln_invest year, link(company) recast(line) lc(gs12) ///
    plot(line gmean year, sort lw(medthick)) yla(`yla', ang(h))  ///
    legend(order(2 "geometric mean")) ytitle(investment)
    Click image for larger version

Name:	linkplot.png
Views:	1
Size:	73.4 KB
ID:	1480016


    Note that for this dataset a plain line plot would work too, once you have the right sort order. The twist with linkplot is extra trickery to rule out spurious connections between different incomplete panels. Hence points are joined if (and only if) they belong to the same group, here panel.
    Last edited by Nick Cox; 22 Jan 2019, 02:06.

    Comment


    • #3
      This is the equivalent code using line. You could write your own code to avoid using mylabels too, but I will leave that on one side. (If you just used ysc(log) and the original data you would need to work harder to get the geometric mean.)

      Code:
      webuse grunfeld, clear
      gen ln_invest = ln(invest)
      egen gmean = mean(ln_invest), by(year)
      
      * ssc inst mylabels needed
      mylabels 1 10 100 1000, myscale(ln(@)) local(yla)
      
      set scheme s1color
      
      line ln_invest year, c(L) lc(gs12) ///
      || line gmean year, sort lw(medthick) yla(`yla', ang(h))  ///
      legend(order(2 "geometric mean")) ytitle(investment)

      Comment

      Working...
      X