在ggplot2中绘制envfit载体(素食包)

HFB*_*ing 6 r ggplot2 vegan

我正在努力完成我在素食主义者和ggplot2中创建的NMDS图,但无法弄清楚如何在图中添加envfit物种加载矢量.当我尝试它时说"无效的图形状态".

下面的例子稍微修改了另一个问题(从vegan包绘制ordiellipse函数到ggplot2中创建的NMDS图),但它完全表达了我想要包含的例子,因为我使用这个问题来帮助我首先将metaMDS导入ggplot2:

library(vegan)
library(ggplot2)
data(dune)

# calculate distance for NMDS
NMDS.log<-log(dune+1)
sol <- metaMDS(NMDS.log)

# Create meta data for grouping
MyMeta = data.frame(
  sites = c(2,13,4,16,6,1,8,5,17,15,10,11,9,18,3,20,14,19,12,7),
  amt = c("hi", "hi", "hi", "md", "lo", "hi", "hi", "lo", "md", "md", "lo", 
      "lo", "hi", "lo", "hi", "md", "md", "lo", "hi", "lo"),
row.names = "sites")

# plot NMDS using basic plot function and color points by "amt" from MyMeta
plot(sol$points, col = MyMeta$amt)

# same in ggplot2
NMDS = data.frame(MDS1 = sol$points[,1], MDS2 = sol$points[,2])
ggplot(data = NMDS, aes(MDS1, MDS2)) + 
  geom_point(aes(data = MyMeta, color = MyMeta$amt))

#Add species loadings
vec.sp<-envfit(sol$points, NMDS.log, perm=1000)
plot(vec.sp, p.max=0.1, col="blue")
Run Code Online (Sandbox Code Playgroud)

Rei*_*son 7

(否则是优秀的)接受的答案的问题,并解释了为什么矢量在所包含的图中的长度都相同[ 注意,现在已经编辑了接受的答案以按照我在下面描述的方式缩放箭头,以避免对于遇到Q&A的用户来说,混淆的是,$vectors$arrows返回的对象组件中存储envfit()的是拟合向量的方向余弦.这些都是单位长度,因此@Didzis Elferts的图中的箭头都是相同的长度.这与输出不同plot(envfit(sol, NMDS.log)),因为我们通过与排序配置("轴")的相关性来缩放矢量箭头坐标.这样,与排序配置显示弱关系的物种会得到较短的箭头.缩放是通过将方向余弦乘以打印输出表中显示的值的sqrt(r2)位置r2来完成的.当矢量添加到现有图中时,素食主义者还尝试缩放矢量集,使得它们填充可用的绘图空间,同时保持箭头的相对长度.如何完成此操作将在详细信息部分中讨论,?envfit并要求使用未导出的函数vegan:::ordiArrowMul(result_of_envfit).

这是一个完整的工作示例,它复制了plot.envfit使用ggplot2的行为:

library(vegan)
library(ggplot2)
library(grid)
data(dune)

# calculate distance for NMDS
NMDS.log<-log1p(dune)
set.seed(42)
sol <- metaMDS(NMDS.log)

scrs <- as.data.frame(scores(sol, display = "sites"))
scrs <- cbind(scrs, Group = c("hi","hi","hi","md","lo","hi","hi","lo","md","md",
                              "lo","lo","hi","lo","hi","md","md","lo","hi","lo"))

set.seed(123)
vf <- envfit(sol, NMDS.log, perm = 999)
Run Code Online (Sandbox Code Playgroud)

如果我们在这一点停下来看看vf:

> vf

***VECTORS

             NMDS1       NMDS2     r2 Pr(>r)    
Belper -0.78061195 -0.62501598 0.1942  0.174    
Empnig -0.01315693  0.99991344 0.2501  0.054 .  
Junbuf  0.22941001 -0.97332987 0.1397  0.293    
Junart  0.99999981 -0.00062172 0.3647  0.022 *  
Airpra -0.20995196  0.97771170 0.5376  0.002 ** 
Elepal  0.98959723  0.14386566 0.6634  0.001 ***
Rumace -0.87985767 -0.47523728 0.0948  0.429
.... <truncated>
Run Code Online (Sandbox Code Playgroud)

因此,r2数据被用于缩放在列中的值NMDS1NMDS2.最终的情节产生于:

spp.scrs <- as.data.frame(scores(vf, display = "vectors"))
spp.scrs <- cbind(spp.scrs, Species = rownames(spp.scrs))

p <- ggplot(scrs) +
  geom_point(mapping = aes(x = NMDS1, y = NMDS2, colour = Group)) +
  coord_fixed() + ## need aspect ratio of 1!
  geom_segment(data = spp.scrs,
               aes(x = 0, xend = NMDS1, y = 0, yend = NMDS2),
               arrow = arrow(length = unit(0.25, "cm")), colour = "grey") +
  geom_text(data = spp.scrs, aes(x = NMDS1, y = NMDS2, label = Species),
            size = 3)
Run Code Online (Sandbox Code Playgroud)

这会产生:

在此输入图像描述

  • @Vinterwoo无论有没有`coord_fixed`&`ratio = 1`,这些点都会匹配`envfit`的`plot`方法中的正确点位置.需要`coord_fixed`等部分来维护排序解决方案的度量属性; 两个轴应按相同的"单位"缩放. (2认同)

Did*_*rts 5

从添加库开始。另外,库grid是必需的。

library(ggplot2)
library(vegan)
library(grid)
data(dune)
Run Code Online (Sandbox Code Playgroud)

进行metaMDS分析并将结果保存在数据框中。

NMDS.log<-log(dune+1)
sol <- metaMDS(NMDS.log)

NMDS = data.frame(MDS1 = sol$points[,1], MDS2 = sol$points[,2])
Run Code Online (Sandbox Code Playgroud)

添加物种负荷并将其保存为数据框。箭头余弦的方向存储在list vectors和matrix中arrows。要获得箭头的坐标,应将这些方向值乘以r2存储在中的值的平方根vectors$r。更直接的方法是使用scores()@Gavin Simpson的答复中提供的功能。然后添加包含species名称的新列。

vec.sp<-envfit(sol$points, NMDS.log, perm=1000)
vec.sp.df<-as.data.frame(vec.sp$vectors$arrows*sqrt(vec.sp$vectors$r))
vec.sp.df$species<-rownames(vec.sp.df)
Run Code Online (Sandbox Code Playgroud)

箭头添加为geom_segment(),物种名称添加为geom_text()。对于这两个任务,vec.sp.df都使用数据框。

ggplot(data = NMDS, aes(MDS1, MDS2)) + 
  geom_point(aes(data = MyMeta, color = MyMeta$amt))+
  geom_segment(data=vec.sp.df,aes(x=0,xend=MDS1,y=0,yend=MDS2),
      arrow = arrow(length = unit(0.5, "cm")),colour="grey",inherit_aes=FALSE) + 
  geom_text(data=vec.sp.df,aes(x=MDS1,y=MDS2,label=species),size=5)+
  coord_fixed()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明