在ggplot中重新排序数据 [英] reorder data in ggplot

查看:764
本文介绍了在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过渡性
19 Pyxis 0.928 hypolimnic
20 Sphingobacteria_GQB5E 0.928过渡性
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))



然后我尝试重新排序

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ fill = preference_watermass))+
geom_bar(stat =identity)+
coord_flip()+
labs(y =Spearman correlation rho,x =tribe)+
scale_fill_manual(values = c(blue,red,black))

但是得到这个:



我错过了什么,我可以订购这些值?
感谢您的帮助!

解决方案

您必须重新排序数据本身,而不仅限于 aes ,正如@amzu所述:

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


New and stuck in and at ggplot:

I have the following data:

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

I wanted to make a bargraph were factors(tribes) are sorted according to accending values of 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"))

Then I tried reordering

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

But get this one:

What am I missing that I can order the values? Thanks for your help!

解决方案

You have to reorder the data itself, not just within aes, as correctly stated by @amzu:

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

这篇关于在ggplot中重新排序数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