[R] Superimposing y-variables in Lattice formulas

Stavros Macrakis macrakis at alum.mit.edu
Fri Nov 14 22:23:41 CET 2008


Given a data frame of a categorical variable and two continuous
variables, I would like to display one continuous variable against the
other for each value of the categorical variable, all superimposed on
the same plot.  For example:

data(Indometh); str(Indometh)
Classes 'nfnGroupedData', 'nfGroupedData', 'groupedData' and
'data.frame':      66 obs. of  3 variables:
 $ Subject: Ord.factor w/ 6 levels "1"<"4"<"2"<"5"<..: 1 1 1 1 1 1 1 1 1 1 ...
 $ time   : num  0.25 0.5 0.75 1 1.25 2 3 4 5 6 ...
 $ conc   : num  1.5 0.94 0.78 0.48 0.37 0.19 0.12 0.11 0.08 0.07 ...
...

I can easily display conc as a function of time with one panel per
value of the categorical variable:

     xyplot ( conc ~ time | Subject , Indometh )

But how do I specify that I want to display all the Subjects on a
single graph, superimposing them all?  I have played with various
combinations of the formula operators ~, *, +, :, and |, but not
gotten anywhere, and I haven't been able to find a clear description
of the semantics of these operators either in the xyplot man page or
in Murrell's R Graphics

What I'm looking for is something like the output of:

I <- Indometh;
for (i in I$Subject)
  { (if (i==1) plot else points)(
       I[ I$Subject==i, c("time","conc")],
       col=i, type="l");
    par(new=TRUE); }
par(new=FALSE);

I recognize this is atrocious programming style, but I'm afraid I
don't know any better; I would appreciate guidance on how to do this
right in traditional graphics, too....

           -s



More information about the R-help mailing list