Announcement

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

  • Problem sketching hourly time labels on x (or t) axis of a two-way graph

    Hi experts;

    I am trying to sketch a twoway rcap figure showing the start and end time of a few people's performances (horizontally);

    Here are my xvar1 and xvar2 (showing the start and end points of rcap). My y-axis values are also the list of people performing during these time series.

    Start_Time End_Time
    08:30:00 16:00:00
    12:00:00 13:30:00
    12:00:00 13:00:00
    13:30:00 15:15:00
    13:30:00 15:29:59
    14:00:00 15:00:00
    14:00:00 15:00:00
    14:30:00 15:29:59
    14:45:00 15:00:00
    15:00:00 16:59:59
    15:00:00 16:00:00
    15:30:00 16:14:59
    16:00:00 16:45:00
    16:30:00 17:30:00
    17:00:00 18:00:00
    17:00:00 18:00:00
    17:00:00 18:00:00
    17:30:00 18:29:59
    17:30:00 19:30:00
    18:00:00 20:15:00
    18:30:00 19:30:00
    19:00:00 21:00:00
    19:30:00 21:00:00
    20:00:00 21:45:00
    20:30:00 21:40:00
    21:00:00 22:30:00
    21:00:00 21:29:59
    22:00:00 22:59:59
    23:00:00 01:59:59
    23:30:00 01:59:59

    My problem is that I cannot label the x (or t) axis (the horizontal axis) properly. I know to with the numbers (not hours) it is easy to create lables like below:
    xscale (range(0 24))
    xlabel(0 (2) 24) // showing 0 2 4 6 ...... 22 24 on your x-axis

    But here with the time, I don't know how to do it like the numbers.

    Here is what I want:
    08:00:00 09:00:00 10:00:00 ....... 21:00:00 22:00:00 23:00:00 // or even better below
    08:00 am 09:00 am 10:00 am ........ 08:00 pm 09:00 pm 10:00 pm

    The format of Start_Time and End_Time above is %tcHH:MM:SS.

    Can you please help me what I should do to label my figure x-axis like what I described?

    Thanks

  • #2
    Please use dataex in the future to present data examples. You can use the clock() function to access the specific SIF time values, e.g.,

    Code:
    . di clock("00:00:00", "hms")
    0
    
    . di clock("01:00:00", "hms")
    3600000
    
    . di clock("23:59:59", "hms")
    86399000
    Once you have these, you proceed the same way as you would with numerical values, only specifying the format.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float id double(starttime endtime)
     1 30600000 57600000
     2 43200000 48600000
     3 43200000 46800000
     4 48600000 54900000
     5 48600000 55799000
     6 50400000 54000000
     7 50400000 54000000
     8 52200000 55799000
     9 53100000 54000000
    10 54000000 61199000
    11 54000000 57600000
    12 55800000 58499000
    13 57600000 60300000
    14 59400000 63000000
    15 61200000 64800000
    16 61200000 64800000
    17 61200000 64800000
    18 63000000 66599000
    19 63000000 70200000
    20 64800000 72900000
    21 66600000 70200000
    22 68400000 75600000
    23 70200000 75600000
    24 72000000 78300000
    25 73800000 78000000
    26 75600000 81000000
    27 75600000 77399000
    28 79200000 82799000
    29 82800000  7199000
    30 84600000  7199000
    end
    format %tcHH:MM:SS starttime
    format %tcHH:MM:SS endtime
    
    tw (rcap starttime endtime id, xlab("") ylab(3600000(3600000)86399000, ///
    format(%tcHH:MM:SS) angle(0)) scheme(s1color) xtitle("") ytitle(Start/End ))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	55.9 KB
ID:	1508570




    If you want 1:00 am, 2:00 am, etc. on the axis, you will have to use value labels for your time variable. There are plenty of examples in the forum on how to do this if you search.
    Last edited by Andrew Musau; 20 Jul 2019, 03:03.

    Comment


    • #3
      Hi Andrew;

      Thank you. The answer was very helpful and worked with my code. My understanding is that I guess Stata does not understand numbers representing time in a format other than the number of milliseconds from a reference point.

      Thanks
      Alireza

      Comment


      • #4
        Hi everyone,

        In this example how would you make the last 2 bars show the right way when crossing the 00:00 mark? In this example, they span the whole day even if the actual value is 23:30:00 - 01:59:59.
        I have the same problem when plotting the start and end times of someone's sleep.

        Thank you for the help!
        Petru

        Comment


        • #5
          As the clock resets in #2, you need a day variable to identify which day it is. While identifying the day is simple when the data span two days, dealing with data over multiple days may lead to issues if you attempt to infer days in the presence of multiple clock resets.

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float id double(starttime endtime)
           1 30600000 57600000
           2 43200000 48600000
           3 43200000 46800000
           4 48600000 54900000
           5 48600000 55799000
           6 50400000 54000000
           7 50400000 54000000
           8 52200000 55799000
           9 53100000 54000000
          10 54000000 61199000
          11 54000000 57600000
          12 55800000 58499000
          13 57600000 60300000
          14 59400000 63000000
          15 61200000 64800000
          16 61200000 64800000
          17 61200000 64800000
          18 63000000 66599000
          19 63000000 70200000
          20 64800000 72900000
          21 66600000 70200000
          22 68400000 75600000
          23 70200000 75600000
          24 72000000 78300000
          25 73800000 78000000
          26 75600000 81000000
          27 75600000 77399000
          28 79200000 82799000
          29 82800000  7199000
          30 84600000  7199000
          end
          format %tcHH:MM:SS starttime
          format %tcHH:MM:SS endtime
          
          gen double new_endtime= cond(end<start, end+start, end)
          
          tw (rcap starttime new_endtime id, xlab("") ylab(28800000(3600000)92000000, ///
          format(%tcHH:MM:SS) angle(0))  xtitle("") ytitle(Start/End))
          Click image for larger version

Name:	Graph.png
Views:	1
Size:	51.7 KB
ID:	1740102

          Comment


          • #6
            Hi Andrew,

            Thank you so much for the reply. This method worked very well, and with some adaption to the code, in the end I got this:

            Code:
            gen double new_starttime= cond(endtime<starttime, starttime - 3*endtime, starttime)
            I pushed back the start time, which works better for my sleep time from 23:45:00 to 08:00:00.
            The other way would show the bar for this somehow above all the other bars that span only one day.

            Thank you for the help,
            Petru

            Comment

            Working...
            X