ggplot 中的数据重新排序

标签 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

我缺少什么可以订购值? 感谢您的帮助!

最佳答案

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

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

enter image description here

关于ggplot 中的数据重新排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23886472/

相关文章:

r - 使用ggplot2绘制两个长度不等的数据集

r - 如何使用数据表中当前行前后 2 行的值

r - 如何使用小平面网格和位置闪避对绘图中的点范围进行排序和着色?

r - ggplot2 scale_x_log10() 破坏/不适用于通过 stat_function() 绘制的函数

r - emmeans:使标记的意思更大

r - 如何将标题与换行符左对齐?

r - 使用 ggplot2 进行多变量回归

R 有没有办法找到 Inf/-Inf 值?

r - 多边形边的排序

删除基于 2 个 data.frames 之间重复的观察结果