Judith Flores
2008-Aug-08 22:36 UTC
[R] How to add 'specific length' segments to lattice plots
Hello, I would like to add a horizontal line to each individual panel panel, the length of the segment is given by a vector. If you run the following lines (a very simplified version of my script and data): group<-c('A','A','A','A','B','B','B','B','C','C','C','C') value<-c(89,35,58,33,45,79,66,73,35,37,12,39) day<-c(0,10,0,10,0,9,0,9,0,5,0,5) index<-c(2,2,2,2,4,4,4,4,7,7,7,7) subgroup<-c('g1','g1','g2','g2','g3','g3','g2','g2','g4','g4','g2','g2') data<-data.frame(group,value,day,index, subgroup) library('lattice') xyplot(data$value~data$day|data$group, groups=data$subgroup, panel=function(x,y,...) { panel.xyplot(x,y, type='b',...) } ) What I would like to add to each panel is a horizontal line that, always starting at 0 for the value of the first argument of the function panel.segments, and the length of the segment will be defined by a value of the vector data$index. For example: panel.segments(x[x==0],y[y==20],x[data$index],y[y==20]) This means that 3 of the arguments will be constants, only the third argument is variable. So I tried something like this: xyplot(data$value~data$day|data$group, groups=data$subgroup, panel=function(x,y,k=data$index, subscripts,...) { panel.xyplot(x,y, type='b',...) panel.segments(0,20,k[subscripts], 20) } ) But I obtain and error message letting me know that argument 'subscripts' is missing. Could some one explain to me what I am doing wrong? And how can I obtain a horizontal line per panel? Thank you, as always, for your help and guidance, Judith