使用 ggplot 在 MDS 上绘制向量
plot vectors on MDS using ggplot
我需要绘制一个 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 不知道如何处理 class metaMDS/monoMDS 的数据)。
我想要这样的东西
谢谢
根据您的代码,我不确定您的确切目标是什么,但您可能需要注意以下几点。
要点1:不要在顶层ggplot()
调用中放任何东西,除非你想让后续层继承它。
而不是:
g <- ggplot(data = mds, aes(MDS1, MDS2)) +
使用:
g <- ggplot() +
您已经创建了数据框 mdspoints
和 mdsvectors
,并且您的几何图层 none 需要 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 <- 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
,"species" 没有列名。行名是否与物种相对应?我不确定,因为我自己不使用 vegan 包,但快速查看其 scores()
功能的帮助文件会发现以下内容:
## Default S3 method:
scores(x, choices, display=c("sites", "species"), ...)
这表明这可能是 sites 的分数,而不是 species 的分数。如果这种理解是正确的,您需要在创建 mdspoints
时指定 "species",并根据行名称手动创建 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
我需要绘制一个 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 不知道如何处理 class metaMDS/monoMDS 的数据)。
我想要这样的东西
谢谢
根据您的代码,我不确定您的确切目标是什么,但您可能需要注意以下几点。
要点1:不要在顶层ggplot()
调用中放任何东西,除非你想让后续层继承它。
而不是:
g <- ggplot(data = mds, aes(MDS1, MDS2)) +
使用:
g <- ggplot() +
您已经创建了数据框 mdspoints
和 mdsvectors
,并且您的几何图层 none 需要 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 <- 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
,"species" 没有列名。行名是否与物种相对应?我不确定,因为我自己不使用 vegan 包,但快速查看其 scores()
功能的帮助文件会发现以下内容:
## Default S3 method: scores(x, choices, display=c("sites", "species"), ...)
这表明这可能是 sites 的分数,而不是 species 的分数。如果这种理解是正确的,您需要在创建 mdspoints
时指定 "species",并根据行名称手动创建 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