Announcement

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

  • Line graph (mean) with panel data

    Hey Stata community,

    I tried to plot something using a panel data set and the "line" command (sales over time from 20 different companies).

    Unfortunately, Stata shows me for each company separate lines. I just want to have 1 line over time (the mean of sales from all companies).

    To demonstrate, I added a graph.

    How can I see the mean line instead of 20 lines in the plot? Is there a mean-command that works?

    Many thanks and best regards!
    Click image for larger version

Name:	sales.png
Views:	1
Size:	370.7 KB
ID:	1612425


  • #2
    Here's some sample code to run that demonstrates two approaches to getting a graph of mean values.
    Code:
    webuse pig
    describe
    // not means
    graph twoway line weight week, name(g0)
    // means, bars rather than line - but maybe better for you?
    graph bar weight, over(week) name(g1)
    // calculate means and graph as a line
    preserve
    collapse (mean) weight, by(week)
    list
    graph twoway line weight week, name(g2)
    restore

    Comment


    • #3
      Thank you William, it worked!

      Comment


      • #4
        One reason why the graph in #1 is a mess is that you have spurious connections between the last value for one company and the first value for the next. You don't give your code, but I imagine it was something like


        Code:
        line Sales Year
        This code for the pig data shows some technique, You can run this code, unless you are behind a major firewall.


        Code:
        webuse pig, clear
        egen mean = mean(weight), by(week)
        egen tag = tag(week)
        line weight week, c(L) xla(1/9) lc(gs12) || line mean week if tag , ytitle(weight (units)) legend(order(2))
        noting particularly

        1. The c(L) option

        2. Showing all the data as backdrop and a summary more prominently

        In the case of your data, I wonder whether log scale would help and using a summary other than the mean, say the median or geometric mean. Naturally, the mean through its relation to the total may be informative too.

        Comment


        • #5
          Thanks Nick, I will try this!

          Comment


          • #6
            For the historical record, here is another approach using margins. Not as pretty or elegant as Nick's for sure.
            Code:
            webuse pig, clear
            regress weight i.week
            margins week
            marginsplot, noci

            Comment

            Working...
            X