Announcement

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

  • Adding variable reference lines to twoway graphs

    Hello,

    I am trying to add vertical reference bars to a series of graphs plotted using xtline. I have a time-dependent variable, time_var, plotted versus day for individuals identified by id. I have been able to plot these in separate panels for each id using the following code:

    xtline time_var, t(day) i(id)

    I would like to add a vertical reference bar to each panel that is different for each id so that I can mark the time when an event occurs, event_day. For example, based on the data below, there should be a vertical line at x = 2 for id#2 and at x = 1 for id#3, but no vertical line for id#1.

    I'm able to generate fixed reference lines that appear in all panels by using xline(), but I haven't been able to figure out how to set the placement of these lines equal to event_day. (xline(event_day) does not work...)

    Should I reshape the data back to wide? Or is there a better command for what I'm trying to do?

    Thank you!



    id time_var day event_day
    1 5 1 .
    1 6 2 .
    2 34 1 2
    2 50 2 2
    2 60 3 2
    3 2 1 1
    3 2 2 1

  • #2
    To the extent that such a reference bar is only for visual purposes i.e. just serves to show when an event occurs, I would use the addplot option with the plottype dropline. To ensure that the line covers the entire length of the graph, create a variable that is equal to the maximum value of your variable of interest (in this case max time_var=60)

    Code:
    input id time_var day event_day
    1 5 1 .
    1 6 2 . 
    2 34 1 2
    2 50 2 2
    2 60 3 2
    3 2 1 1
    3 2 2 1
    end
    
    xtset id day
    gen max=60
     xtline time_var, addplot( dropline max event_day)


    Code:
    Click image for larger version

Name:	xtline.png
Views:	1
Size:	10.2 KB
ID:	1347939

    Comment

    Working...
    X