# [R] Plotting multiple curves with lattice graphs

From: Karen Chiswell <karenc2204_at_yahoo.com>
Date: Thu 05 Apr 2007 - 13:50:39 GMT

I would like to plot multiple curves (parametric density curves) in one plot.

For example:
# parameters for three normal density curves
parms =
data.frame(ID=c(1,2,3),mu=c(50,55,60),sigma=c(10,12,15))

but I would like to use lattice graphics (even though I only want one panel) because I want to save the resulting plot in an object.

I could create a dataframe that contains the values I want to plot, and then plot using xyplot(). For example,

x = seq(from=0, to=150, length=100)
plot.df = data.frame(ID=rep(parms\$ID,each=100), x=rep(x,3),mu=rep(parms\$mu,each=100),sigma=rep(parms\$sigma,each=100)) plot.df\$d =
dnorm(plot.df\$x,mean=plot.df\$mu,sd=plot.df\$sigma)

require(lattice)
p=xyplot(d~x, data=plot.df, groups=ID, type="l")

However... is there a more elegant/efficient way to do this? I have tried using the panel.curve panel function (with xyplot() and histogram()), but I am not very experienced with using panel functions (or R actually), and I haven't had any success with this yet.

Many thanks for any suggestions,
Karen

Karen Chiswell
Department of Statistics
North Carolina State University