Announcement

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

  • Adding vertical lines to line graph

    Hi all,

    This is my first post here so I hope I’m doing this right. I’m having problems adding a vertical line to a line plot, using either twoway or tsline, I always get the same error. First some context:

    - I have a variable date, with the format %tcDD/NN/CCYY (e.g. 24/03/2021). I have observations for a year.
    - I want to plot a variable that represents heart rate variability (format %10.0g, e.g. the value “109”)
    - I want to add a vertical line to represent an event occurring at a given date. I know these dates beforehand, so I simply want to be able to add a line for date on the graph, for example, on the 16th of October, 2021.

    This is what I do:

    Code:
    twoway (line heart_rate_variability date)
    (this works and produces this graph)
    Click image for larger version

Name:	Graph1.jpg
Views:	1
Size:	38.3 KB
ID:	1663311


    Then I try adding the line, using

    Code:
    twoway (line heart_rate_variability date), xline(16/10/2021)
    And I get the error:

    invalid line argument, 16/10/2021
    r(198);

    The same if I use the variation:

    Code:
    twoway (line heart_rate_variability date), xline(tc(16/10/2021))
    Alternatively, I tsset date and do:

    Code:
    tsline heart_rate_variability, tline(16/10/2021)
    And I get the error:
    invalid number, (16/10/2021)
    r(198);

    I hope I’m giving enough information, otherwise, apologies, and please let me know how I can make myself clearer. I’m sure this is a small technical thing I’m failing to see. I have read help, the manual, and other bits and pieces but I'm really stuck with this. Any help is highly appreciated!
    Thanks.

    Sergio

  • #2
    The actual dates are integers, held as SIF values. For a convenience function, see

    Code:
    help td()

    This will suggest something in the lines of

    Code:
    tsline heart_rate_variability, tline(`=td(16oct2021)')
    ADDED IN EDIT: Actually, the following example suggests that your code should work.

    Code:
    sysuse tsline2
    tsset day
    tsline calories, tline(28/11/2002)

    Provide a data example to figure out why it does not, e.g., by copying and pasting the result of


    Code:
    dataex heart_rate_variability date
    Last edited by Andrew Musau; 06 May 2022, 07:28.

    Comment


    • #3
      Thank you Andrew for your prompt response.

      I tried

      Code:
       
       tsline heart_rate_variability, tline(`=td(16oct2021)')
      and this works, in the sense that I do not get any error message and the command executes, but I do not see the result, the graph that I get doesn't have any vertical reference lines. I actually had this problem before too, whereas in one of the many options that I tried, I used SIF values and got no error, but a graph with no line either. The second code you share (with the tsline2 example dataset) works perfectly. Here is an example of the data:
      Code:
       * Example generated by -dataex-. For more info, type help dataex clear input int heart_rate_variability double date  90 1.9321632e+12 142 1.9322496e+12 117  1.932336e+12 144 1.9324224e+12 139 1.9325088e+12 127 1.9325952e+12 124 1.9326816e+12 136  1.932768e+12 129 1.9328544e+12 109 1.9329408e+12 132 1.9330272e+12 141 1.9331136e+12 104    1.9332e+12 132 1.9332864e+12 127 1.9333728e+12 119 1.9334592e+12 126 1.9335456e+12 112  1.933632e+12 135 1.9337184e+12 120 1.9338048e+12 134 1.9338912e+12 122 1.9339776e+12 122  1.934064e+12 145 1.9341504e+12 111 1.9342368e+12 138 1.9343232e+12 105 1.9344096e+12 133  1.934496e+12 134 1.9345824e+12 126 1.9346688e+12 103 1.9347552e+12 141 1.9348416e+12 118  1.934928e+12 109 1.9350144e+12  95 1.9351008e+12 147 1.9351872e+12 138 1.9352736e+12 135   1.93536e+12 142 1.9354464e+12 124 1.9355328e+12 144 1.9356192e+12 100 1.9357056e+12 127  1.935792e+12 137 1.9358784e+12 119 1.9359648e+12 126 1.9360512e+12 121 1.9361376e+12 114  1.936224e+12 130 1.9363104e+12 119 1.9363968e+12 110 1.9364832e+12 128 1.9365696e+12 140  1.936656e+12 126 1.9367424e+12 101 1.9368288e+12 132 1.9369152e+12 130 1.9370016e+12 125  1.937088e+12 103 1.9371744e+12 112 1.9372608e+12 131 1.9373472e+12 128 1.9374336e+12 110   1.93752e+12 141 1.9376064e+12 129 1.9376928e+12 106 1.9377792e+12 114 1.9378656e+12 159  1.937952e+12 110 1.9380384e+12 129 1.9381248e+12 125 1.9382112e+12 153 1.9382976e+12 142  1.938384e+12 130 1.9384704e+12 148 1.9385568e+12 134 1.9386432e+12 143 1.9387296e+12 142  1.938816e+12 112 1.9389024e+12 116 1.9389888e+12 148 1.9390752e+12 131 1.9391616e+12 140  1.939248e+12 135 1.9393344e+12 139 1.9394208e+12 108 1.9395072e+12 137 1.9395936e+12 115   1.93968e+12 126 1.9397664e+12 116 1.9398528e+12 141 1.9399392e+12 120 1.9400256e+12 110  1.940112e+12 114 1.9401984e+12 140 1.9402848e+12 107 1.9403712e+12 155 1.9404576e+12 137  1.940544e+12 108 1.9406304e+12 133 1.9407168e+12 end format %tcDD/NN/CCYY date
      Thanks again

      Comment


      • #4
        Thanks for the data example. Actually, you have a time variable and not a date variable. Use the -dofc()- function to extract the date from the time variable and then all should be good.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input int heart_rate_variability double date  
        90 1.9321632e+12
        142 1.9322496e+12
        117  1.932336e+12
        144 1.9324224e+12
        139 1.9325088e+12
        127 1.9325952e+12
        124 1.9326816e+12
        136  1.932768e+12
        129 1.9328544e+12
        109 1.9329408e+12
        132 1.9330272e+12
        141 1.9331136e+12
        104 1.9332e+12
        132 1.9332864e+12
        127 1.9333728e+12
        119 1.9334592e+12
        126 1.9335456e+12
        112  1.933632e+12
        135 1.9337184e+12
        120 1.9338048e+12
        134 1.9338912e+12
        122 1.9339776e+12
        122 1.934064e+12
        145 1.9341504e+12
        111 1.9342368e+12
        138 1.9343232e+12
        105 1.9344096e+12
        133  1.934496e+12
        end
        
        gen Date= dofc(date)
        format Date %tdDD/NN/CCYY
        tsset Date
        tsline heart_rate_variability, tline(10/04/2021) scheme(s1color) xtitle("")
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	39.4 KB
ID:	1663349

        Last edited by Andrew Musau; 06 May 2022, 10:50.

        Comment


        • #5
          Hi Andrew,

          Thank you so much for following up to my question, apologies for my delayed response. You are right, it was a matter of the format on the time (or date) variable. I also realized that I got the same results if I just spell out the full time format:

          Code:
          tline(`=tc(21apr2021 14:30)')
          Thanks again!

          Sergio

          Comment


          • #6
            All the date-time values in the data example don't carry time of day detail. If that's generally true, you lose nothing by working with the equivalent daily date.

            Comment

            Working...
            X