[R] color of lines while printing through for loop

Patrick Connolly p_connolly at slingshot.co.nz
Sat Sep 25 00:28:37 CEST 2010


On Fri, 24-Sep-2010 at 06:11PM +0200, Jonas Josefsson wrote:

> 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?

Without knowing what pheno.dt looks like, I can't be sure, but I'd
have thought it would work if you replaced

col = colors[i]
with
col = rainbow(x)[i]


HTH




>
> 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])
>   }
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.   
   ___    Patrick Connolly   
 {~._.~}                   Great minds discuss ideas    
 _( Y )_  	         Average minds discuss events 
(:_~*~_:)                  Small minds discuss people  
 (_)-(_)  	                      ..... Eleanor Roosevelt
	  
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.



More information about the R-help mailing list