title: Using a color vector in a multipanel xyplot in r
body: <p>Given 4 vectors of equal length (x,y,z,q) where q is a character vector with color names used as a grouping structure, 
  I run the xyplot command:</p>
  
  <pre><code>xyplot(y~x|factor(z),pch=19,data=mydata,col=mydata$q)
  </code></pre>
  
  <p>Everything works fine, except that past the first factor level of z, the plots are not respecting the order of color vector q and points are randomly assigned colors from the color vector q. If I call vector q, I see that color names are correctly ordered for every level of z.  </p>
  
  <p>As a clear example:
  a row in the data frame may contain the following values for (x,y,z,q): (13,25,march,"blue")</p>
  
  <p>For the "march" plot in the multipanel xyplot, this data point will appear but as a different color, albeit a color from vector q.</p>
  
  <p>I have a similar problem with using a vector with a grouping structure for cex; the first factor level produces the correct plot, but the other factor levels do not change according to the vector witha grouping structure.</p>
  
  <p>If this question is not clear I will happily modify it until it makes sense. I'm sure other people are having this problem.</p>
  
  <pre><code> str(cohort)
  
  
  
   $ date                 : Date, format: "2012-03-05" "2012-03-05" ...
   $ area                 : int  1111 3053 3555 3556 4228 6447 1111 ...
   $ score             : num  0.2282 0.1498 0.1823 0.0995 0.3083 ...
   $ color                 : chr  "purple" "blue" "brown" "violet" ...
   $ cex3                  : num  0.753 0.84 1.067 0.875 0.753 ...
   $ pch3                  : num  7 19 19 19 19 8 7 19 19 19 ...
  $ time_spent            : int  0 0 0 0 0 0 1 1 1 1 ...
  </code></pre>
  
  <p>working command:</p>
  
  <pre><code>xyplot(score~time_spent|factor(date),groups=area,data=cohort,ylim=c(0,.6),panel=function(x,y,subscripts){panel.xyplot(x=x,y=y,subscripts=subscripts,pch=cohort[subscripts,"pch3"],col=cohort[subscripts,"color"],col.line="black",cex=cohort[subscripts,"cex3"])})
  </code></pre>
  
  <p>The actual grouping vector I want text for in a legend is cohort$area...
  the character vector cohort$color and the vector cohort$pch3 are used as grouping structures to visually represent cohort$area in the plots...
  thus, i want a color code/symbol legend for cohort$color and cohort$pch3 but text labels from cohort$area.</p>
  
  <p>Example legend: purple dot-"1111"
                  blue dot-"3053" etc.</p>
  
  <p>Came up with working solution:</p>
  
  <pre><code> xyplot(score ~ time_spent | factor(date), groups = area, data = cohort,auto.key=list(columns=10,points=TRUE,title="area"),
      par.settings = list(superpose.symbol = list(col = sort(unique(cohort$color)),pch=sort(unique(cohort$pch3)))),
      panel = function(x, y, subscripts = subscripts, groups = groups) {
      panel.xyplot(x = x, y = y,
      subscripts = subscripts, groups = groups,col.line="black",type=c("p","g"))})
  </code></pre>