在ggplot2中绘制envfit矢量(素食包) [英] Plotting envfit vectors (vegan package) in ggplot2

查看:699
本文介绍了在ggplot2中绘制envfit矢量(素食包)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

下面的例子从另一个问题(将素食包中的ordiellipse函数绘制到ggplot2创建的NMDS图上),但它表达了我想包括的例子,因为我使用这个问题帮助我将metaMDS放入ggplot2中:

 图书馆(素食主义者)
图书馆(ggplot2)
数据(沙丘)

#计算NMDS的距离
NMDS.log< -log(dune + 1)
sol< - metaMDS(NMDS.log)

#创建用于分组$ b $的元数据MyMeta = data.frame(
sites = c(2,13,4,16,6,1,8,5,17,15,10) ,11,9,18,3,3,20,14,19,12,7),
amt = c(hi,hi,hi,md,lo,hi hilomdmdlo
lohilohimdmd lo,hi,lo),
row.names =sites)

#使用基本绘图函数和colo绘制NMDS从MyMeta
绘图(sol $ points,col = MyMeta $ amt)

#与ggplot2相同
NMDS = data.frame(MDS1 = sol $ (数据= NMDS,aes(MDS1,MDS2))+
geom_point(aes(data = MyMeta,color = MyMeta $,点数[,1],MDS2 = sol $ points [,2])
ggplot am))

#添加物种加载
vec.sp< -envfit(sol $ points,NMDS.log,perm = 1000)
plot(vec.sp,p。 max = 0.1,col =blue)


解决方案

添加库。此外还需要库 grid

  library(ggplot2)
图书馆(纯素)
图书馆(网格)
数据(沙丘)

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

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

NMDS = data.frame(MDS1 = sol $ points [,1],MDS2 = sol $ points [,2])

添加物种加载并将它们保存为数据框。箭头余弦的方向存储在列表向量和矩阵箭头中。为了得到箭头的坐标,这些方向值应该乘以存储在向量中的 r2 值的平方根$ r 。更直接的方法是使用@Gavin Simpson的答案中提供的函数 scores()。然后添加包含种类名称的新列。

  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)

使用 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 =箭头(长度=单位(0.5,cm)),color =gray,inherit_aes = FALSE)+
geom_text(data = vec.sp.df,aes(x = MDS1,y = MDS2,label = species),size = 5)+
coord_fixed()


I am working on finalizing a NMDS plot that I created in vegan and ggplot2 but cannot figure out how to add envfit species-loading vectors to the plot. When I try to it says "invalid graphics state".

The example below is slightly modified from another question (Plotting ordiellipse function from vegan package onto NMDS plot created in ggplot2) but it expressed exactly the example I wanted to include since I used this question to help me get metaMDS into ggplot2 in the first place:

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")

解决方案

Start with adding libraries. Additionally library grid is necessary.

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

Do metaMDS analysis and save results in data frame.

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

NMDS = data.frame(MDS1 = sol$points[,1], MDS2 = sol$points[,2])

Add species loadings and save them as data frame. Directions of arrows cosines are stored in list vectors and matrix arrows. To get coordinates of the arrows those direction values should be multiplied by square root of r2 values that are stored in vectors$r. More straight forward way is to use function scores() as provided in answer of @Gavin Simpson. Then add new column containing species names.

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)

Arrows are added with geom_segment() and species names with geom_text(). For both tasks data frame vec.sp.df is used.

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()

这篇关于在ggplot2中绘制envfit矢量(素食包)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