Announcement

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

  • Doubt with daily dates and x-axis

    Greetings,

    I am working with a database and I have two doubts:

    1) is there any way that in the x-axis I can only show the year (leaving not visible the month and days). Some time Nick helped me to modify the axis, but I have tried that way and the axis does not look good at all. I used tlabel(, format(%dM/CY)), but as I don't know the STATA number I can't manipulate the axis with the dates.

    I would like to do something like what I show you in the attached image

    2) the base that I imported already had the dates and STATA recognized it well, but I would like to clone the variable where I have the date and from there pass it to the pure format of STATA and with it I can work in the part of the axes (for example 1/2/2012 -> 18994).

    Thanks for your attention colleagues!!!

    Code:
    clear
    input int fecha double tdc
    18994 13.9342
    18995 13.6882
    18996 13.7144
    18997 13.7409
    18998 13.7228
    19001 13.7437
    19002 13.6204
    19003 13.6592
    19004 13.5982
    19005 13.6263
    19008 13.5401
    19009 13.4621
    19010 13.3267
    19011 13.2516
    19012  13.224
    19015 13.1383
    19016 13.1712
    19017 13.1319
    19018 12.9325
    19019 12.9504
    19022 13.0063
    19023 13.0077
    19024   12.89
    19025 12.8038
    19026  12.712
    19030 12.6472
    19031 12.6833
    19032   12.72
    19033 12.7857
    19036 12.7392
    19037 12.7753
    19038 12.7658
    19039 12.8822
    19040 12.7856
    19043 12.6793
    19044 12.7408
    19045 12.8296
    19046 12.8294
    19047 12.8675
    19050 12.8779
    19051 12.8575
    19052 12.7891
    19053 12.7764
    19054 12.7723
    19057 12.8339
    19058 12.9777
    19059 12.9469
    19060  12.784
    19061 12.6299
    19064  12.715
    19065 12.6336
    19066 12.6719
    19067 12.6591
    19068 12.6645
    19072 12.6935
    19073 12.6833
    19074  12.833
    19075 12.8035
    19078 12.6795
    19079 12.6706
    19080 12.8039
    19081 12.8489
    19082 12.8093
    19085 12.7691
    19086 12.7317
    19087 12.8155
    19092 12.9902
    19093 13.1568
    19094 13.1324
    19095 13.0736
    19096 13.1458
    19099 13.2168
    19100 13.0816
    19101 13.1322
    19102 13.2288
    19103 13.1171
    19106 13.2087
    19107 13.1392
    19108 13.1667
    19109 13.2093
    19110 13.0141
    19113 12.9942
    19115 12.9629
    19116 12.9725
    19117 13.1344
    19120 13.1659
    19121 13.3374
    19122  13.515
    19123  13.447
    19124 13.4929
    19127 13.6739
    19128 13.7302
    19129 13.7747
    19130 13.7994
    19131 13.7809
    19134 13.7935
    19135 13.7203
    19136 14.0335
    19137  14.004
    19138 14.0156
    end
    format %tdnn/dd/CCYY fecha
    
    tsset fecha
    
    tsline tdc
    Attached Files

  • #2
    Juan is alluding to

    https://www.statalist.org/forums/for...is-double-time

    where the problem was axis labels for quarterly data.

    Note also his thread

    https://www.statalist.org/forums/for...g-ticks-x-axis

    where it's the same problem for monthly data.

    The point is simple but was aired systematically in https://www.stata-journal.com/articl...rticle=gr0030: with a moderately (or extremely) long time series, axis labels for every date would usually be a (very) bad idea, and indeed for say quarterly or monthly data, the preferred labels would usually start (and possibly stop) with showing years.

    The question in #1 is about daily dates and the sample graph in #1 shows year labels for 2012/2023. So one approach, consistent with the 2007 paper just cited. is to put ticks at each 1 January (31 December if you really prefer) and labels without ticks at each 1 July (or any date close to that).

    mylabels and myticks from the Stata Journal can help here. Here is a silly example to make the point, code first and then a copy of results.

    Code:
    clear 
    set obs 11 
    gen date = mdy(7, 1, 2011 + _n)
    gen foo = 42 
    format date %td 
    
    gen year = year(date)
    su year, meanonly 
    
    mylabels 2012/2023, myscale(mdy(7, 1, @)) local(labels)
    myticks 2012/2023, myscale(mdy(1,1,@)) local(ticks)
    
    line foo date, xla(`labels', noticks) xtic(`ticks', tlength(*3)) xtitle("")
    Code:
    . clear 
    
    . set obs 11 
    Number of observations (_N) was 0, now 11.
    
    . gen date = mdy(7, 1, 2011 + _n)
    
    . gen foo = 42 
    
    . format date %td 
    
    . 
    . gen year = year(date)
    
    . su year, meanonly 
    
    . 
    . mylabels 2012/2023, myscale(mdy(7, 1, @)) local(labels)
    19175 "2012" 19540 "2013" 19905 "2014" 20270 "2015" 20636 "2016" 21001 "2017" 21366 "2018" 21731
    "2019" 22097 "2020" 22462 "2021" 22827 "2022" 23192 "2023"
    
    . myticks 2012/2023, myscale(mdy(1,1,@)) local(ticks)
    18993 19359 19724 20089 20454 20820 21185 21550 21915 22281 22646 23011
    
    . 
    . line foo date, xla(`labels', noticks) xtic(`ticks', tlength(*3)) xtitle("")
    Click image for larger version

Name:	yearintervals.png
Views:	1
Size:	12.7 KB
ID:	1703743



    The idea is that mylabels and myticks write the code you need -- and insist that it is assigned to local macros so that you don't need to type it out again (or even copy and paste). You may want to tune the ticks and labels with other standard suboptions, as also surveyed in https://journals.sagepub.com/doi/pdf...36867X19874264

    After a gestation lasting 19 years. those commands were finally written up in Stata Journal 22(4) and the paper is visible at https://journals.sagepub.com/doi/10....6867X221141058

    Comment


    • #3
      Nick, you are a genius.

      That's just what I wanted to do. Thanks so much for the recommendations. I think I still have a ways to go (I will never finish learning about this) to read a lot more on the subject.

      Thank you very much.

      Greetings!!!

      Comment

      Working...
      X