0

I have a data.frame like this

    OTU         DCA1.Score  ID
1   Ac1         -0.05377328 Gp01
2   Ac2          2.49918986 Gp02
3   Ac3          0.42897126 Gp04

and another one like this

    OTU     DCA1.Score      ID
1   APB_2   1.151251e+00    APB
2   APB_1   9.556547e-01    APB
3   ACT_8   -2.686852e-01   ACT
4   ACT_3   -5.396745e-01   ACT
5   VMC_1   -1.236140e-02   VMC
6   ACT_15  -3.473628e-01   ACT
7   ACT_1   -2.229005e-01   ACT

In both cases i use ggplot2 for a barchart

library(ggplot2)    
ggplot(df, aes(x=OTU,y=DCA1.Score, color=ID)) + 
geom_bar(stat="identity") + 
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1, size=5))

In reality both dataframes have 340 rows. The first df features 21 factors in the ID column, the second one just 13.

ggplot will sort the second dataframe by factor "ID" before plotting, the first one is left untouched. Why is that?

See the results

nouse
  • 3,315
  • 2
  • 29
  • 56
  • possible duplicate of [ggplot legends - change labels, order and title](http://stackoverflow.com/questions/12075037/ggplot-legends-change-labels-order-and-title) – Henrik Oct 15 '14 at 14:10
  • Or is this a more canonical answer: http://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph/5210833#5210833 – Henrik Oct 15 '14 at 14:11
  • 1
    Isnt the real question here why ggplot does a reorder/sorting in one case, but not in the other? – nouse Oct 15 '14 at 14:19
  • I assume that both barplots are sorted, but the first `data.frame` happens to be already in the right order. A closer look to `str(df1)` and `str(df2)` would shed light on this. – thothal Oct 15 '14 at 14:42
  • Yup, the first data.frame is indeed sorted by ID, the latter isnt. Curiously, the plots are carried out vice versa (i.e. the first df is sorted by OTU (alphabetically), while the other is sorted by ID (alphabetically) prior to the plotting. – nouse Oct 15 '14 at 14:59

0 Answers0