【问题标题】:plot vectors on MDS using ggplot使用 ggplot 在 MDS 上绘制向量
【发布时间】:2018-09-01 22:23:53
【问题描述】:

我需要绘制一个带有向量的 MDS,以显示我的物种的丰度变化。 我需要只有向量的情节

这是我的每个物种和代码的丰富数据

library(vegan)

A <- c(54.67, 37.67, 19.33, 0, 6, 8, 84.67, 0,0,0,0,0,0,0)
B <- c(3.67, 10.33, 32.67, 5.33, 20.33, 5.33, 4.67, 3, 4, 0.01, 0.1, 0, 5, 0)
C <- c(10, 1.67, 2.67, 1.67, 11.33, 1.33, 1, 2, 2.77, 0, 0.02, 1,3,0)
D <- c(1,10.33, 2.33, 28.33, 29.33, 4.33, 21, 6.97, 4.47, 0, 0.16, 11, 4,0)
df <- cbind(A, B, C, D)
row.names(df) <- c('B_2016', 'Emb_2016', 'Fes_2016', 'Ku_2016', 'Ra_2016', 'Ud_2016',
                   'Ve_2016', 'Ba_2017', 'Emb_2017', 'Fes_2017', 'Ku_2017', 'Ra_2017', 
                   'Ud_2017', 'Ve_2017')

mds <- metaMDS(df, distance='bray')

我正在使用这些代码来创建数据框

mdspoints <- data.frame(scores(mds))
mdsvectors <- data.frame(mds$species)

这是我用来绘制图表的代码

g <- ggplot(data = mds, aes(MDS1, MDS2)) + 
  geom_segment(data = mdsvectors, aes(x=0, xend=MDS1, y=0, yend=MDS2),
               arrow = arrow(length = unit(0.5, "cm")),
               colour="grey", inherit_aes = FALSE) + 
  geom_text(data=mdspoints, aes(x=MDS1, y=MDS2, label=species), size=5)

但我无法绘制任何图形并出现错误(错误:ggplot2 不知道如何处理 metaMDS/monoMDS 类的数据)。

我想要这样的东西

谢谢

【问题讨论】:

  • 呃,你得到的错误是相关的。
  • 我怎样才能创建正确的情节?
  • 好吧,我将首先解释您之前所说的错误。
  • 在 NMDS 中使用来自原点的向量来显示物种没有多大意义,因为它们是使用加权平均值计算的。
  • 您的图表显示的内容与您所说的想要拥有的完全不同。您的代码尝试绘制从原点到物种点的箭头(您不应该像@RichardTelford 告诉您的那样做),但该图显示了来自不同点的箭头。你喜欢展示什么?从原点改变点或距离和方向?

标签: r ggplot2 vegan mds


【解决方案1】:

根据您的代码,我不确定您的目标到底是什么,但您可能需要注意以下几点。

第 1 点:不要在顶层 ggplot() 调用中放置任何内容,除非您希望后续层继承它。

代替:

g <- ggplot(data = mds, aes(MDS1, MDS2)) + 

用途:

g <- ggplot() + 

您已经创建了数据框 mdspointsmdsvectors,并且您的几何图层都不需要来自 mds 的任何内容。你在这里真的不需要它。但是因为它那里,ggplot会检查它。

如果mds 是一个数据框,它将通过 ggplot 的检查,并被忽略,因为后续层不需要它。但是,它是metaMDS / monoMDS 对象,导致ggplot 抛出你看到的错误。

第 2 点:检查您的数据框是否符合您的预期。

您的代码包括以下行:

  geom_text(data=mdspoints, aes(x=MDS1, y=MDS2, label=species), size=5)

这告诉 ggplot 对于绘制标签,它应该查看 mdspoints 数据框,并搜索名为 MDS1 / MDS2 / species 的变量。

这实际上是从mdspoints &lt;- data.frame(scores(mds)) 创建的:

> mdspoints
             NMDS1         NMDS2
B_2016   -141.6526 -6.290613e-01
Emb_2016 -141.8424 -3.280861e-01
Fes_2016 -142.1144 -4.456856e-01
Ku_2016  -141.8335  3.674413e-01
Ra_2016  -141.8977  2.283486e-02
Ud_2016  -141.8824 -1.480702e-01
Ve_2016  -141.5302 -3.732303e-01
Ba_2017  -141.9265  2.233302e-01
Emb_2017 -141.9695  1.210940e-01
Fes_2017 -140.6462  1.430899e-01
Ku_2017  -141.8616  2.216499e-01
Ra_2017  -141.7638  7.116520e-01
Ud_2017  -142.0109  1.130730e-01
Ve_2017  1842.9317 -3.167902e-05

所以,NMDS1 / NMDS2 而不是 MDS1 / MDS2,并且没有“物种”的列名。行名是否与物种相对应?我不确定,因为我自己不使用 vegan 包,但快速查看其scores() 函数的帮助文件会发现以下内容:

## Default S3 method:
scores(x, choices, display=c("sites", "species"), ...)

这表明这可能是 sites 的分数,而不是 species。如果这种理解是正确的,您将需要在创建 mdspoints 时指定“物种”,并根据行名手动创建 species 列:

mdspoints <- data.frame(scores(mds, "species"))
mdspoints$species <- row.names(mdspoints)

结果

剧情如下:

ggplot() + 
  geom_segment(data = mdsvectors, aes(x=0, xend=MDS1, y=0, yend=MDS2),
               arrow = arrow(length = unit(0.5, "cm")),
               colour="grey") +
  geom_text(data=mdspoints, aes(x=NMDS1, y=NMDS2, label=species), size=5) +
  labs(x = "NMDS1", y = "NMDS2") + # add axis labels
  theme_classic()                  # use a white theme for better contrast

【讨论】:

  • Z.Lin 的另一个出色、全面的答案:)
  • 您应该始终在排序图中具有相同的纵横比。您可以在图表中添加+ coord_equal()+ coord_fixed() 来实现此目的。执行此操作时,您会发现图表中只有一个实轴,并且箭头几乎没有意义(似乎 NMDS2 在输入数据中显示舍入错误)。
  • @JariOksanen 谢谢,那么我应该使用什么代码..我需要创建箭头来连接每个位置的 2016-2017 年数据点对,以显示我网站上物种 A-B-C-D 的组成变化
猜你喜欢
  • 2017-08-25
  • 2018-11-11
  • 2020-11-19
  • 2020-11-04
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多