[R] color of lines while printing through for loop

Jonas Josefsson jonas at runtimerecords.net
Fri Sep 24 18:11:28 CEST 2010


I am trying to find a convenient way to control line colors when 
printing from a for loop using the lines command.
Right now I have solved this by creating a colors vector that is refered 
to in the loop with index. However, the colors choosen here are just 
1,2,3,4,5...

I would like to get colors from the col = rainbow(x) that you can use in 
plot() and set the to be my number of lines (I think I know how to set 
it to my number of lines, but I don't know how to implement this into my 
loop.

Is this possible?

This is my script:

__________________________________________________________________
###creating two emty vectors and opening a graphics window
x <- vector()
y <- vector()
plot(x,y, xlim = c(pheno.dt$year[1],pheno.dt$year[nrow(pheno.dt)]), ylim 
= c(min(pheno.dt$julian, na.rm = TRUE),max(pheno.dt$julian, na.rm = 
TRUE)), xlab = "Year", ylab = "Julian Day")

###setting up colors
sp_pheno.unique <- unique(pheno.dt$sp_pheno)
colors <- seq(1,200,1)

   ####printloop
   for (i in 1:length(unique(pheno.dt$year))) {
       data.sub <- subset(pheno.dt, pheno.dt$sp_pheno==sp_pheno.unique[i])
       x <- seq(pheno.dt$year[1],pheno.dt$year[nrow(pheno.dt)])
       y <- data.sub$julian
       lines(x,y, col = colors[i])
   }



More information about the R-help mailing list