gpt4 book ai didi

ggplot 中的数据重新排序

转载 作者:行者123 更新时间:2023-12-02 21:26:24 28 4
gpt4 key购买 nike

新的并且坚持使用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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com