Announcement

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

  • Plotting yearly and monthly data in the same graph

    Hi all,

    I have two variables. Let's say I observe variable M each month. However, for variable Y, I only have one observation each year. Is there a smart way to plot these two variables in the same graph?

    Obviously, I can have the Y variable for each first month (plus eleven missings for the other months of the same year), but I was wondering if anyone has thought of another way. Ideally, I want a line rather than a scatter.

    Thanks!

  • #2
    Hi Arnau,
    Stata will happily connect your single non-missing yearly data points and plot the monthly data on the same graph:
    Code:
    clear
    set obs 10
    gen year = 2010
    replace year = year[_n-1] + 1 if _n > 1
    gen yr_data = runiform(0,1)
    expand 12
    bys year: gen month = 1
    bys year: replace month = month[_n-1] + 1 if _n > 1
    gen mth_data = runiform(0,1)
    gen date = mdy(month,1,year)
    format date %td
    tsset date
    bys year: replace yr_data = . if _n > 1
    tsline yr_data mth_data

    Comment


    • #3
      You will need one variable for the x axis which is best as a monthly date. Given that yearly data are best repeated 12 times.

      On a line graph you may use connect(l) for monthly data and connect(J) for yearly data.

      Maria Boutchkova #2 Your code is helpful but sometimes a little more complex than needed and even a little hard to follow for other reasons. What have daily dates to do with monthly and yearly data? Here's your code simplified:


      Code:
      clear
      set obs 10
      gen year = 2009 + _n
      gen yr_data = runiform(0,1)
      expand 12
      bys year: gen month = _n
      gen mth_data = runiform(0,1)
      gen mdate = ym(year, month)
      format mdate %tm
      tsset mdate
      tsline yr_data mth_data, c(J L)
      Last edited by Nick Cox; 01 Sep 2021, 07:07.

      Comment


      • #4
        Thank you both for your answers!

        Comment

        Working...
        X