plot is not keeping the order of variable

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

plot is not keeping the order of variable

orduek
Hi,
I created a simple data frame with one factor and one numerical variable.
The factor was actually a vector of names of techniques to trimm reaction
time data.
I want to create a plot that shows the value of F test for every trimming
method.
So the data frame has its trim factor (who has those labels
"mean","2500","2000","1500","1000","log","inverse","1SD","2SD")
and the numerical variable of the data frame has the F values for each one
of those trimming method.
When I ask R to plot it, it doesn't keep the order of the trimm verctor
correctly and the plot confuses the order so the most left one will be 1500
and then 1SD etc.
The values are correct but it is important for me to keep it in the same
order I built it.
How can I do it?
Thank you very much,
Or D.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: plot is not keeping the order of variable

Ista Zahn
Hi Or,
I can't know for sure what your problem is without an example, but the
first thing I would do is check to make sure that your labels are
stored as an ordered factor (and that the order is correct). See
?factor for details.

Best,
Ista

On Sun, Feb 21, 2010 at 10:57 AM, Or Duek <[hidden email]> wrote:

> Hi,
> I created a simple data frame with one factor and one numerical variable.
> The factor was actually a vector of names of techniques to trimm reaction
> time data.
> I want to create a plot that shows the value of F test for every trimming
> method.
> So the data frame has its trim factor (who has those labels
> "mean","2500","2000","1500","1000","log","inverse","1SD","2SD")
> and the numerical variable of the data frame has the F values for each one
> of those trimming method.
> When I ask R to plot it, it doesn't keep the order of the trimm verctor
> correctly and the plot confuses the order so the most left one will be 1500
> and then 1SD etc.
> The values are correct but it is important for me to keep it in the same
> order I built it.
> How can I do it?
> Thank you very much,
> Or D.
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>



--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: plot is not keeping the order of variable

orduek
Ok,
It seems that the problem lays in the order (as Ista mentioned),
But, when I ask R to order it he chooses to order it by size and not by the
specific order I mentioned when I built the vector.
Is it possible to tell him to keep the order as mentioned?
Thank you.


On Sun, Feb 21, 2010 at 6:16 PM, Ista Zahn <[hidden email]> wrote:

> Hi Or,
> I can't know for sure what your problem is without an example, but the
> first thing I would do is check to make sure that your labels are
> stored as an ordered factor (and that the order is correct). See
> ?factor for details.
>
> Best,
> Ista
>
> On Sun, Feb 21, 2010 at 10:57 AM, Or Duek <[hidden email]> wrote:
> > Hi,
> > I created a simple data frame with one factor and one numerical variable.
> > The factor was actually a vector of names of techniques to trimm reaction
> > time data.
> > I want to create a plot that shows the value of F test for every trimming
> > method.
> > So the data frame has its trim factor (who has those labels
> > "mean","2500","2000","1500","1000","log","inverse","1SD","2SD")
> > and the numerical variable of the data frame has the F values for each
> one
> > of those trimming method.
> > When I ask R to plot it, it doesn't keep the order of the trimm verctor
> > correctly and the plot confuses the order so the most left one will be
> 1500
> > and then 1SD etc.
> > The values are correct but it is important for me to keep it in the same
> > order I built it.
> > How can I do it?
> > Thank you very much,
> > Or D.
> >
> >        [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] 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.
> >
>
>
>
> --
> Ista Zahn
> Graduate student
> University of Rochester
> Department of Clinical and Social Psychology
> http://yourpsyche.org
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: plot is not keeping the order of variable

Don MacQueen
Perhaps this example will help.

>  x <- factor(c('b','a','d'), levels=c('a','d','b'))
>  y <- 1:3
>  plot(x,y)

And compare with

>  z <- factor(c('b','a','d'))
>  plot(z,y)

In the first, the plot is in the order that I chose. In the second it
is in the order that R chose -- and R chose it according to a
built-in and reasonable rule.

