[R] 2D plot with colors from 3rd variable?

Tsjerk Wassenaar tsjerkw at gmail.com
Wed Mar 18 09:48:39 CET 2009


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:
> Hi,
>
> Are your variable numerical? If so, you can do what you want by just define
> the color as the variable (col = variable3). Colors in R can be defined by
> numbers, so each number will corresponds to one color.
>
> In example:
>
> plot( iris[, 1], iris[, 2], col = iris[,3])
>
>
> Regards
>
>
>
> 2009/3/18 Josh <tejalonline at gmail.com>
>
>> Hi,
>> I have three related variables (vectors) and would like to see their
>> distribution on a 2D plot of first two variables, having colors
>> proportional
>> to the values from third variable. I could have done so by passing 3rd
>> variable to the color palette, but this would disrupt the relationship
>> information among them.
>> I am sure there has to be some way to do it, but I don't know how. Any help
>> in this direction will be appreciated.
>>
>> thanks
>> jo
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



-- 
Tsjerk A. Wassenaar, Ph.D.
Junior UD (post-doc)
Biomolecular NMR, Bijvoet Center
Utrecht University
Padualaan 8
3584 CH Utrecht
The Netherlands
P: +31-30-2539931
F: +31-30-2537623




More information about the R-help mailing list