On Wed, Aug 5, 2009 at 1:30 PM, Jacob Wegelin <[email protected]>wrote:
> I would like to use lattice graphics to plot multiple functions (or groups
> or subpopulations) on the same plot region, using different line types "lty"
> or colors "col" to distinguish the functions (or groups).
>
> In traditional graphics, this seems straightforward: First plot all the
> data using 'type="n"', and subsequently execute a series of "points" or
> "lines" commands, one for each different group or function.
>
> What is the elegant way to do this using xyplot?
>
> To make this concrete, consider the following toy example:
>
> k<- 10
> x<- (1:k)/3
> yM<-6 + x^2
> yF<-12 + x^(1.5)
> xNA<-x[length(x)]
>
> # Insertion of NA row is necessary to prevent a meaningless line
> # from being drawn from the females to the males across the entire plot.
>
> DAT<-data.frame(
> x=c(x, xNA, x)
> ,
> y=c(yF, NA, yM)
> ,
> sex=c( rep(0, k ), 0, rep(1, k))
> )
>
> library("lattice")
>
Solution (easier than I had imagined):
myPanel<-function( x, y ) {
panel.xyplot(x, y, type="n")
llines( x[DAT$sex==0], y[DAT$sex==0], col="red", lty=1 )
llines( x[DAT$sex==1], y[DAT$sex==1], col="blue", lty=2 )
}
xyplot(
y ~ x
, data=DAT
, type="l"
, panel=myPanel
)
Jacob A. Wegelin
Assistant Professor
Department of Biostatistics
Virginia Commonwealth University
730 East Broad Street Room 3006
P. O. Box 980032
Richmond VA 23298-0032
U.S.A.
E-mail: [email protected]
URL: http://www.people.vcu.edu/~jwegelin
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.