[R] lines with colored segments

Duncan Murdoch murdoch.duncan at gmail.com
Sun Nov 4 14:51:02 CET 2012


On 12-11-04 8:29 AM, David Stevens wrote:
> Hello all
>
> I'm trying to create a plot similar to a plot.default(..., type='b')
> with points plotted connected by lines that leave small gaps between the
> end of the line and the point symbol, BUT, with each line segment's
> color controlled by a category. plot... draws the line color uniformly
> according to the first color in a color sequence, ignoring the
> remainder.  I can use segments() to give the proper colors using the x,y
> data, but those segments don't have the small gaps around the symbols.
> Somewhere, somehow, plot... either only draws the shortened segments, or
> draws the full segment, blanks out the space around the the symbol then
> adds the symbol (or, maybe something more sophisticated). Obviously I'm
> not the first to want to do this. Has anyone addressed this?
>

You might be the first to want to do this.  It's a fairly strange plot: 
  you have a category determined by the pair x[i] and x[i+1].

I think the only way to do this would be to draw the segments in a loop. 
  For example,

x <- 1:10
y <- 1:10
plot(x, y) # draw the points, in black
col <- rainbow(9)
for (i in 1:9) # draw the segments in colour
   lines(x[i:(i+1)], y[i:(i+1)], type='c', col=col[i])

Duncan Murdoch




More information about the R-help mailing list