title: Plot time series using different colours based on factor
body: <p>I'd like to plot a single line which is multi-coloured, and the colours are based on the corresponding value in a factor. For instance, a time series of daily stock close price, where the days it has gone up by more than a certain amount are in blue, and the days it has gone done by a lot are in red, and the other days it is in boring black.</p>
  
  <p>My data is in an <code>xts</code> object (with the factor put in there with <code>as.numeric(myfactor)</code>), and I'd like to be using the quantmod <code>chartSeries</code> or <code>chart_Series</code> functions. But if that is impossible then something using <code>plot</code> would be enough.</p>
  
  <p>Some sample data:</p>
  
  <pre><code>library(xts)
  x = xts( data.frame( v=(rnorm(50)+10)*10, type=floor(runif(50)*4) ),
    order.by=as.Date("2001-01-01")+1:50)
  </code></pre>
  
  <p>And I can plot it like this:</p>
  
  <pre><code>library(quantmod)
  chartSeries(x$v)
  addTA(x$type, type='p')
  </code></pre>
  
  <p>Which looks like this:
  <a href="https://i.stack.imgur.com/N0gWB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/N0gWB.png" alt="plot using chartSeries"></a></p>
  
  <p>I.e. I felt it'd be easier to match the information in the bottom chart with the top one if coloured line segments were being used.</p>