[R] xyplot (lattice): colours of lines

Deepayan Sarkar deepayan at stat.wisc.edu
Fri Feb 6 20:46:48 CET 2004


[I'm CC-ing this to r-help since this is an interesting problem]

I think I finally understand what you mean. You want a different line for each 
subject-treatment combination, but you want the lines to be color-coded only 
on the basis of treatment. Is that correct ?

When you create the new factor 

A$patient <- factor(paste(as.character(A$subject), 
                    as.character(A$treatment), sep ="/"))

it has 134 levels:

> levels(A$patient)
  [1] "100/1"  "10/1"   "101/2"  "102/1"  "103/2"  "104/2"  "105/1"  "11/2"  
  [9] "113/1"  "114/2"  "115/1"  "116/2"  "117/1"  "118/2"  "119/1"  "12/1"  
 [17] "121/2"  "129/1"  "131/2"  "13/2"   "133/2"  "134/1"  "135/1"  "136/2" 
 [25] "139/2"  "140/2"  "141/1"  "144/2"  "145/2"  "146/1"  "148/2"  "149/2" 
 [33] "150/1"  "151/1"  "15/2"   "152/2"  "155/1"  "156/2"  "157/1"  "158/2" 
 [41] "159/1"  "16/1"   "166/2"  "167/1"  "17/1"   "171/1"  "172/2"  "173/1" 
 [49] "174/2"  "175/1"  "176/2"  "179/2"  "180/1"  "181/1"  "18/2"   "182/2" 
 [57] "183/2"  "184/1"  "185/2"  "186/1"  "189/2"  "190/1"  "19/1"   "20/2"  
 [65] "207/1"  "21/1"   "211/2"  "212/1"  "213/2"  "214/1"  "215/2"  "220/1" 
 [73] "221/2"  "22/2"   "222/2"  "224/1"  "225/1"  "227/2"  "24/1"   "26/1"  
 [81] "32/1"   "34/2"   "36/1"   "37/2"   "38/1"   "42/1"   "43/2"   "47/2"  
 [89] "50/1"   "5115/1" "534/2"  "535/1"  "536/2"  "55/2"   "56/1"   "57/2"  
 [97] "58/1"   "587/2"  "60/2"   "61/2"   "62/1"   "63/2"   "64/1"   "65/1"  
[105] "66/2"   "67/1"   "68/2"   "69/1"   "70/2"   "7/1"    "71/1"   "72/1"  
[113] "73/1"   "77/1"   "78/2"   "80/2"   "81/2"   "8/2"    "83/2"   "84/1"  
[121] "85/1"   "86/1"   "87/2"   "88/2"   "89/2"   "90/1"   "91/1"   "9/2"   
[129] "92/2"   "93/1"   "94/2"   "95/2"   "97/1"   "98/2"  

You want to have distinct lines for each of these, but you want the line color 
to be red or green depending only on whether the last digit is 1 or 2. 

One way to do this would be:


plev <- levels(A$patient)
plev <- strsplit(plev, split = "/", fixed = TRUE)
plev <- as.numeric(do.call("rbind", plev)[,2])
col.groups <- c("red", "green")[plev]

xyplot(X ~ time | center, data=A, type="l", 
       panel=panel.superpose, groups=patient,
       col = col.groups, 
       xlab="TIME", ylab="X")


I hope that's what you are looking for.

Deepayan



On Friday 06 February 2004 12:40, you wrote:
> there is still something wrong:
> if you check the plot the two colours do not correspond to treatment
> assignments. For instance,
> center 35 has 2 green lines instead of 1 red and 1 green
> center 21 has 3 red lines and 1 green instead of 2 red and 2 green
> center 41 has 3 downsloping red lines instead of only two
> I am sorry If i was not clear earlier, that is probably for my bad english
> many many thanks, for all this help you are giving me
> Umberto
>
>
> From: Deepayan Sarkar <deepayan at stat.wisc.edu>
>
> >To: "Umberto Maggiore" <umberto_maggiore at hotmail.com>
> >Subject: Re: [R] xyplot (lattice): colours of lines
> >Date: Fri, 6 Feb 2004 12:13:19 -0600
> >
> >
> >That's not consistent with what you asked for earlier.
> >
> >If you need only patients as a grouping factor, just use
> >
> >xyplot(<...>, panel = panel.superpose,
> >        groups = patients, col = c("red", "green"))
> >
> >Deepayan
> >
> >On Friday 06 February 2004 11:37, you wrote:
> > > The problem is that I want only two colours, say green for patients on
> > > treatment=1
> > > and red for patients on treatment=2
> > > Umberto
> > >
> > >
> > > From: Deepayan Sarkar <deepayan at stat.wisc.edu>
> > >
> > > >To: "Umberto Maggiore" <umberto_maggiore at hotmail.com>
> > > >Subject: Re: [R] xyplot (lattice): colours of lines
> > > >Date: Fri, 6 Feb 2004 10:48:33 -0600
> > > >
> > > >On Thursday 05 February 2004 13:52, Umberto Maggiore wrote:
> > > > > It works. However, now I get another odd result: in some plots
> > > > > there are straight lines connecting the end of a line with the
> > > > > beginning of another one.
> > > >
> > > >I don't see the problem (see attached pdf for what I get). Do you get
> > > >something different ? If so, what versions of R and lattice are you
> > > >using ?
> > > >
> > > >Note that A$patients now has 134 levels, with only 7 colors to be
> > > > recycled, so
> > > >you will get repititions.
> > > >
> > > >Deepayan




More information about the R-help mailing list