[R] Multiple lines with a different color assigned to each line (corrected code)

Jim Lemon jim at bitwrit.com.au
Wed Feb 27 10:35:55 CET 2008


Judith Flores wrote:
> Sorry, I just realized I didn't type in the correct
> names of the variables I am working with, this is how
> it should be:
> 
> plot(1,1,type="n")
> for (i in summ$tx) {
> 
>    points(summ$timep[summ$tx==i],summ$mn[summ$tx==i])
>    lines(summ$timep[summ$tx==i],summ$mn[summ$tx==i])
> }
> 
Hi Judith,

I think this might help:

plot(1,1,type="n")
# define your colors here
# you can generate the vector in many ways
ncolors<-length(unique(summ$tx))
colorvector<-rainbow(ncolors)
colorindex<-1
for(i in summ$tx) {
  points(summ$timep[summ$tx==1],summ$mn[summ$tx==i],
   type="b",col=colorvector[colorindex])
  colorindex<-colorindex+1
}

This may also answer the query from Valentin Bellassen.

Jim



More information about the R-help mailing list