Announcement

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

  • Time series graph labels--remove clutter

    Hi folks,

    This is a bit hard to explain, but I have weekly time series data that I am graphing. I would like each data point to mention the month that it occurs. But, I would like to then have a second line of labels by year. I only want the year to appear once for each time it occurs. For instance, 2012 should be centered below all the 2012 data.

    I found an example in excel. Check out the second graph on this page. That is similar to what I want to do. http://chandoo.org/wp/2010/11/17/sho...ars-in-charts/

    Any help would be great here. I am attaching some sample data.

    Thanks,
    Steve

    sales week_date
    21579.45 01oct2011
    22339.82 08oct2011
    23991.69 15oct2011
    23501.75 22oct2011
    10376.31 29oct2011
    40477.81 19nov2011
    34751.51 26nov2011
    55628.31 10dec2011
    21243.51 11feb2012
    26773.37 10mar2012
    26007.25 31mar2012
    34790.88 07apr2012
    35097.36 14apr2012
    35098.74 21apr2012
    28945.33 28apr2012
    32681.25 05may2012
    37340.03 12may2012
    31319 19may2012
    34123.23 26may2012
    32072.16 02jun2012
    33841.12 09jun2012
    37077.03 16jun2012


  • #2
    This only requires recourse to documented options. You can use xmla() for a extra series of labels. You just need to tweak away from their defaults.

    Code:
    clear
    input sales str9 s_week_date
    21579.45 01oct2011
    22339.82 08oct2011
    23991.69 15oct2011
    23501.75 22oct2011
    10376.31 29oct2011
    40477.81 19nov2011
    34751.51 26nov2011
    55628.31 10dec2011
    21243.51 11feb2012
    26773.37 10mar2012
    26007.25 31mar2012
    34790.88 07apr2012
    35097.36 14apr2012
    35098.74 21apr2012
    28945.33 28apr2012
    32681.25 05may2012
    37340.03 12may2012
    31319 19may2012
    34123.23 26may2012
    32072.16 02jun2012
    33841.12 09jun2012
    37077.03 16jun2012
    end
    
    gen week_date = date(s_week_date, "DMY")
    
    l
    
         +---------------------------------+
         |    sales   s_week_~e   week_d~e |
         |---------------------------------|
      1. | 21579.45   01oct2011      18901 |
      2. | 22339.82   08oct2011      18908 |
      3. | 23991.69   15oct2011      18915 |
      4. | 23501.75   22oct2011      18922 |
      5. | 10376.31   29oct2011      18929 |
         |---------------------------------|
      6. | 40477.81   19nov2011      18950 |
      7. | 34751.51   26nov2011      18957 |
      8. | 55628.31   10dec2011      18971 |
      9. | 21243.51   11feb2012      19034 |
     10. | 26773.37   10mar2012      19062 |
         |---------------------------------|
     11. | 26007.25   31mar2012      19083 |
     12. | 34790.88   07apr2012      19090 |
     13. | 35097.36   14apr2012      19097 |
     14. | 35098.74   21apr2012      19104 |
     15. | 28945.33   28apr2012      19111 |
         |---------------------------------|
     16. | 32681.25   05may2012      19118 |
     17. | 37340.03   12may2012      19125 |
     18. |    31319   19may2012      19132 |
     19. | 34123.23   26may2012      19139 |
     20. | 32072.16   02jun2012      19146 |
         |---------------------------------|
     21. | 33841.12   09jun2012      19153 |
     22. | 37077.03   16jun2012      19160 |
         +---------------------------------+
    
    
    twoway connected sales week_date, xla(18901(21)19160, format("%tdd/N")) ///
    xmla(18950 "2011"  19100 "2012", tlength(*10) tlcolor(none) labsize(medium) labcolor(red)) ///
    xli(`=mdy(1,1,2012)', lc(red) lw(vvthin)) scheme(s1color) xtitle("")
    The rest is experiment, experiment, experiment.

    1. Here three-weekly steps (daily dates 21 days apart) seem as close as can be tolerated. Your preferred format for those dates will almost certainly differ.

    2. The years can be minor labels but label size must be made bigger than default. To push them away from the axis we need long ticks, except naturally that the ticks are better made invisible.

    3. A separation into years is perhaps simplest by vertical line(s).

    4. With a date axis the axis title is often better suppressed.
    Click image for larger version

Name:	timelabels.png
Views:	1
Size:	14.0 KB
ID:	1294753




    See also

    SJ-7-4 gr0030 . Stata tip 55: Better axis labeling for time points/intervals
    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
    Q4/07 SJ 7(4):590--592 (no commands)
    tip on labeling graphs for time points and time intervals

    http://www.stata-journal.com/sjpdf.h...iclenum=gr0030
    Last edited by Nick Cox; 18 May 2015, 18:23.

    Comment


    • #3
      Thanks so much. I did try to look through the ticks documentation, but didn't really understand that this was minor ticks I was looking for. I find I work much better with seeing some result examples with code, and then figuring out what it is that works for my particular problem. Anyhow, I really do appreciate your time!

      Comment


      • #4
        I think we all agree: one good example is worth any amount of exegesis.

        Good, but note that you are using minor labels here, not minor ticks. Minor ticks could be useful for extra long ticks at year ends.

        Comment


        • #5
          Okay, thanks for making the distinction. I think that was a point of confusion, that I was some how labeling a minor tick. Makes sense now. Thanks.

          Comment


          • #6
            Indeed. For others watching yet bemused:

            The tick options such as xtick(), xmtick() define ticks which are unadorned (no labels) and useless unless visible.

            Conversely the label options such as xlabel(), xmlabel() imply associated ticks, although there can be good reasons to make the ticks invisible by removing them, tuning their length to zero, or setting their colour to be invisible.

            Comment

            Working...
            X