Christel u. Frank Sarfert
2007-Mar-16 21:39 UTC
[R] Discriminating between experiments with xyplot (lattice)
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)
## plotting
xyplot(conversion ~ time, data = d, groups = factor(temp), auto.key = T)
## result: the group for temp = 250 includes two experiments, which cannot be
associated to the individual experiments
--
C. u. F. Sarfert
Hauffstr. 8
70825 Korntal
Deepayan Sarkar
2007-Mar-16 21:47 UTC
[R] Discriminating between experiments with xyplot (lattice)
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
Deepayan Sarkar
2007-Mar-17 01:21 UTC
[R] Discriminating between experiments with xyplot (lattice)
Here's one possibility:
d <- make.groups(v1,v2,v3)
## create interaction dropping unused levels
d$intg <- with(d, which:factor(temp))[, drop=TRUE]
## extract experiment and temp information from levels
intg.expt <- sapply(strsplit(levels(d$intg), ":", fixed = TRUE),
"[[", 1)
intg.temp <- sapply(strsplit(levels(d$intg), ":", fixed = TRUE),
"[[", 2)
## find a suitable color vector (where colors are repeated when the
## temperature is)
temp.unique <- unique(intg.temp)
col.temp <-
rep(trellis.par.get("superpose.symbol")$col,
length = length(temp.unique))
col.intg <- col.temp[match(intg.temp, temp.unique)]
xyplot(conversion ~ time, data = d, groups = intg,
intg.expt = intg.expt,
panel = panel.superpose,
panel.groups = function(x, y, ..., group.number, intg.expt) {
panel.xyplot(x, y, ...)
panel.text(tail(x, 1), tail(y, 1), intg.expt[group.number], pos = 4)
},
col = col.intg,
key = list(text = list(temp.unique), points = list(col col.temp, pch =
1)))
Hope that helps,
Deepayan
On 3/16/07, Frank Sarfert <CFSarfert at web.de>
wrote:> Hi Deepayan,
>
> many thanks for your quick reply.
> I actually cannot condition whith the experiment number, because in my real
life data, I would like to condition with pressure and I have many more
experiments (not just 3). So ideally, I would have a plot with a label near the
end point of the curve which says which experiment it comes from. So, if you
could really write some code - that'd be great! Many thanks in advance!
>
> Best regards
> Frank
>
>
> -----Urspr?ngliche Nachricht-----
> Von: "Deepayan Sarkar" <deepayan.sarkar at gmail.com>
> Gesendet: 16.03.07 22:47:47
> An: "Christel u. Frank Sarfert" <cfsarfert at web.de>
> CC: r-help at stat.math.ethz.ch
> Betreff: Re: [R] Discriminating between experiments with xyplot (lattice)
>
>
> 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
>
>
>
> _____________________________________________________________________
> Der WEB.DE SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
> http://smartsurfer.web.de/?mc=100071&distributionid=000000000066
>
>