[R] Discriminating between experiments with xyplot (lattice)

Deepayan Sarkar deepayan.sarkar at gmail.com
Fri Mar 16 22:47:36 CET 2007


On 3/16/07, Christel u. Frank Sarfert <cfsarfert at web.de> wrote:
> Hi,
>
> suppose I have data from 3 experiments which show conversion as a function of
> time for different boundary conditions, e.g. pressure, temperature. I want to
> plot all experiments as conversion over time grouped according to the
> temperature. However, since I have more than one experiment performed at the
> same temperature (but different pressures) I end up figuring out which curve
> belongs to which experiment. (An example with artificial data of the
> structure I use is given below. It shows three experiments where two
> experiments at temp = 250 but press = 1 and press = 0.5 are plotted within
> one group.)
> My question is: Is there a way to identify which curve whithin the same group
> belongs to which experiment, e.g by plotting a label like the experiment
> number to the end point, or by choosing different symbols for the different
> experiments - while keeping the color encoding of the groups?
>
> Your help is greatly appreciated!
>
> Best regards
>                       Frank
>
>
> require(lattice)
>
> ## generating the data, I need
> time <- seq(0,50,1)
> conversion <- 2 * log(time + 1)
> press <- rep(1,51)
> temp <- rep(250,51)
> experiment <- rep(1, 51)
> v1 = as.data.frame(cbind(experiment,time,conversion,press,temp))
>
> conversion <- 2.5 * log(time + 1)
> press <- rep(1, 51)
> temp <- rep(270,51)
> experiment <- rep(2, 51)
> v2 = as.data.frame(cbind(experiment,time,conversion,press,temp))
>
> conversion <- 1.25 * log(time + 1)
> press <- rep(0.5, 51)
> temp <- rep(250,51)
> experiment <- rep(3, 51)
> v3 = as.data.frame(cbind(experiment,time,conversion,press,temp))
>
> d <- rbind(v1,v2,v3)

You want to use make.groups rather than rbind here, so that you retain
information on which experiment each row is coming from:

> dd <- make.groups(v1,v2,v3)
> str(dd)
'data.frame':	153 obs. of  6 variables:
 $ experiment: num  1 1 1 1 1 1 1 1 1 1 ...
 $ time      : num  0 1 2 3 4 5 6 7 8 9 ...
 $ conversion: num  0.00 1.39 2.20 2.77 3.22 ...
 $ press     : num  1 1 1 1 1 1 1 1 1 1 ...
 $ temp      : num  250 250 250 250 250 250 250 250 250 250 ...
 $ which     : Factor w/ 3 levels "v1","v2","v3": 1 1 1 1 1 1 1 1 1 1 ...

Now how you choose to plot this is up to you. One simple possibility
is to create a new factor encoding both experiment and temperature,
e.g.,

xyplot(conversion ~ time, data = dd,
          groups = (which:factor(temp))[,drop=TRUE],
          auto.key = T)

Another is to condition on one, e.g.,

xyplot(conversion ~ time | factor(temp), data = dd, groups = which,
auto.key = T)

It is possible to use one one variable for color and another for
plotting character, but the code involved would be somewhat less
elegant (mostly because the support functions are not already built
in). Let me know if you really want that; I can come up with some
sample code.

-Deepayan



More information about the R-help mailing list