使用ggplot2从素食包中绘制ordiellipse [英] Plotting ordiellipse from vegan package with ggplot2

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

问题描述

我想使用ggplot2为我正在处理的一些数据制作一个NMDS排序图。有一个很好的例子来说明如何从这里找到的先前的堆栈溢出线程执行此操作:从素食包中绘制ordiellipse函数到ggplot2中创建的NMDS图

我试图从此前一个线程并用于我的数据。但是,我将函数传递给R后遇到以下错误消息:

数组(x,c(length(x),1L)中的错误,if (!is.null(names(x)))list(names(x),:
'data'必须是一个向量类型,是'NULL'

我可以使用随Vegan包提供的沙丘数据集的功能,但是使用我自己的数据集时遇到了障碍。任何人有任何想法或想法?我在下面提供了一个链接到我的数据集并粘贴了我的代码

可以看到这些数据:
https://docs.google.com/spreadsheets/d/1O2YCapLaCMlCco3-0mZ07_KoJPTfWdjWYudLvMd183Y/edit?usp=sharing



代码:

 #rm(list = ls(all = T ))
setwd('C:')
库(素食主义者)
库(ggplot2)

PCBprop< -read.csv(PCBprop_stackoverflow。 csv,header = T)
#Subset到密歇根湖观察
MICH <-PCBprop [with(PCBprop,BASIN ==MICHIGAN),]
#MICHIGAN
michcovariate< -MICH [,c(1,2)]#协变量数据
michcongener< -MICH [,3:60] #PCB同类数据
michpcbnmds< - metaMDS(michcongener,k = 2,距离='bray',autotransform = TRUE,trymax = 500)
得分<-scores(michpcbnmds)
nmdsscores <-data.frame(cbind(michcovariate,score))
plot(michpcbnmds $ points,col = nmdsscores $ CAT2)#来自纯素的基本排序图
ord <-ordiellipse(michpcbnmds,nmdsscores $ CAT2,display =sites,kind =se,conf = 0.95,label = T) #95%椭圆图

#Ordiellipse图
NMDS = data.frame(NMDS1 = nmdsscores $ NMDS1,NMDS2 = nmdsscores $ NMDS2,group = nmdsscores $ CAT2)#设置数据帧
df_ell< - data.frame()
for(g in levels(NMDS $ group)){
df_ell < - rbind(df_ell,cbind(as.data.frame(with (NMDS [NMDS $组== g,],
素食主义者::: veganCovEllipse(ord [[g]] $ cov,ord [[g]] $ center,ord [[g]] $ scale))),group = g))}



g1 <-ggplot(data = NMDS,aes(NMDS1,NMDS2))+ geom_point(aes(color = group),size = 1.5)+
geom_path(data = df_ell,aes (x = NMDS1,y = NMDS2,color = group),size = 1.5,linetype = 2)+
scale_colour_brewer(palette =Dark2,name =Species_Location)


src =https://i.stack.imgur.com/114WM.pngalt =>



问题在于 ord其中一个组( BND_NO )为[[g]] = NULL 。这可能是因为当 BASIN == MICHIGAN 时,该组只有2行数据,并且不足以产生95%的置信度椭圆。



我在循环开始处添加了以下行:

  if(is.null(ord [[g]]))next 

生成上面的图。


I am would like to make an NMDS ordination plot using ggplot2 for some data I am working up. There is an excellent example of how to do this from a previous stack overflow thread found here: Plotting ordiellipse function from vegan package onto NMDS plot created in ggplot2

I have attempted to copy this function from this previous thread and use for my data. However, I run into the following error message after I pass the function to R.:

Error in array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x), : 'data' must be of a vector type, was 'NULL'

I can get the function to work for the dune dataset provided with the Vegan package but have hit a roadblock with my own dataset. Anyone have any thoughts or ideas? I have provided a link below to my dataset and have pasted my code below as well.

The data can be seen: https://docs.google.com/spreadsheets/d/1O2YCapLaCMlCco3-0mZ07_KoJPTfWdjWYudLvMd183Y/edit?usp=sharing

Code:

# rm(list=ls(all=T))
setwd('C:')
library(vegan)
library(ggplot2)

PCBprop<-read.csv("PCBprop_stackoverflow.csv", header=T)
#Subset to just Lake Michigan Observation
MICH<-PCBprop[with(PCBprop, BASIN=="MICHIGAN"),]
#MICHIGAN
michcovariate<-MICH[,c(1,2)]#covariate data
michcongener<- MICH[,3:60]# PCB congener data
michpcbnmds <- metaMDS(michcongener, k = 2, distance ='bray', autotransform = TRUE, trymax=500) 
score<-scores(michpcbnmds)
nmdsscores<-data.frame(cbind(michcovariate,score))
plot(michpcbnmds$points, col = nmdsscores$CAT2) #Basic ordination plot from Vegan
ord<-ordiellipse(michpcbnmds, nmdsscores$CAT2, display = "sites", kind = "se", conf = 0.95, label        = T) #95% ellipses on plot

#Ordiellipse Graph
NMDS = data.frame(NMDS1 = nmdsscores$NMDS1, NMDS2=nmdsscores$NMDS2, group=nmdsscores$CAT2)#sets up data frame 
df_ell <- data.frame()
for(g in levels(NMDS$group)){
df_ell <- rbind(df_ell, cbind(as.data.frame(with(NMDS[NMDS$group==g,],
                                                  vegan:::veganCovEllipse(ord[[g]]$cov,ord[[g]]$center,ord[[g]]$scale))) ,group=g))}



g1<-ggplot(data = NMDS, aes(NMDS1, NMDS2)) + geom_point(aes(color = group), size=1.5) +
geom_path(data=df_ell, aes(x=NMDS1, y=NMDS2,colour=group), size=1.5, linetype=2) +
scale_colour_brewer(palette="Dark2", name="Species_Location")

解决方案

Like this?

The problem is that ord[[g]] = NULL for one of the groups (BND_NO). This is probably because there are only 2 rows of data for that group when BASIN==MICHIGAN, and that is not sufficient to generate a 95% confidence ellipse.

I added the following line at the beginning of your loop:

if(is.null(ord[[g]])) next

to generate the plot above.

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

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