Hi!

As Ben Bolker told you already, the levels are alphabetically ordered by default. When you print rfactor, the last line shows you the different levels, in the saved order.
rfactor
[1] c c c d b a b d d a a e e b b e c e e a a b b b a b a e a a b d b b c a b b
[39] d c a e c d e d a a a a
Levels: a b c d e                   ## here

If you want to change the order manually, do this:
rfactor=as.factor(sample(letters[1:5], 50, replace=T))
rfactor
[1] e e a a e a a a b a a d a b a b b c d c b e b e d d c a a e a b c e b c b e
[39] a e e d b e c b e d a e   ## different values because of sample()
Levels: a b c d e
rfactor2 <- factor(rfactor, levels=c("e","d","c","b","a"))
rfactor2
[1] e e a a e a a a b a a d a b a b b c d c b e b e d d c a a e a b c e b c b e [39] a e e d b e c b e d a e ## but the values here are the same as for my rfactor Levels: e d c b a ## note that the order here is the one you typed in your call to factor(, levels=)

HTH,
Ivan


Le 12/4/2010 02:04, Rainer Schuermann a écrit :
It seems to be dependent upon the the character ("assigned alphabetically") which I found 
out by manually changing the order of appearance of the characters in rfactor; the colour would 
stick to the character "a", whether this appears first in rfactor or not.

I as able to control the colours being used by
boxplot(x~rfactor, col=c("2", "6", "3","5","4"))
Rgds,
Rainer

Disclaimer:
I'm new to R and very early on the learning curve - just trying my best...


On Saturday 04 December 2010 01:22:56 Eduardo Klein wrote:
Hi,

I would like to know how R assigns the numeric code to a set of factors
in a vector. For example, I have a vector of 5 different factors in a
random order, and I want a color-coded plot by factors:

rfactor=as.factor(sample(letters[1:5], 50, replace=T))
rfactor
   [1] c c c d b a b d d a a e e b b e c e e a a b b b a b a e a a b d b
b c a b b
[39] d c a e c d e d a a a a
Levels: a b c d e

x=rnorm(50)
boxplot(x~rfactor, col=1:5)

So, colors 1 to 5 are assigned to factors alphabetically (a-1, b-2, ...,
e-5), or by order of appearance (c-1, d-2, b-3, a-4, e-5)? It is
possible to control that?

Saludos, EKS


______________________________________________
[email protected] 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.


--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
[email protected]

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php

______________________________________________
[email protected] 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.

Reply via email to