-Don

At 6:30 PM +0200 2/21/10, Or Duek wrote:

>Ok,
>It seems that the problem lays in the order (as Ista mentioned),
>But, when I ask R to order it he chooses to order it by size and not by the
>specific order I mentioned when I built the vector.
>Is it possible to tell him to keep the order as mentioned?
>Thank you.
>
>
>On Sun, Feb 21, 2010 at 6:16 PM, Ista Zahn <[hidden email]> wrote:
>
>>  Hi Or,
>>  I can't know for sure what your problem is without an example, but the
>>  first thing I would do is check to make sure that your labels are
>>  stored as an ordered factor (and that the order is correct). See
>>  ?factor for details.
>>
>>  Best,
>>  Ista
>>
>>  On Sun, Feb 21, 2010 at 10:57 AM, Or Duek <[hidden email]> wrote:
>>  > Hi,
>>  > I created a simple data frame with one factor and one numerical variable.
>>  > The factor was actually a vector of names of techniques to trimm reaction
>>  > time data.
>>  > I want to create a plot that shows the value of F test for every trimming
>>  > method.
>>  > So the data frame has its trim factor (who has those labels
>>  > "mean","2500","2000","1500","1000","log","inverse","1SD","2SD")
>>  > and the numerical variable of the data frame has the F values for each
>>  one
>>  > of those trimming method.
>>  > When I ask R to plot it, it doesn't keep the order of the trimm verctor
>>  > correctly and the plot confuses the order so the most left one will be
>>  1500
>>  > and then 1SD etc.
>>  > The values are correct but it is important for me to keep it in the same
>>  > order I built it.
>>  > How can I do it?
>>  > Thank you very much,
>  > > Or D.
>  > >
>  > >        [[alternative HTML version deleted]]
>  > >
>  > > ______________________________________________
>  > > [hidden email] 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.
>>  >
>>
>>
>>
>>  --
>>  Ista Zahn
>>  Graduate student
>>  University of Rochester
>>  Department of Clinical and Social Psychology
>>  http://*yourpsyche.org
>>
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] 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.


--
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA
925-423-1062

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: plot is not keeping the order of variable

Peter Dalgaard
In reply to this post by orduek
Or Duek wrote:
> Ok,
> It seems that the problem lays in the order (as Ista mentioned),
> But, when I ask R to order it he chooses to order it by size and not by the
> specific order I mentioned when I built the vector.
> Is it possible to tell him to keep the order as mentioned?
> Thank you.

You're still not completely clear about how you built the factor, but I
venture a guess:

Notice that factors tend to get their levels in alphabetical order,
unless special tricks are used.

 > d <- data.frame(f=c("rare", "medium","well-done"))
 > d$f
[1] rare      medium    well-done
Levels: medium rare 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


>
>
> On Sun, Feb 21, 2010 at 6:16 PM, Ista Zahn <[hidden email]> wrote:
>
>> Hi Or,
>> I can't know for sure what your problem is without an example, but the
>> first thing I would do is check to make sure that your labels are
>> stored as an ordered factor (and that the order is correct). See
>> ?factor for details.
>>
>> Best,
>> Ista
>>
>> On Sun, Feb 21, 2010 at 10:57 AM, Or Duek <[hidden email]> wrote:
>>> Hi,
>>> I created a simple data frame with one factor and one numerical variable.
>>> The factor was actually a vector of names of techniques to trimm reaction
>>> time data.
>>> I want to create a plot that shows the value of F test for every trimming
>>> method.
>>> So the data frame has its trim factor (who has those labels
>>> "mean","2500","2000","1500","1000","log","inverse","1SD","2SD")
>>> and the numerical variable of the data frame has the F values for each
>> one
>>> of those trimming method.
>>> When I ask R to plot it, it doesn't keep the order of the trimm verctor
>>> correctly and the plot confuses the order so the most left one will be
>> 1500
>>> and then 1SD etc.
>>> The values are correct but it is important for me to keep it in the same
>>> order I built it.
>>> How can I do it?
>>> Thank you very much,
>>> Or D.
>>>
>>>        [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>>
>>
>>
>> --
>> Ista Zahn
>> Graduate student
>> University of Rochester
>> Department of Clinical and Social Psychology
>> http://yourpsyche.org
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.


--
    O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])              FAX: (+45) 35327907

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: plot is not keeping the order of variable

