在ggplot中重新排序数据

时间:2014-05-27 10:02:28

标签: r ggplot2

新的并且停留在ggplot:

我有以下数据:

tribe   rho preference_watermass
1   Luna2   -1.000  hypolimnic
2   OP10I-A1    -1.000  epilimnic
3   B0_FO56C    -0.986  hypolimnic
4   Planctomycetes_FGIDN    -0.943  hypolimnic
5   acIV_IVNEG  -0.943  hypolimnic
6   FTD4J6C01EE04F  -0.941  hypolimnic
7   alll    -0.941  hypolimnic
8   FTD4J6C02HMHDM  -0.928  hypolimnic
9   Planctomycetes_GQLBU    -0.928  hypolimnic
10  acII-A  -0.886  epilimnic
11  LD19_GMKGQ  -0.880  hypolimnic
12  acIV_E2W4O  -0.880  hypolimnic
13  Planctomycetes_AFE3Z    -0.880  epilimnic
14  Bacteriodetes   -0.880  epilimnic
15  FTD4J6C01APQD0  -0.880  epilimnic
16  FTD4J6C02GBAUX  -0.880  epilimnic
17  baII    -0.845  epilimnic
18  FTD4J6C01CJCBG  0.812   transitient
19  Pyxis   0.928   hypolimnic
20  Sphingobacteria_GQB5E   0.928   transitient
21  acI-A_I8OXG 0.943   hypolimnic
22  LD12    0.943   epilimnic

我想制作一个条形图,根据rho的加入值对因子(部落)进行排序。

plot2<-ggplot(data=dfm, aes(x=tribe,y=rho,fill=preference_watermass))+
  geom_bar(stat="identity")+
  coord_flip()+
  labs(y="Spearman correlation rho",x="tribe")+
  scale_fill_manual(values=c("blue", "red", "black"))

enter image description here

然后我尝试重新排序

plot2<-ggplot(data=dfm, aes(reorder(tribe,rho,order=TRUE),rho,fill=preference_watermass))+
  geom_bar(stat="identity")+
  coord_flip()+
  labs(y="Spearman correlation rho",x="tribe")+
  scale_fill_manual(values=c("blue", "red", "black"))

但是得到这个: enter image description here

我错过了什么可以订购价值? 谢谢你的帮助!

1 个答案:

答案 0 :(得分:3)

您必须重新排序数据本身,而不仅仅是在aes内,正如@amzu正确说明的那样:

dfm$tribe <- reorder(dfm$tribe, dfm$rho)
plot2

enter image description here