Announcement

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

  • xlabel acts unexpectedly when date are used on x-axis

    Dear Statalisters,

    Why is it that xlabel(#) fails to produce the expected result when date appears on the x-axis?

    Consider the minimal example:

    clear
    use http://www.stata-press.com/data/r13/sp500

    scatter high low // by default, 6 labels on x-axis
    scatter high low, xlabel(#4) // after customization, 4 label on x-axis

    line open date // by default 5 label on x-axis, with "2002" on the right-hand side appearing outside the bounds of the plot and being cutoff
    line open date, xlabel(#4) // same result as without the xlabel option

    I tried "tsset date", prior to the "line" command, but it did not help.

    Suggestion to fix this basic problem would be appreciated. I would prefer a solution that does not require custom entry of dates in xlabel every time I want to make a simple plot like the one above.

    Thank you!



  • #2
    A solution:

    tsset date, daily
    tsline open // same as "line open date"
    tsline open, tlabel(#3) // responsive, but does not eliminate the problem of the cutoff "2002" on the right-hand side
    tsline open, tlabel(01mar2001 01jul2001 01nov2001, format(%tdMon-CCYY)) // better

    Looks like I'll have to customize the selection of tlabel every time I do not want an ugly looking plot with date on the x-axis...

    If you know of a better solution, please share!

    Comment


    • #3
      Hi, Maxime,

      Perhaps an interesting solution would be the specification of the values you want to be displayed on the x axis.

      For example: xlabel( 1992 1995 1998 2004).

      Also to be noticed, and according to the manual (http://www.stata.com/manuals13/g-3ax...el_options.pdf),

      A command such as xlabel(##)

      was not taken too seriously
      In the example from the manual, when performing xlabel(#10), the results presented just 8 labels.

      Hopefully it helps.

      Best,

      Marcos
      Best regards,

      Marcos

      Comment


      • #4
        The problem apparently is that the lengtk of the data labels is not taken into account. The most handy tool probably is this:
        Code:
        line open date , plotregion(margin(l=10 r=10))

        Comment


        • #5
          I like both suggestions. Playing with plotregion is a good hack. Customization by inputting dates in tlabel or doing something similar through xlabel is more effort, but the final result is probably the most desirable.

          Most plot commands probably accept the tlabel option after the data has been tsset, such as:

          clear
          use http://www.stata-press.com/data/r13/sp500
          tsset date, daily
          line open date, tlabel(01mar2001 01jul2001 01nov2001, format(%tdMon-CCYY)) ,

          otherwise, for commands that do not accept tlabel, but accept xlabel, one would first have to figure out the desired input in xlabel,

          di d(1mar2001)
          * 15035
          di d(01jul2001)
          * 15157
          di d(01nov2001)
          * 15280

          then use them as input in xlabel:

          line open date, xlabel(15035 15157 15280, format(%tdMon-CCYY)).

          Comment


          • #6
            Something like this works too

            Code:
            line open date, xlabel(`=d(1mar2001)' `=d(1jul2001)', format(%tdMon-CCYY))
            See help macro. The syntax is that Stata will evaluate an expression and substitute the result on the fly. You don't need the intermediary of a local macro.

            See also Scott Merryman's mydays within mylabels (SSC).

            Last edited by Nick Cox; 24 Feb 2015, 10:33.

            Comment


            • #7
              Thanks Nick, more elegant solutions are always appreciated!

              Comment

              Working...
              X