[R] adding to Trellis plots

Deepayan Sarkar deepayan at stat.wisc.edu
Thu Aug 14 06:49:16 CEST 2003


On Wednesday 13 August 2003 07:43, Brunschwig, Hadassa {PDMM~Basel} wrote:
> Hello everyone
>
> I have data grouped by subjects and i used the trellis plot to show the
> curve for each subject. I have another vector "a" which gives me the
> highest points the curves for each subjects can achieve ( the curve is
> exponential ). Now i want to draw a horizontal line, whose values are saved
> in "a", on each plot for each subject. Is there any possibility to do that?
> Obviously i should use something like panel.abline, the problem is how i
> can inform R that each value in "a" is a horizontal line for each plot.

Some more clarification on your data structure would have helped. Is your 
variable "a" a part of the data frame (with the same value repeated for all 
rows corresponding to each subject), or is it a separate, shorter vector, 
with length equal to the number of subjects ?

If the former, then you should use subscripts, as others have already 
suggested, perhaps as:

xyplot(y ~ x | subject,  data = foo,
       panel = function(x, y, subscripts, ...) {
           panel.xyplot(x, y, ...)
           panel.abline(h=foo$a[subscripts][1], ...)
       })

In the second case, you could do something similar to determine (inside the 
panel function) which element of "a" you need to use (assuming the grouping 
variable "subject" is a factor):

           panel.abline(h = a[as.numeric(foo$subject)[subscripts][1]], ...)

(assuming that the grouping variable "subject" is a factor)

Alternatively, you could probably also get away with

xyplot(y ~ x | subject, data = foo,
       panel = function(x, y, panel.number, ...) {
           panel.xyplot(x, y, ...)
           panel.abline(h=a[panel.number], ...)
       })

Deepayan




More information about the R-help mailing list