Zacarias
Zacarias

Reputation: 25

xyplot() in R, spaghetti plot, how to make a tick at a certain point for each individual

Good day,

I am using xyplot() to create a spaghetti plot... for each subject_identifier it plots the score of Y over time.

xyplot(Y~time,groups=subject_identifier,data=c,type=c('l'))

An event happens for each subject_identifier at some specified time, event_time. How can I make a tick or a dot on each line at event_time for each individual?

Thanks

Upvotes: 0

Views: 1679

Answers (1)

IRTFM
IRTFM

Reputation: 263411

If the event_times are a vector: event_times <- c(5,3,6,8) and you expect the groups to line up in order with those indices then you can use groups as an index into event_times which will then be indices for x and y in the panel arguments:

xyplot(Y~time,groups=subject_identifier,data=cdat,
              panel=function(x,y,groups,...){
                panel.xyplot(x,y,groups=groups,...)
                panel.segments(x0=x[(grp-1)*10+event_times[grp]],
                               x1=x[(grp-1)*10+event_times[grp]], 
                               y0=y[(grp-1)*10+event_times[grp]]-.2,
                             y1=y[(grp-1)*10+event_times[grp]]+.2, groups=groups,...)},
       type=c('l'))

So the first group gets ticked at time=5, the second at time=3 , and so on. Need to offset by (group-1)*10 although there may be cleaner approaches with panel.superpose. Other options for ticks are of course possible but you have been rather vague in your problem presentation.

enter image description here

Upvotes: 1

Related Questions