Skip to content
Prev 174090 / 398503 Next

2D plot with colors from 3rd variable?

Hi Jo,

To have some more flexibility with coloring I use the following, e.g.
with rainbow colors:

col<- rainbow(255,end=5/6)

colid <- function( x, range=NULL, depth=255 )
{
    if ( is.null( range ) )
        y <- as.integer(x-min(x))/(max(x)-min(x))*depth+1
    else
    {
        y <- as.integer(x-range[1])/(range[2]-range[1])*depth+1
        y[ which( y < range[1] ) ] <- 1
        y[ which( y > range[2] ) ] <- depth
    }
    y
}

plot( iris[,1],iris[,2],col=col[ colid(iris[,3]) ] )

Hope it helps,

Tsjerk
On Wed, Mar 18, 2009 at 9:15 AM, Usuario R <r.user.spain at gmail.com> wrote: