On Wed, 5 Aug 2009, Deepayan Sarkar wrote:

> On 8/5/09, Jacob Wegelin <jacob.wegelin@gmail.com> 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))
>>  )
>
> It's much simpler in lattice, and you don't need to play such tricks.
Option 1:
>
> xyplot(yM + yF ~ x, type = "l", auto.key = list(points = FALSE, lines =
TRUE))
>
> and if you want to control lty etc:
>
> xyplot(yM + yF ~ x, type = "l", auto.key = list(points = FALSE, lines =
TRUE),
>       par.settings = simpleTheme(lty = c(2, 3)))
>
>
> Option 2 (a bit more work, but less mysterious under the hood):
>
> DAT<-
>    data.frame(x = c(x, x), y=c(yF, yM),
>               sex= rep(c("Female", "Male"), each = length(x)))
>
> xyplot(y ~ x, data = DAT, groups = sex, type = "l")

Dear Bill and Deepayan,

Thanks. This is helpful. Where can one find a thorough documentation of all
these features like par.settings, simpleTheme, the options for where to
place the legend or "key", auto.key, the different locations besides "top"
where one can place the "auto.key", etc.?  I don't think this is all clearly
laid out in the R help files or latticeLab.pdf. But using your hints I found
that the following worked:


xyplot(
y ~ x
, groups= ~ sex
, type="l"
, auto.key = list(columns=2, points = FALSE, lines = TRUE)
, par.settings = simpleTheme(lty = c(1, 2), col="black")
, data=DAT
)

Now, how would I use lattice tools to plot males with a line and females
with points--and still get an informative autokey?

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: jwegelin@vcu.edu
URL: http://www.people.vcu.edu/~jwegelin

	[[alternative HTML version deleted]]


