Announcement

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

  • Time of day labels in tempogram

    Hi there,
    I am trying to plot a tempogram, where on the X-axis I have the time of the day (24hr) and on the Y-axis I have the proportion of individuals commuting.
    I have formatted the time variable as a %tc+HH+MM and set the data to be a time-series.
    I am struggling to label the X-axis correctly though.
    Working example:
    Code:
    clear all
    input str40 times float Y
    
    "0:00" 0.05
    "8:00" 0.5
    "13:00" 0.2
    "17:00" 0.7
    "22:00" 0.1
    end
    
    ge time = clock(times, "hm")
    format time %tc+HH+MM
    tsset time, format(%tc+HH+MM) 
    
    tsline Y
    Will produce this plot for which I don't like the labels:
    Click image for larger version

Name:	tempogram1.png
Views:	3
Size:	43.8 KB
ID:	1576130
    whereas
    Code:
    tsline Y , tlabel(1300)
    will produce the wrong label, implying that I am misspecifying it
    Click image for larger version

Name:	tempogram2.png
Views:	1
Size:	39.8 KB
ID:	1576131
    Other attempts such as
    Code:
    tsline Y , tlabel(13:00)
    Code:
    tsline Y , tlabel(01jan1960 13:00:00)
    Code:
    tsline Y , tlabel(01jan1960_13:00:00)
    fail miserably with invalid label specifier, error messages issued by Stata.
    I have browsed the help for tlabel but cannot find the right example.
    Any idea?
    Attached Files

  • #2
    Here's one solution using labmask (in Stata type ssc install labmask).
    Note labmask is from SSC and is written by Nick Cox.


    Code:
    gen t = _n 
    labmask t, val(times)
    twoway line Y t, xlab(, valuelabel) xtitle("")

    Comment


    • #3
      @Justin Blasongame Thanks. Your solution works, but it is a workaround.
      Specifying a specific label also requires the programmer to remember the actual value of the variable, rather than the assigned label.
      For instance, to specify the 13:00 label I need to recall that 13:00 corresponds to t=3.
      Code:
      twoway line Y t, xlab(3, valuelabel)
      which is annoying if there are many values.
      I would guess that there is a correct way to specify the tlabel given the formatting of the variable.
      But your solution is clearly better than what I have so far!

      Comment


      • #4
        Your problem is that you are being bitten by Stata's working in milliseconds, despite your display format.


        What's up with 0533 and so forth?

        Code:
        . di clock("05:33", "hm")
        19980000
        
        . di clock("05:34", "hm")
        20040000
        Answer: it's, very nearly, 20 million milliseconds after midnight. Well, understood now, but clearly not what you want.

        I think you need to develop your own system for graphics here. It's not difficult.

        Step 1: Create a numeric variable with units hours.

        Step 2: Define your own format -- but through value labels.

        Step 3: Ignore tsset and %tc as they aren't helping.


        Code:
        clear all
        input str40 times float Y
        "0:00" 0.05
        "8:00" 0.5
        "13:00" 0.2
        "17:00" 0.7
        "22:00" 0.1
        end
        
        gen times2 = subinstr(times, ":", " ", .) 
        gen htime = real(word(times2, 1)) + real(word(times2, 2)) / 60 
        
        forval t = 0/23 { 
            label def htime `t' "`t':00", add 
        }
        label def htime 24 "0:00", add 
        
        label val htime htime 
        
        line Y htime, xla(0(6)24, valuelabel) xtitle("")
        Code:
        
        


        This trickery will need to be supplemented if you want show (e.g.) "12:30" as well.




        Comment


        • #5
          Nick Cox, regarding step#3, I would argue that the tlabel() option affords you flexibility. #1 (OP): As the manual advises, time variables should always be stored as double.

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input str40 times float(Y)
          "0:00"  .05        
          "8:00"   .5 
          "13:00"  .2 
          "17:00"  .7 
          "22:00"  .1 
          end
          format %tc+HH+MM time
          
          gen double time= clock(times, "hm" )
          tsset time
          tsline Y, tlabel(0 (`=clock("06:00", "hm")') `=clock("23:59", "hm")', format(%tc+HH+:+MM))
          Click image for larger version

Name:	Graph.png
Views:	1
Size:	18.7 KB
ID:	1576168

          Comment


          • #6
            Thanks @Nick Cox and @Andrew Musau, both solutions clearly work.
            However, in my case I prefer Andrew's solution because it's more direct and does not require the value labels workaround.
            thanks again for your time, I was getting quite frustrated with this.

            Comment


            • #7
              Just an extra detail: If you do not like leading zeros, then change the format to

              Code:
              %tc+hH+:+MM

              Example:

              Code:
               di %tc+hH+:+MM `=clock("08:59", "hm")'
               8:59

              Comment


              • #8
                The statement "you need to develop your own system for graphics here" in #4 is just too strong.

                But I've seen people puzzled by, and struggling with, the implication that all kinds of time of day data must be handled in milliseconds. We know why milliseconds are supported: many users really need them. But many other users just have hours and minutes.

                I have fielded questions like why is my time showing up as a number in trillions? How do I draw a line at 8:12 on the graph to show when the landslide started? It's true that devices like those Andrew Musau showed make the latter easier than might be feared.

                By the way value labels aren't essential to the approach in #4. It is perfectly possible to go


                Code:
                xla(0 "0:00" 6 "6:00" 12 "12:00") 
                and so forth.

                Comment

                Working...
                X