title: Why does coloring by factor fail when using a subset of a data frame?
body: <p>I have been searching for an answer to this question for a few days, haven't found the answer, and thought that it was time to come here and post a question myself.  I have a data frame that I read in using <code>read.csv()</code> with the <code>as.is=TRUE</code> flag.</p>
  
  <pre><code>x$night &lt;- as.Date(x$night)
  x$gsub_version &lt;- as.numeric(x$gsub_version)
  x$gsub_version &lt;- x$gsub_version + 1
  head(x)
  night      filter      mag     mag_err gsub_version
  2013-10-08      B 17.67273 0.003226886            1
  2013-10-08      B 17.80228 0.003165991            2
  </code></pre>
  
  <p>To save space, I clipped the results.  I want to plot the data with <code>filter=='V'</code> and <code>col=factor(x$gsub_version)</code>.  </p>
  
  <pre><code>plot(mag ~ night, data=x[x$filter=="V",], ylim=c(21,17), col=factor(x$gsub_version))
  </code></pre>
  
  <p>Unfortunately this produces this <a href="http://i.stack.imgur.com/E2OwH.png" rel="nofollow">plot</a> with incorrect colors after a certain point.  When I include the extra step to create a dedicated data frame for the <code>filter</code> variable, the plot looks correct.</p>
  
  <pre><code>y &lt;- x[x$filter=="V",]
  plot(mag ~ night, data=y, ylim=c(21,17), col=factor(y$gsub_version))
  </code></pre>
  
  <p>And here is the <a href="http://i.stack.imgur.com/9ICVu.png" rel="nofollow">new plot</a> that is produced.  The new plot correctly uses the values of <code>gsub_version</code>.  My question is twofold: What makes these two methods different considering that y is comprised of the same subset used to make the first plot, and why is the first method failing?</p>
  
  <p>Thank you for taking the time to read this.</p>