[R] sorting in barplot

Marc Schwartz marc_schwartz at comcast.net
Wed May 2 18:04:18 CEST 2007


Romain,

Try this before calling bargraph.CI():

 Fst2$REGION <- reorder(Fst2$REGION, Fst2$MOY_FST, mean)

This will reorder the factor levels for REGION, based upon the mean
value of MOY_FST for each region. Thus, your REGIONS should then be
sorted by increasing mean values in the plot.

For example, note the factor levels for REGION at first:

> str(Fst2)
'data.frame':   27 obs. of  2 variables:
 $ REGION : Factor w/ 3 levels "AR","GE","HT-S": 1 1 1 1 1 1 1 1 1 1 ...
 $ MOY_FST: num  0.039 0.04 0.041 0.041 0.044 0.051 0.055 0.058 0.069 0.076 ...


Now, note them after using reorder.factor():

> Fst2$REGION <- reorder(Fst2$REGION, Fst2$MOY_FST, mean)

> str(Fst2)
'data.frame':   27 obs. of  2 variables:
 $ REGION : Factor w/ 3 levels "AR","HT-S","GE": 1 1 1 1 1 1 1 1 1 1 ...
 $ MOY_FST: num  0.039 0.04 0.041 0.041 0.044 0.051 0.055 0.058 0.069 0.076 ...


This is consistent with the means for each REGION:

> tapply(Fst2$MOY_FST, Fst2$REGION, mean)
        AR       HT-S         GE 
0.05140000 0.08916667 0.11945455 



Unless the author of the CRAN package that you are using has internally
altered the factor levels for the variable, you should be OK.

Another alternative, if needed, would be to use the barplot2() function
in the gplots package, or simply use barplot() and then add the CI's
using arrows() or segments().  There is an example of doing this in the
help for both functions.

HTH,

Marc

On Wed, 2007-05-02 at 17:36 +0200, Romain.Mayor at ville-ge.ch wrote:
> Thank you for your answer,
> 
> But I think it doesn't work, because the function "bargraph.CI" (I use it
> because barplot doesn't have a standard error option) use two internally
> function(one for mean and the other for standard error) which use something
> like tapply (which sort the factors by alphabet). If I decompose the two
> function and  than reorder one time with mean and one other time with
> standard error, so it doesn't match on the graph.
> I try also so: bargraph.CI(x.factor = REGION, response = MOY_FST, data =
> Fst2,ylab = "Average Fst by pair of
> populations",reorder(Fst2$REGION,Fst2$MOY_FST,mean))
> with tihs Fst2 table:
>   REGION MOY_FST
> 1      AR   0.039
> 2      AR   0.040
> 3      AR   0.041
> 4      AR   0.041
> 5      AR   0.044
> 6      AR   0.051
> 7      AR   0.055
> 8      AR   0.058
> 9      AR   0.069
> 10     AR   0.076
> 11   HT-S   0.080
> 12   HT-S   0.084
> 13   HT-S   0.090
> 14   HT-S   0.091
> 15   HT-S   0.094
> 16   HT-S   0.096
> 17     GE   0.079
> 18     GE   0.086
> 19     GE   0.095
> 20     GE   0.104
> 21     GE   0.107
> 22     GE   0.118
> 23     GE   0.119
> 24     GE   0.137
> 25     GE   0.139
> 26     GE   0.152
> 27     GE   0.178
> 
> But it doesn't work well.
> 
> RM, best regards.
> 
> 
> 
>                                                                            
>              Marc Schwartz                                                 
>              <marc_schwartz at co                                             
>              mcast.net>                                                  A 
>                                        Romain.Mayor at ville-ge.ch            
>              01.05.2007 19:49                                           cc 
>                                        r-help at stat.math.ethz.ch            
>                                                                      Objet 
>              Veuillez répondre         Re: [R] sorting in barplot          
>                      à                                                     
>              marc_schwartz at com                                             
>                  cast.net                                                  
>                                                                            
>                                                                            
>                                                                            
> 
> 
> 
> 
> On Tue, 2007-05-01 at 19:33 +0200, Romain.Mayor at ville-ge.ch wrote:
> > Hello,
> >
> > I'm trying to sort my bargraph.CI plot (function like barplot in the
> > SCIPLOT package) by the mean of the response variable.
> >
> > Does somebody have a trick for it.
> >
> > Thank you.
> >
> > Romain Mayor, PHD student.
> 
> If it is built on top of barplot(), then by default, the factor levels
> of your response variable will determine the order of the bars in the
> plot.
> 
> See ?reorder.factor for more details relative to defining the order
> based upon the mean of the variable. There is an example there of using
> the median.
> 
> HTH,
> 
> Marc Schwartz
> 
> 
>



More information about the R-help mailing list