[R] color for points

Vincent Goulet vincent.goulet at act.ulaval.ca
Sun Oct 9 17:45:33 CEST 2005


Le 8 Octobre 2005 05:35, vincent at 7d4.com a écrit :
> Sam R. Smith a écrit :
> > Hi,
> > I have the following code to randomly generate the points:
> > csr <-function(n=60){
> > x=runif(n)
> > y=runif(n)
> > f=cbind(x,y)
> > }
> > plot(csr())
> >
> > I wonder how to code to make the first twenty points to be BLUE; second
> > twenty points to be RED; the last twenty points to be GREEN?
>
> mynewfct = function(n=60)
> {
> x=runif(n)
> y=runif(n)
> f=cbind(x,y)
> plot(f[1:20] , col='blue');
> par(new=T);
> plot(f[21:40] , col='red');
> par(new=T);
> plot(f[41:60] , col='green');
> }
>
> hih

This function will plot 60 points, no matter the value of argument 'n'. 
Building also on Jim Holtman's comment, may I rather suggest

csr <- function(n=60)
{
	x <- cbind(runif(n), runif(n))
	plot(x, col=rep(c("blue", "red", "green"), each=20, length.out=n))
	x   # why not return the values...
}

HTH

-- 
  Vincent Goulet, Professeur agrégé
  École d'actuariat
  Université Laval, Québec 
  Vincent.Goulet at act.ulaval.ca   http://vgoulet.act.ulaval.ca




More information about the R-help mailing list