[R] Creating ordered colors for ordered factors

David Winsemius dwinsemius at comcast.net
Tue Aug 21 02:22:14 CEST 2012


On Aug 20, 2012, at 5:06 PM, Rui Barradas wrote:

> Hello,
>
> Try the following.
>
> g <- factor(c(rep("blue",10),rep("green",10),rep("red",10)),
>    levels = c("blue", "green", "red"))
> plot(y~x, col=levels(g)[g])
> plot(y~x, col=as.character(g))

It's not necessary to embed the color names in the factor.
>
> Hope this helps,
>
> Rui Barradas
> Em 21-08-2012 00:49, Hans Thompson escreveu:
>> I have been trying to find a way to label my cluster with the  
>> colors I want
>> using factors.  I am not having a good time.  I can make the plot I'm
>> looking for but how can I make it so I specify that my ordered  
>> factors have
>> ordered colors (1= "blue", 2="green", 3="red")?
>>
>> x <- c(rnorm(10,1,1),rnorm(10,10,1),rnorm(10,10,1))
>> y <- c(rnorm(10,10,1),rnorm(10,10,1),rnorm(10,1,1))
>> f<- factor(c(rep(1,10),rep(2,10),rep(3,10)))

# This should work:
>> plot(y~x, col=  c("blue", "green", "red")[f] )

-- 

David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list