On 8/2/07, Sandeman, L. R. <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I have written code to plot an xyplot as follows:
>
> library(lattice)
> xyplot(len~ageJan1|as.factor(cohort),groups=sex,as.table=T,strip=strip.c
> ustom(bg='white',fg='white'),data=dat,
> xlab="Age (January 1st)",ylab="Length (cm)",main="Linear models for male
> and female cod, by cohort",type='p',
> lwd=1.5,auto.key=list(text=c("Male","Female"),points=F,rectangles=F,line
> s=T))
>
> I have fitted a linear model to the same data (for each sex in each
> cohort(year)).  I would like to add the fitted models to the existing
> plot (one line for male and one for female, where each panel is a
> separate cohort - as in the above code....).  I also want to do this
> with non-linear models if possible.
>
> I have trawled R help, and it seems that panel.superpose may be one
> method of attempting this, however, I am unable to produce any working
> code.
>
> My dataset is too large to put in as an example, but it is in the basic
> form as below (where sex is 1 for male and 5 for female).
>
>    len age fitted_model cohort sex
> 1   24   2 30.05771   1977   1
> 2   31   3 36.64122   1977   1
> 3   22   2 27.73938   1978   1
> 4   34   3 36.64122   1977   1
> 5   22   2 27.73938   1978   1
> 6   31   3 36.64122   1977   1
> 7   34   3 36.64122   1977   1
> 8   28   2 27.73938   1978   1
> 9   23   2 27.73938   1978   1
> 10  24   2 27.73938   1978   1
> 11  25   2 27.73938   1978   1
> Etc...


You need to rearrange your data first, so that it looks like

   len      age     cohort sex  source
1   24        2       1977   1   obs
2   31        3       1977   1   obs
3   22        2       1978   1   obs
4   34        3       1977   1   obs
...
1  30.05771   2       1977   1   fitted
2  36.64122   3       1977   1   fitted
3  27.73938   2       1978   1   fitted
4  36.64122   3       1977   1   fitted

There are several ways to do that; options include reshape(),
make.groups(), and the reshape package.

After that, something like

xyplot(len ~ age | as.factor(cohort), data = <...>,
       groups = interaction(sex, source),
       type = c("p", "p", "l", "l"), distribute.type = TRUE)

should give you what you want (this does use panel.superpose
internally).

-Deepayan

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to