Announcement

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

  • Nice labels for dates in line graph

    In the below line graph, I would like to control the labels displayed on the x-axis. Could you please tell me how I can force the dates to show only the starting month of each year like 2009m1, 2010m1, 2011m1 and so on with the display format of only four-digit years. Also, is there a way to customize the labels such that the axis displays every second or third year? Thank you!


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float date double(price)
    588 1.426
    589 1.258
    590 1.292
    591 1.396
    592 1.489
    593 1.802
    594 1.695
    595   1.9
    596 1.773
    597 1.941
    598  1.98
    599 1.959
    600 2.035
    601 1.998
    602 2.125
    603 2.267
    604 2.093
    605 2.066
    606 2.042
    607 2.093
    608  2.13
    609 2.252
    610 2.324
    611 2.446
    612 2.601
    613 2.793
    614 3.081
    615 3.231
    616 3.001
    617 3.015
    618 3.117
    619 2.974
    620 2.937
    621  2.96
    622 3.046
    623 2.878
    624 3.034
    625 3.178
    626  3.27
    627 3.217
    628 2.947
    629 2.667
    630 2.879
    631 3.143
    632 3.186
    633 3.157
    634 2.997
    635  2.96
    636 3.044
    637   3.2
    638 3.007
    639 2.879
    640 2.843
    641 2.861
    642 2.981
    643  3.04
    644 3.014
    645 2.935
    646 2.857
    647 2.953
    648 2.913
    649 2.973
    650 2.917
    651 2.932
    652 2.904
    653 2.921
    654 2.842
    655 2.821
    656 2.709
    657   2.5
    658 2.314
    659 1.784
    660 1.531
    661 1.824
    662 1.711
    663 1.776
    664 1.917
    665 1.802
    666 1.624
    667 1.463
    668 1.444
    669 1.418
    670 1.351
    671 1.089
    672  .958
    673  .999
    674 1.127
    675 1.199
    676 1.378
    677  1.46
    678 1.343
    679 1.379
    680   1.4
    681 1.544
    682 1.427
    683 1.596
    end
    format %tm date
    
    nicelabels date, local(datelbl) nvals(10) tight
    mylabels `datelbl', local(mydatelbl)
    
    nicelabels price, local(ylbl) nvals(10) tight
    mylabels `ylbl', prefix("$") local(myylbl)
    
    line price date, ylabel(`myylbl', angle(0)) tlabel(`datelbl')

  • #2
    nicelabels and mylabels are written up at https://journals.sagepub.com/doi/pdf...6867X221141058

    I have a different suggestion based on https://www.stata-journal.com/articl...article=gr0030 The trickery here seems to be undone by an assigned monthly date format, and I don't know why, but that is why I don't work with a variable with that display format.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float date double(price)
    588 1.426
    589 1.258
    590 1.292
    591 1.396
    592 1.489
    593 1.802
    594 1.695
    595   1.9
    596 1.773
    597 1.941
    598  1.98
    599 1.959
    600 2.035
    601 1.998
    602 2.125
    603 2.267
    604 2.093
    605 2.066
    606 2.042
    607 2.093
    608  2.13
    609 2.252
    610 2.324
    611 2.446
    612 2.601
    613 2.793
    614 3.081
    615 3.231
    616 3.001
    617 3.015
    618 3.117
    619 2.974
    620 2.937
    621  2.96
    622 3.046
    623 2.878
    624 3.034
    625 3.178
    626  3.27
    627 3.217
    628 2.947
    629 2.667
    630 2.879
    631 3.143
    632 3.186
    633 3.157
    634 2.997
    635  2.96
    636 3.044
    637   3.2
    638 3.007
    639 2.879
    640 2.843
    641 2.861
    642 2.981
    643  3.04
    644 3.014
    645 2.935
    646 2.857
    647 2.953
    648 2.913
    649 2.973
    650 2.917
    651 2.932
    652 2.904
    653 2.921
    654 2.842
    655 2.821
    656 2.709
    657   2.5
    658 2.314
    659 1.784
    660 1.531
    661 1.824
    662 1.711
    663 1.776
    664 1.917
    665 1.802
    666 1.624
    667 1.463
    668 1.444
    669 1.418
    670 1.351
    671 1.089
    672  .958
    673  .999
    674 1.127
    675 1.199
    676 1.378
    677  1.46
    678 1.343
    679 1.379
    680   1.4
    681 1.544
    682 1.427
    683 1.596
    end
    format %tm date
    
    gen year = yofd(dofm(date))
    su year, meanonly 
    
    forval y = `r(min)'/`r(max)' { 
        local mid = ym(`y', 6) + 0.5 
        local end = ym(`y', 1) - 0.5 
        local xtick `xtick' `end'
        local xla `xla' `mid' "`y'"
    }
    local end = ym(r(max), 12) + 0.5 
    local xtick `xtick' `end'
    
    nicelabels price, local(ylbl) nvals(10) tight
    mylabels `ylbl', prefix("$") local(myylbl)
    
    gen DATE = date 
    
    line price DATE, ylabel(`myylbl', angle(0)) xla(`xla', noticks) xtick(`xtick', tlength(*3)) xtitle("")
    
    
    Click image for larger version

Name:	bettertime.png
Views:	1
Size:	47.1 KB
ID:	1727813

    Comment


    • #3
      Nick Cox, thank you for that suggestion and links to additional reading material. It's really helpful!

      Comment

      Working...
      X