Skip to content

lines with colored segments

3 messages · David Stevens, Duncan Murdoch, Jim Lemon

#
On 12-11-04 8:29 AM, David Stevens wrote:
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
#
On 11/05/2012 12:29 AM, David Stevens wrote:
Hi David,
Try this:

library(plotrix)
x<-c(0,cumsum(rnorm(99)))
y<-c(0,cumsum(rnorm(99)))
xydist<-sqrt(x*x+y*y)
plot(x,y,main="Random walk plot",xlab="X",ylab="Y",type="n")
color.scale.lines(x,y,col=2+(diff(xydist)>0))
boxed.labels(x,y,labels=1:100,border=FALSE,cex=0.5)

This colors a random walk with green if the "step" advances away from 
the start and red if it regresses toward the start. Just pass your 
colors in the "col=" argument.

Jim