[R] Creating ordered colors for ordered factors

Rui Barradas ruipbarradas at sapo.pt
Tue Aug 21 02:06:18 CEST 2012


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

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)))
> plot(y~x, col=f)
>
> Thanks
>
> Hans
>
> 	[[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.




More information about the R-help mailing list