Announcement

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

  • Time series line graphs

    Hi there

    I am trying to set up a time series line graph to illustrate the growth rate of two variables. I have generated two new variables to show the yearly growth rate, then added these to a two way line graph. Please see code below

    gen L_Prod = L.Avg_Prod
    gen GR_Prod = 100*(Avg_Prod-L_Prod)/L_Prod

    gen L_Import_Total = L.Import_Total
    gen GR_Import = 100*(Import_Total-L_Import_Total)/L_Import_Total

    label var GR_Import "Import value growth rate (%)"

    label var GR_Prod "Productivity growth rate (%)"

    twoway tsline GR_Prod, yaxis(1) || tsline GR_Import, yaxis(2)

    The x axis is year but I want to change the range. I have tried to use xlabel but it doesn't recognise any years I put in. Eg I tried xlabel (2016 2017 ...) and also xlabel (2016(1)2021).

    Each time I had an error message saying 2016 is an invalid name. Perhaps this is because I'm working with panel data as apposed to time series data? Is there any way around this?

    Screenshot of graph is attached for reference.

    Thanks in advance
    Attached Files

  • #2
    I can't reproduce your problem in the absence of a data example. With panel data, a recipe like

    Code:
    webuse grunfeld, clear 
    
    gen GR_invest = 100* D.invest/L.invest 
    
    tsline GR_invest
    just produces a tangle of lines, one for each panel. But adding an option like

    Code:
    tsline GR_invest, xla(1936(2)1954)
    works to change the axis labels as shown.

    Note the simpler way to get growth rates in my first code block.

    Comment


    • #3
      Try taking out any space after xlabel and before its argument. So xlabel(2016(1)2021) not xlabel (2016(1)2021) noting also that xlabel(2016/2021) is equivalent.

      Comment


      • #4
        Thanks a lot for your help Nick, that is much appreciated.

        Your suggestions worked and I have attached an updated screenshot of the graph.

        One minor issue is that there appears to be a gap on the x axis before the first label (2016). This isn't the end of the world but I was wondering if there's any way to remove this?
        Attached Files

        Comment


        • #5
          You can try xsc(r(2016, 2021)) or -- if that does not work -- temporarily dropping 2015 from the dataset.


          Code:
          preserve 
          drop if year == 2015 
          * your unstated command 
          restore
          I recommend almost any graph scheme over the default s2color.

          Comment

          Working...
          X