Dear useRs,

I have a question regarding panel functions in the lattice package, in 
particular how to customize the plotting colors in xyplot.

I have longitudinal data from several treatments (TRT) with subjects (SUBJ) 
nested in treatment.  Each subject has several time points; the time points are 
nearly balanced across subjects, although not completely.  At each sample 
(SAMP) at each time point (TIME), two replicates (REP) are taken.   Let's call 
the response Y and the whole data.frame DAT.

My goal is to make a trellis plot with each treatment in its own panel, each 
subject having its own color and lines connecting the REPs within SAMP (after 
adding jitter along the TIME axis to distinguish SUBJs).  Let me switch over to 
an explicit example with artificial data.

   set.seed(695442223)

   DAT <- data.frame(TRT=factor(rep(c("Trt1","Trt2"), each=12)),              # 
2 treatments
                     SUBJ=factor(paste('subj', rep(seq(4), each=6), sep='')), # 
4 subjects, 2 w/in each TRT
                     SAMP=factor(rep(seq(12), each=2)),                       # 
4x3 = 12 samples
                     REP=factor(c(1,2)),                                      # 
2 replicates
                     TIME=rep(c(4,4,8,8,12,12), times=4),                     # 
3 arbitrary times
                     Y=round(rep(rgamma(n=12, shape=47500, scale=1/500), 
each=2) +
                              rep(rgamma(n=12, shape=4, scale=1/8), each=2) * 
c(-1,1),
                             2)   # arbitrary Y but very easy to view
   )

   DAT <- transform(DAT,
                       # horizontal jitter
                    plotX=TIME+ifelse(SUBJ %in% c("subj1","subj3"), -1,1)/10,
                       # unique plotting color for each subject
                    COLS=factor(c( "green", "black", "blue", 
"red"))[as.numeric(SUBJ)]
   )

The canonical example is:

xyplot(Y ~ plotX | TRT, data = DAT, groups = SAMP, type="b")

Of course, this connects the REPs within SAMP, since SAMP is the "groups" 
variable.  But each SAMP is now independently colored, recycled from (if I 
understand correctly)  trellis.par.get('superpose.<symbol/line>')$col, instead 
of each SUBJ being independently colored from the COLS variable.

In Section 5.2 in Deepayan Sarkar's "Lattice: Multivariate Data Visualization 
with R" (Springer 2008), the suggestion in Section 5.2 is to use a panel 
function which gets passed an explicit "subscripts" argument, to distinguish 
the groups indexing from the color indexing.  Starting with the book's code to 
plot Figure 5.4, I hacked around to get this:

    xyplot(Y ~ plotX | TRT, data = DAT, groups = SAMP, type="b",
              # the full vector of colors
          cols = DAT[['COLS']],
          panel = function(x, y, ..., cols, groups, subscripts) {
                           colSUBJ <- cols[subscripts]
                           panel.xyplot(x, y, groups=groups, 
subscripts=subscripts,
                                        ..., col.line=colSUBJ, 
col.symbol=colSUBJ)
                          }
         )


Now this gets each treatment TRT with its own color, but not each subject SUBJ. 
 I am indeed not exactly sure what this code does specifically, with the 
subscripts argument.  But nearly every other permutation of arguments I could 
think off either gave a totally wrong plot, or an error.

How do I write a panel function to join reps in the same sample with the groups 
argument, but give each subject a unique color? (an alternate chore is to use 
only unique colors within each treatment, or repeating colors in different 
treatments).  And what is a good pointer or two to good resources explaining 
the use of the subscripts argument in panel functions?  Sarkar's book is 
fantastic but its treatment of this issue is quite thin.

Thanks,
John Szumiloski

John Szumiloski, Ph.D.
Principal Scientist, Statistician
Analytical and Bioanalytical Development
NBR105-1-1411

Bristol-Myers Squibb
P.O. Box 191
1 Squibb Drive
New Brunswick, NJ
08903-0191

(732) 227-7167





________________________________
 This message (including any attachments) may contain co...{{dropped:8}}

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Reply via email to