[R] Override col.lines and col.symbol in panel.xyplot with type='b'

From: Szumiloski, John <john_szumiloski_at_merck.com>
Date: Mon, 11 Apr 2011 11:57:38 -0400


Dear useRs,

I have a longitudinal experiment with several treatment groups, ~20 subjects per group, ~6 timepoints and a continuous dependent variable. I have been successfully been using lattice::xyplot with this data. However, I have been stumped with a particular application of it.

I would like to use xyplot on my data, broken into treatment groups with the groups argument, using type='b' to show subjectwise longitudinal data. So far so good, I have done this many times. But now I wish to show the same data but having the color of the lines and symbols overridden in some arbitrary way, yet not without changing anything else about the plot, in particular the structure/topology of the plot from using the groups argument and type='b'.

This requires using a panel function of some sort. I have come to think I will need to use a function with a 'subscripts' argument as the panel function, which then itself calls panel.xyplot() and uses its arguments col.line and col.symbol. The closest example I could find is on page 73 of Sarkar's UseR! Lattice book, where the subscripts argument indexed the data within each grouped subplot, and it was used as an index for a user-generated vector of colors. This seems like what I want to do.

But I could not get this paradigm to work. Here is a simple example using trivial data:

> unlist(R.Version())

                                      platform                                           arch
                             "i386-pc-mingw32"                                         "i386"
                                            os                                         system
                                     "mingw32"                                "i386, mingw32"
                                        status                                          major
                                     "Patched"                                            "2"
                                         minor                                           year
                                        "12.2"                                         "2011"
                                         month                                            day
                                          "03"                                           "18"
                                       svn rev                                       language
                                       "55383"                                            "R"
                                version.string
"R version 2.12.2 Patched (2011-03-18 r55383)"

> search()

[1] ".GlobalEnv"        "package:stats"     "package:graphics"
[4] "package:grDevices" "package:utils"     "package:datasets"
[7] "package:methods"   "Autoloads"         "package:base"

> require(lattice)

Loading required package: lattice

> set.seed(388659262)
> dat <- data.frame(Panel=rep(c('A','B'), each=4),

+                   ID=factor(rep(letters[1:4], each=2)),
+                   X=rep(c(0,1), times=4),
+                   Y=runif(8)
+                  )

##### now for the arbitrary colors. Let's highlight one subject red, the rest black

> dat$Color <- with(dat, ifelse(Panel=='A' & ID == 'a', 2, 1))
> dat

  Panel ID X         Y Color
1     A  a 0 0.1138821     2
2     A  a 1 0.7361403     2
3     A  b 0 0.3304683     1
4     A  b 1 0.5866701     1
5     B  c 0 0.8819857     1
6     B  c 1 0.7329025     1
7     B  d 0 0.5000357     1
8     B  d 1 0.6365438     1

#####  The following standard plot is fine.
#####  Each subject is colored differently,
#####  I believe recycling through the colors from either
#####  trellis.par.get("superpose.symbol")$col or
##### trellis.par.get("superpose.line")$col, ##### but be default they are the same anyway

> xyplot(Y ~ X | Panel, data=dat, groups=ID, type='b',
+ scales=list(x=list(at=c(0,1),labels=c(0,1))))

#####  But for example, this following attempt to grab the
#####  corresponding values of dat$Color do not have
#####  my intended effect.  There are now three "subjects"
#####  plotted per group, each groups' line colors are the same,
#####  and the symbol colors are nearly the same as the line colors....but not exactly!


> xyplot(Y ~ X | Panel, data=dat, groups=ID, type='b',
+ scales=list(x=list(at=c(0,1),labels=c(0,1))), + panel=function(..., groups, subscripts) panel.xyplot(..., col.symbol=dat$Color[subscripts], + col.line=dat$Color[subscripts]) + )

At one point in my efforts I was actually able to get the symbol colors correct, but the line colors were (to me) incomprehensibly wrong. But alas I have not be able to reproduce that to show here.

\begin{ignorant speculation alert}
I suspect that having (in the example) 8 points but only 4 lines causes undesired recycling somewhere. \end{speculation}

Any assistance as to how to properly use the panel functions (or any other approach short of abandoning lattice graphics) to override the coloring would be greatly appreciated.

John

John Szumiloski, Ph.D.
Senior Biometrician
Biometrics Research
WP53B-120
Merck Research Laboratories
P.O. Box 0004
West Point, PA 19486-0004
USA
(215) 652-7346 (PH)
(215) 993-1835 (FAX)
john<dot>szumiloski<at>merck<dot>com



These opinions are my own and do not necessarily reflect that of Merck & Co., Inc.

Notice: This e-mail message, together with any attachme...{{dropped:14}}



R-help_at_r-project.org 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. Received on Mon 11 Apr 2011 - 18:45:07 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 12 Apr 2011 - 07:40:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive