Announcement

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

  • Drawing a yearly time graph with months as observations

    Hello Stata people;

    I'm using Stata version 1301 for this data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str10 Month float(Inflation Core_Inflation)
    "2014-01-01" 1.55776 1.60703
    "2014-02-01" 1.12047 1.55481
    "2014-03-01" 1.61269 1.64566
    "2014-04-01" 2.01513 1.82106
    "2014-05-01" 2.16695 1.94553
    "2014-06-01" 2.05898 1.92286
    "2014-07-01" 1.97424 1.84496
    "2014-08-01"  1.7151  1.7364
    "2014-09-01" 1.68405 1.74095
    "2014-10-01" 1.60954 1.81763
    "2014-11-01" 1.23152 1.74159
    "2014-12-01"  .65312 1.62242
    "2015-01-01" -.22993 1.63163
    "2015-02-01" -.08703 1.68808
    "2015-03-01" -.02203 1.74538
    "2015-04-01" -.10403 1.80283
    "2015-05-01"  .03503 1.75094
    "2015-06-01"  .17957 1.77727
    "2015-07-01"  .22569 1.83463
    "2015-08-01"   .2413 1.85063
    "2015-09-01"  .00884  1.8971
    "2015-10-01"  .12762 1.91353
    "2015-11-01"  .43632 1.99743
    "2015-12-01"  .63872 2.07151
    "2016-01-01"  1.2375 2.14502
    "2016-02-01"  .84728 2.22257
    "2016-03-01"  .89162 2.14243
    "2016-04-01" 1.17263 2.15665
    "2016-05-01" 1.07848 2.25456
    "2016-06-01" 1.07929 2.26221
    "2016-07-01"  .86836 2.17014
    "2016-08-01" 1.05532 2.30873
    "2016-09-01" 1.54864 2.27112
    "2016-10-01" 1.68592 2.20456
    "2016-11-01" 1.68433 2.14542
    "2016-12-01"  2.0508 2.19712
    "2017-01-01" 2.51039 2.25021
    "2017-02-01" 2.81036 2.23535
    "2017-03-01"  2.4412 2.04585
    "2017-04-01" 2.17622 1.89657
    "2017-05-01" 1.85634 1.73709
    "2017-06-01" 1.64057 1.69912
    "2017-07-01" 1.72511 1.67696
    "2017-08-01" 1.92812 1.65524
    "2017-09-01" 2.18057 1.59539
    "2017-10-01" 2.02076 1.75964
    "2017-11-01" 2.17249 1.73761
    "2017-12-01" 2.12993 1.77017
    "2018-01-01" 2.15132 1.89127
    "2018-02-01" 2.26347  1.8777
    "2018-03-01" 2.33095 2.12278
    "2018-04-01"   2.471 2.15064
    "2018-05-01" 2.78192   2.273
    "2018-06-01" 2.80755 2.24552
    "2018-07-01" 2.85412  2.2676
    "2018-08-01" 2.64292 2.12089
    "2018-09-01" 2.33206 2.19768
    "2018-10-01" 2.49203 2.12641
    "2018-11-01" 2.14733 2.21538
    "2018-12-01" 2.00238 2.24853
    "2019-01-01" 1.48759 2.17943
    "2019-02-01" 1.51886 2.14109
    "2019-03-01" 1.88319 2.06656
    "2019-04-01" 2.00058 2.09134
    "2019-05-01" 1.79591 1.97243
    "2019-06-01" 1.67119  2.0746
    "2019-07-01" 1.82633  2.1657
    "2019-08-01" 1.73764 2.32114
    "2019-09-01"  1.6845 2.33001
    "2019-10-01" 1.73397 2.33781
    "2019-11-01" 2.09229  2.3547
    "2019-12-01" 2.31953  2.2906
    "2020-01-01" 2.59977 2.28174
    "2020-02-01" 2.34132 2.36766
    "2020-03-01" 1.49404 2.09774
    "2020-04-01"  .31305 1.43093
    "2020-05-01"   .1982 1.21846
    "2020-06-01"  .71666 1.18368
    "2020-07-01"  .99686  1.5607
    "2020-08-01" 1.28107 1.72239
    "2020-09-01" 1.39102 1.71906
    "2020-10-01" 1.23039 1.63641
    "2020-11-01" 1.17575 1.66938
    "2020-12-01" 1.32042 1.62431
    "2021-01-01" 1.35532 1.38724
    "2021-02-01"  1.6675 1.27389
    "2021-03-01" 2.62365 1.64723
    "2021-04-01" 4.13737 2.96524
    "2021-05-01" 4.92647 3.78468
    "2021-06-01" 5.31742 4.42319
    "2021-07-01" 5.26917 4.20735
    "2021-08-01" 5.18132 3.94863
    "2021-09-01" 5.36352 4.01364
    "2021-10-01" 6.22659 4.58326
    "2021-11-01" 6.86556 4.97442
    "2021-12-01" 7.15946 5.50416
    "2022-01-01" 7.57808 6.05844
    "2022-02-01" 7.94922 6.45552
    "2022-03-01" 8.54078 6.48031
    "2022-04-01" 8.23516 6.15731
    end
    As you can see, it is about monthly observations for Inflation and Core Inflation for the USA.

    First, I do want to change the "Month" variable in a way to be able to apply the tsset command on it, and then, as you can see, my observations are monthly defined, yet, if I'm going to show an observation for each month on my graph, it will too busy and unreadable, also, I'm not interested in monthly observations as much as in the annual tendency, so, could there be a way to have an annula graph, but also to show the months just as small ticks or something like that (witout the months' names of course, since the order of the dick in the year would show that)?

    With many thanks!

  • #2
    I think you've had all the answers you need in previous threads you started, or else this is just minor variations on previous themes.

    https://www.statalist.org/forums/for...-tsset-command

    https://www.statalist.org/forums/for...w-a-time-graph

    are two examples among several.

    You can and should extract monthly dates using standard functions. You could extract numeric daily dates and convert them to monthly dates instead; you would get the same result.

    Any inclination to tsset is irrelevant to getting a good graph. We've been round this before, in one of the threads cited above and elsewhere. Do you look back at your own threads ever?

    Values for every month are absolutely no problem, as this shows. Ticks for every month would just be noise to little effect, I suggest. I am not clear what else you mean otherwise.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str10 Month float(Inflation Core_Inflation)
    "2014-01-01" 1.55776 1.60703
    "2014-02-01" 1.12047 1.55481
    "2014-03-01" 1.61269 1.64566
    "2014-04-01" 2.01513 1.82106
    "2014-05-01" 2.16695 1.94553
    "2014-06-01" 2.05898 1.92286
    "2014-07-01" 1.97424 1.84496
    "2014-08-01"  1.7151  1.7364
    "2014-09-01" 1.68405 1.74095
    "2014-10-01" 1.60954 1.81763
    "2014-11-01" 1.23152 1.74159
    "2014-12-01"  .65312 1.62242
    "2015-01-01" -.22993 1.63163
    "2015-02-01" -.08703 1.68808
    "2015-03-01" -.02203 1.74538
    "2015-04-01" -.10403 1.80283
    "2015-05-01"  .03503 1.75094
    "2015-06-01"  .17957 1.77727
    "2015-07-01"  .22569 1.83463
    "2015-08-01"   .2413 1.85063
    "2015-09-01"  .00884  1.8971
    "2015-10-01"  .12762 1.91353
    "2015-11-01"  .43632 1.99743
    "2015-12-01"  .63872 2.07151
    "2016-01-01"  1.2375 2.14502
    "2016-02-01"  .84728 2.22257
    "2016-03-01"  .89162 2.14243
    "2016-04-01" 1.17263 2.15665
    "2016-05-01" 1.07848 2.25456
    "2016-06-01" 1.07929 2.26221
    "2016-07-01"  .86836 2.17014
    "2016-08-01" 1.05532 2.30873
    "2016-09-01" 1.54864 2.27112
    "2016-10-01" 1.68592 2.20456
    "2016-11-01" 1.68433 2.14542
    "2016-12-01"  2.0508 2.19712
    "2017-01-01" 2.51039 2.25021
    "2017-02-01" 2.81036 2.23535
    "2017-03-01"  2.4412 2.04585
    "2017-04-01" 2.17622 1.89657
    "2017-05-01" 1.85634 1.73709
    "2017-06-01" 1.64057 1.69912
    "2017-07-01" 1.72511 1.67696
    "2017-08-01" 1.92812 1.65524
    "2017-09-01" 2.18057 1.59539
    "2017-10-01" 2.02076 1.75964
    "2017-11-01" 2.17249 1.73761
    "2017-12-01" 2.12993 1.77017
    "2018-01-01" 2.15132 1.89127
    "2018-02-01" 2.26347  1.8777
    "2018-03-01" 2.33095 2.12278
    "2018-04-01"   2.471 2.15064
    "2018-05-01" 2.78192   2.273
    "2018-06-01" 2.80755 2.24552
    "2018-07-01" 2.85412  2.2676
    "2018-08-01" 2.64292 2.12089
    "2018-09-01" 2.33206 2.19768
    "2018-10-01" 2.49203 2.12641
    "2018-11-01" 2.14733 2.21538
    "2018-12-01" 2.00238 2.24853
    "2019-01-01" 1.48759 2.17943
    "2019-02-01" 1.51886 2.14109
    "2019-03-01" 1.88319 2.06656
    "2019-04-01" 2.00058 2.09134
    "2019-05-01" 1.79591 1.97243
    "2019-06-01" 1.67119  2.0746
    "2019-07-01" 1.82633  2.1657
    "2019-08-01" 1.73764 2.32114
    "2019-09-01"  1.6845 2.33001
    "2019-10-01" 1.73397 2.33781
    "2019-11-01" 2.09229  2.3547
    "2019-12-01" 2.31953  2.2906
    "2020-01-01" 2.59977 2.28174
    "2020-02-01" 2.34132 2.36766
    "2020-03-01" 1.49404 2.09774
    "2020-04-01"  .31305 1.43093
    "2020-05-01"   .1982 1.21846
    "2020-06-01"  .71666 1.18368
    "2020-07-01"  .99686  1.5607
    "2020-08-01" 1.28107 1.72239
    "2020-09-01" 1.39102 1.71906
    "2020-10-01" 1.23039 1.63641
    "2020-11-01" 1.17575 1.66938
    "2020-12-01" 1.32042 1.62431
    "2021-01-01" 1.35532 1.38724
    "2021-02-01"  1.6675 1.27389
    "2021-03-01" 2.62365 1.64723
    "2021-04-01" 4.13737 2.96524
    "2021-05-01" 4.92647 3.78468
    "2021-06-01" 5.31742 4.42319
    "2021-07-01" 5.26917 4.20735
    "2021-08-01" 5.18132 3.94863
    "2021-09-01" 5.36352 4.01364
    "2021-10-01" 6.22659 4.58326
    "2021-11-01" 6.86556 4.97442
    "2021-12-01" 7.15946 5.50416
    "2022-01-01" 7.57808 6.05844
    "2022-02-01" 7.94922 6.45552
    "2022-03-01" 8.54078 6.48031
    "2022-04-01" 8.23516 6.15731
    end
    
    gen mdate = monthly(substr(Month, 1, 7), "YM")
    
    local tickpos = ym(2013, 12) + 0.5
    local ticks `tickpos'
    
    forval y = 2014/2021 {
        local labelpos = `tickpos' + 6
        local labels `labels' `labelpos' "`y'"
        local tickpos = ym(`y', 12) + 0.5
        local ticks `ticks' `tickpos'
    }
    
    local labelpos = ym(2022, 5)  
    local labels `labels' `labelpos' " 2022"
    
    label var Core_Inflation "Core inflation"
    
    line *nflation mdate, lc(red blue) yla(, ang(h)) ytitle(%, orient(horiz)) xla(`labels', noticks) xticks(`ticks', tlength(*3)) xtitle("") legend(ring(0) pos(11) col(1)) xsc(r(. `=ym(2022, 6)')) xli(`ticks', lc(gs12))
    The main idea here is to loop over years defining a tick position for each year end and a label position and text for each year middle. There is some ad hoc code to show a tick at end 2013 and to adjust for the truncation for 2024 (surely, more data are available now). See Stata Tip 55: Better Axis Labeling for Time Points and Time Intervals for more.
    Click image for larger version

Name:	inflation.png
Views:	1
Size:	40.8 KB
ID:	1781572

    Last edited by Nick Cox; 04 Sep 2025, 04:51.

    Comment


    • #3
      Nick Cox Thanks for the help.

      I really forgot about those two previous posts of mine, I'm sorry, I thought that the question was a new problem for me.

      By "ticks", I was meaning that for each month in each year, I just want to have points on the graph line showing the month, just like in a previous graph of one of my posts.

      Anyway, thanks again for te help!

      Comment


      • #4
        When you say graph line do you mean a line connecting data points or a graph axis -- either the x axis or the y axis?

        You have made 205 posts, so which are you referring to?

        Comment


        • #5
          Nick Cox Yes exactly, thank you... Yes, I do mean connecting data points on the graph line itself.

          Comment


          • #6
            So when you say ticks I think you mean marker symbols.

            Comment


            • #7
              Nick Cox Yes exactly...

              Comment

              Working...
              X