# [R] Lattice plot questions

Paul, David A paulda at BATTELLE.ORG
Fri Aug 29 20:10:31 CEST 2003

```Thank you VERY much!  That does address my questions.

Respectfully,
david paul

-----Original Message-----
From: Deepayan Sarkar [mailto:deepayan at stat.wisc.edu]
Sent: Friday, August 29, 2003 1:54 PM
To: Paul, David A; 'r-help at stat.math.ethz.ch'
Subject: Re: [R] Lattice plot questions

Let's consider this simulated data:

foo <- data.frame(resp = do.call("c", lapply(as.list(rep(6, 12)),
function(x) sort(rnorm(x)))),
week = rep(2*0:5, 12),
id = factor(rep(1:12, each = 6)))

Does the following give you what you want ?

xyplot(resp ~ week | id, foo,

panel = function(x, y, col = sup.sym\$col, panel.number, ...) {

## this part accesses the data frame directly, and needs to
## be adjusted accordingly if the name of the variables
## therein change. This draws the part meant for the whole
## data set

panel.superpose(x = foo\$week, y = foo\$resp,
subscripts = TRUE, groups = foo\$id,
type = 'l', col = "black")

## this part draws the colored lines, each overwriting one
## of the background lines

sup.sym <- trellis.par.get("superpose.symbol")
col.this <- col[1 + ((panel.number - 1) %% length(col))]
panel.xyplot(x, y, type = 'b', col = col.this,
lwd = 2, cex = 1.2, ...)

})

This will by default use the colors in superpose.symbol, but you can
override
that by specifying your own color as an argument to xyplot, perhaps
something
like

xyplot(resp ~ week | id, foo, col = c("red", "yellow", "blue"),
panel = ....

I personally don't see the point of using different colors though (that
would
seem useful to me only if the different colors appeared within the same
panel).

There's no restriction on the length of the elements of
trellis.par.get("superpose.symbol"). 7 (which is just as good a number as
any
other) is the default choice, inspired by Trellis in S. Set it to whatever
you want with trellis.par.set() or lset() (or just supply the colors to
xyplot(), which is usually more convenient for specific tasks like this).

Names like "navy blue" are just a convenience, and I doubt that all 256^3
colors in the RGB space have been named, so trying to get a name for
"#ff7f00" may not get you anywhere. R has a function col2rgb() that converts

colors to RGB, e.g.

> col2rgb("#ff7f00")
[,1]
red    255
green  127
blue     0
> col2rgb("navyblue")
[,1]
red      0
green    0
blue   128

If you are more comfortable with named colors, use them--- R accepts them
everywhere. A list of names it recognizes is produced by

> colors()

HTH,

Deepayan

On Friday 29 August 2003 11:14, Paul, David A wrote:
> Win2k, R1.7.1:
>
> I am currently working with some growth curve data from a
> biotoxicology experiment.  Each of 12 subjects had their blood drawn
> at 0, 2, 4, 6, 8, and 10 weeks.  For the purposes of the project, it
> would be helpful if I were able to do the following:
>
> 	a.  Produce 12 panels, each displaying the *same* data, with
> 		the "strip" at the top of a particular panel showing
> 		exactly one of the subject id's
> 	b.  For a particular panel, plot the time course data for
> 		the subject whose id appears in the strip in some
> 		really obvious color (like yellow, red, or blue),
> 		with circles at each time point, and connecting the
> 		"dots".  In the same panel, those subjects whose id's
> 		did NOT appear in the strip would ideally have their
> 		data plotted in black, possibly with no symbols at
> 		any of the time points, but still connecting the
> 		"dots".
>
> I apologize if the description of what is needed is vague.
> It is possible to address (a) by duplicating the data set twelve times
> and cbind()ing a factor variable so that each of the duplicated data
> sets is associated with exactly one of the subject id's.  I have done
> this, and the code
>
> subjects <- foo\$subject.id
> superpose.symbol <- trellis.par.get("superpose.symbol")
> xyplot(log.response ~ week | factor.variable, data = foo,
> 	panel = panel.superpose,
> 	groups = subjects,
> 	key = list(space = "top", columns = 6, transparent = TRUE,
> 			text = list(levels(subjects)),
> 			points = Rows(superpose.symbol,1:7))
>        )
>
> works, with "factor.variable" corresponding to the cbind()ed factor
> variable.  Unfortunately, I have been unable to figure out how to do
> (b).  I would also like to determine a more memory efficient way of
> doing (a) -- even though my current data set is small (only 10 weeks
> of data per subject), that will change.
>
> Also, an examination of "superpose.symbol" reveals the
>
> following:
> > superpose.symbol <- trellis.par.get("superpose.symbol")
> > superpose.symbol
>
> \$cex
> [1] 0.8 0.8 0.8 0.8 0.8 0.8 0.8
>
> \$col
> [1] "#00ffff" "#ff00ff" "#00ff00" "#ff7f00" "#007eff" "#ffff00"
> "#ff0000"
>
> \$font
> [1] 1 1 1 1 1 1 1
>
> \$pch
> [1] "o" "o" "o" "o" "o" "o" "o"
>
>
> This seems to indicate that lattice graphics are restricted to no more
> than seven colors, though I cannot believe that this is true. Where
> and how to denote them?  (I find "#ff7f00" to be a little cryptic, for
> example.)  Is there a table that relates these ?hexadecimal? numbers
> to descriptions like "navy blue" for Win2k?
>
> Finally, is it possible to cause superpose.symbol to default to more
> than 7 \$cex/\$col/\$font/\$pch values?  Since I am working with 12
> subjects, plots that assign a different color to each wind up
> duplicating colors and symbols at some point.
>
>