我目前正在尝试在 R 中进行主坐标分析 (PCoA)。我对 R 非常陌生,并且仍在尝试学习语法和代码。我成功地运行 PCoA 并将其绘制出来,现在我尝试使用 scatter3d() 函数在 3D 空间中可视化 PCoA。
我使用以下代码成功运行了 PCoA
#Running the PCoA
library(vegan)
library(labdsv)
Gowerdist <- vegdist(data.frame,method="gower", na.rm= TRUE)
pcotest <- pco(Gowerdist,k=4)
summary(pcoaTESTplot)
write.csv(pcotest$points,'pcotestPOINTS.csv')
#Plotting PcoA
library(ggplot2)
pcoaTESTplot <- read.csv("pcotestPOINTS.csv")
ggplot(pcoaTESTplot, aes(x=V1, y=V2, color=Species)) + geom_point() + geom_text(aes(label=Species),hjust=0, vjust=0)
pcotestPOINTS.csv 通常在 2D 平面中绘制,我按物种对其进行分组,并具有以下值(我是 R 新手,不知道如何将其写成代码;建议会有所帮助):
Species V1 V2 V3 V4
1 cf_M -0.031781895 -0.014792286 -0.004503777 -0.012610220
2 C -0.091464004 -0.134006338 -0.017100030 0.049538102
3 C -0.142280811 -0.071970920 0.057220986 0.015636930
4 G 0.127901175 -0.056155450 -0.018575333 0.015381534
5 G 0.116318613 -0.125552537 0.036418773 -0.098754726
6 G 0.212966778 -0.097406669 -0.023185002 0.081309634
7 G 0.063114834 -0.052422944 -0.027281979 -0.013183572
8 G 0.164193441 -0.145067313 0.047893500 -0.075261012
9 G 0.125573983 -0.030635914 -0.003522366 0.055693725
10 C -0.175866887 -0.049829963 -0.032233067 0.033557543
11 cf_M -0.135541377 0.055739251 -0.089503580 0.048764398
12 C -0.177278483 -0.022729224 -0.036536839 0.056107016
13 C -0.213010465 -0.048179837 -0.066925006 0.044377553
14 C -0.150118314 -0.011262976 0.052875986 0.078814272
15 C -0.052938204 -0.032302610 0.031115540 0.041222419
16 cf_M -0.060527464 0.047843822 -0.032686702 -0.116874986
17 cf_M -0.104463064 -0.056349285 0.031957309 -0.059974654
18 C -0.110412784 -0.023630954 0.005149408 0.044280367
19 cf_M -0.120946082 0.060083837 -0.085371294 -0.130249238
20 cf_M -0.052607412 -0.035729934 0.034557754 0.039291800
21 M -0.098428805 0.227005817 0.012707286 0.015943080
22 G 0.111732258 -0.105793117 -0.078062124 0.018757562
23 G 0.104440727 -0.043103550 -0.054803773 0.040568053
24 G 0.114630615 -0.102812853 0.029796076 -0.025098120
25 cf_G 0.041189558 -0.109686712 -0.081449510 0.012694654
26 G 0.139372615 -0.073429675 -0.035514832 -0.021797285
27 cf_G 0.049630172 -0.120238042 -0.082500823 -0.025354457
28 G 0.131962913 -0.079345351 -0.038031678 0.032418512
29 G 0.145388151 -0.073033647 -0.006097915 0.016838026
30 G 0.153083521 -0.080719015 0.009411666 0.013890614
31 G 0.163658995 -0.056128193 0.014838792 0.019248676
32 G 0.175740848 -0.055809349 -0.085783874 0.042118869
33 M 0.122374853 0.121760579 0.000972723 -0.048284135
34 M 0.073623753 0.083966711 -0.048553107 0.014595662
35 cf_M 0.002493609 -0.019775472 0.048228606 -0.107557856
36 cf_M -0.142542791 -0.048504297 -0.033862597 0.014891024
37 M 0.073067507 0.175692122 -0.032429380 -0.013033796
38 M 0.049394837 0.048055305 -0.048492332 0.024362833
39 M 0.043374473 0.148914450 -0.071568319 0.076386040
40 M 0.100479924 0.101136266 -0.000714071 0.069775037
41 C -0.095274095 -0.066087291 0.126446794 -0.054039041
42 C -0.050515560 -0.075369130 0.075846115 0.004257934
43 cf_C -0.120209368 -0.044737012 -0.015814314 0.029790605
44 M 0.033819722 0.077098451 0.103200615 0.001797658
45 M 0.099041728 0.127793360 0.123679516 -0.092233055
46 C -0.119684548 -0.071573066 0.020774450 0.045440300
47 M 0.080064569 0.158117147 0.050984478 0.049517871
48 M 0.073061563 0.179736841 0.061438231 -0.085872914
49 M 0.066196996 0.126650019 -0.073256733 0.050736463
50 M -0.017180859 0.092915512 -0.062340826 0.030966866
51 M 0.007313941 0.030544171 0.034107786 -0.008451064
52 M 0.030077136 0.091946729 0.019021861 -0.037148376
53 M 0.181104379 0.154261866 0.184970234 0.152371966
54 cf_M -0.076461621 0.038913381 -0.094850112 -0.075737783
55 cf_M -0.077452675 0.058624603 -0.104210238 -0.028904142
56 C -0.136410016 -0.068696015 0.032681381 0.027559673
57 cf_M -0.084262114 0.025497711 -0.046012632 -0.090147470
58 C -0.099403208 -0.049318827 0.047823149 -0.074616210
59 cf_C -0.151949338 0.003355951 -0.074866137 0.026535190
60 M -0.048272207 0.035885684 -0.036572954 -0.024464274
61 M 0.035272332 0.137994016 0.048921034 -0.033152910
62 M 0.061062726 0.088220032 0.027235884 0.006511185
63 cf_M -0.022678804 0.096566014 -0.089668642 -0.032362149
64 M 0.100783139 0.070006730 0.086195185 -0.022204185
65 cf_C -0.009137953 0.017062431 -0.050115368 -0.133785442
66 cf_M -0.107810732 -0.068024004 0.021125172 0.021052237
67 G 0.095668772 -0.138675431 -0.028579849 -0.076913412
68 M -0.027020841 0.069674169 -0.021508615 0.032142949
69 C -0.226937501 -0.080085817 0.216765725 0.015425306
70 G 0.203314776 -0.110344554 0.079133253 0.040076830
71 C -0.153490987 -0.013755267 0.165370191 -0.036327947
72 G 0.113580066 -0.166450142 -0.014627538 -0.018557855
73 M -0.132917211 0.008685202 0.031339457 0.058982043
74 cf_M -0.000375639 0.030195173 -0.024656948 0.018778677
75 C -0.159551518 -0.026830563 -0.020288912 0.049217439
76 M 0.057460058 0.096136625 0.006413249 -0.029953721
77 cf_M -0.066324419 0.070271569 -0.083959037 0.025280882
当我使用 scatter3d() 函数时:
library(scatterplot3d)
library(plot3D)
scatter3d(x = pcoaTESTplot$V1, y = pcoaTESTplot$V2, z = pcoaTESTplot$V3,
point.col = "blue", groups = pcoaTESTplot$Species, ellipsoid = TRUE, grid = TRUE, surface = FALSE)
我绘制了所有物种,但只有“C”物种得到一个椭圆,以及错误
chol.default(shape) 中的错误:3 阶的前导次要不是正定的
我曾尝试更改重新排列值或查看它是否与接近零的值有关,但我知道有人用相似的数字运行相同的代码并且所有组都有一个椭圆。我还尝试使用只有 2 个点的“cf_G”并将其与“G”分组,以查看是否由于试图在两个点上形成椭圆而出现错误,但我仍然遇到相同的错误。有谁知道错误来自什么?谢谢!(对任何粗略的代码/语法表示歉意......)