[R] plot is not keeping the order of variable

Dieter Menne dieter.menne at menne-biomed.de
Mon Feb 22 08:30:55 CET 2010



Peter Dalgaard wrote:
> 
> d <- data.frame(f=c("rare", "medium","well-done"))
> #To get the cast in order of appearance, this can be used:
> 
>  > d$f <- factor(d$f, levels=unique(d$f))
>  > d$f
> [1] rare      medium    well-done
> Levels: rare medium well-done
> 
> 

.. which caused some head-scratching from me, because it made me believe
there was some hidden Kopenhagen-factor conserving the original order. 

To protect the innocent: for the more general case, unique() does not help.

Dieter

levs = c("rare", "medium","well-done") 
set.seed(4711)
d <- data.frame(f=sample(levs,10,TRUE)) 
unique(d$f)
d$f =  factor(d$f, levels=unique(d$f)) 
levels(d$f)
d$f =  factor(d$f, levels = levs) 
levels(d$f)

-- 
View this message in context: http://n4.nabble.com/plot-is-not-keeping-the-order-of-variable-tp1563553p1564087.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list