Dieter Menne
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)
Reply | Threaded
Open this post in threaded view
|

Re: plot is not keeping the order of variable

orduek
In reply to this post by Peter Dalgaard
Thank you very much,
You solved my problem.

On Mon, Feb 22, 2010 at 12:42 AM, Peter Dalgaard
<[hidden email]>wrote:

> Or Duek wrote:
>
>> Ok,
>> It seems that the problem lays in the order (as Ista mentioned),
>> But, when I ask R to order it he chooses to order it by size and not by
>> the
>> specific order I mentioned when I built the vector.
>> Is it possible to tell him to keep the order as mentioned?
>> Thank you.
>>
>
> You're still not completely clear about how you built the factor, but I
> venture a guess:
>
> Notice that factors tend to get their levels in alphabetical order, unless
> special tricks are used.
>
> > d <- data.frame(f=c("rare", "medium","well-done"))
> > d$f
> [1] rare      medium    well-done
> Levels: medium rare 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
>
>
>
>
>>
>> On Sun, Feb 21, 2010 at 6:16 PM, Ista Zahn <[hidden email]> wrote:
>>
>>  Hi Or,
>>> I can't know for sure what your problem is without an example, but the
>>> first thing I would do is check to make sure that your labels are
>>> stored as an ordered factor (and that the order is correct). See
>>> ?factor for details.
>>>
>>> Best,
>>> Ista
>>>
>>> On Sun, Feb 21, 2010 at 10:57 AM, Or Duek <[hidden email]> wrote:
>>>
>>>> Hi,
>>>> I created a simple data frame with one factor and one numerical
>>>> variable.
>>>> The factor was actually a vector of names of techniques to trimm
>>>> reaction
>>>> time data.
>>>> I want to create a plot that shows the value of F test for every
>>>> trimming
>>>> method.
>>>> So the data frame has its trim factor (who has those labels
>>>> "mean","2500","2000","1500","1000","log","inverse","1SD","2SD")
>>>> and the numerical variable of the data frame has the F values for each
>>>>
>>> one
>>>
>>>> of those trimming method.
>>>> When I ask R to plot it, it doesn't keep the order of the trimm verctor
>>>> correctly and the plot confuses the order so the most left one will be
>>>>
>>> 1500
>>>
>>>> and then 1SD etc.
>>>> The values are correct but it is important for me to keep it in the same
>>>> order I built it.
>>>> How can I do it?
>>>> Thank you very much,
>>>> Or D.
>>>>
>>>>       [[alternative HTML version deleted]]
>>>>
>>>> ______________________________________________
>>>> [hidden email] 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.
>>>>
>>>>
>>>
>>> --
>>> Ista Zahn
>>> Graduate student
>>> University of Rochester
>>> Department of Clinical and Social Psychology
>>> http://yourpsyche.org
>>>
>>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
>
> --
>   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
>  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
>  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
> ~~~~~~~~~~ - ([hidden email])              FAX: (+45) 35327907
>
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: plot is not keeping the order of variable

Peter Dalgaard
In reply to this post by Dieter Menne
Dieter Menne wrote:

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

Right. As I said, it gives "cast in order of appearance". The default
factor levels are sort(unique(x)) which is independent of data order,
but not necessarily desirable. In the general case, you have to tell R
about the order explicitly.

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


--
    O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])              FAX: (+45) 35327907

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