用ggplot2从纯素包中绘制ordiellipse。

时间:2022-01-31 23:45:36

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

我想用ggplot2做一个NMDS排序图,以获取一些我正在工作的数据。这里有一个很好的例子,说明了如何从以前的堆栈溢出线程中完成这个任务:从纯素食包中绘制ordiellipse函数,到在ggplot2中创建的NMDS绘图。

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.:

我试图从以前的线程中复制这个函数,并使用我的数据。但是,在将函数传递给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'

数组中的错误(x, c(长度(x), 1L), if (!is.null(名称(x)))列表(名称(x),“数据”必须是向量类型,为“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.

我可以让这个函数在纯素包提供的dune数据集上工作,但却遇到了我自己的数据集的路障。大家有什么想法或想法吗?我已经为我的数据集提供了一个链接,并粘贴了下面的代码。

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

数据可以被看到:https://docs.google.com/spreadsheets/d/1o2ycaplacmlcco3 - 0mz07_kojptfwdjwyudlvmd183y/edit?

Code:

代码:

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

PCBprop<-read.csv("PCBprop_*.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")

1 个解决方案

#1


1  

Like this?

像这样的吗?

用ggplot2从纯素包中绘制ordiellipse。

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.

问题是,对于其中一个组(BND_NO), ord[[g]] = NULL。这可能是因为当盆地==密西根时,该组只有2行数据,而这还不足以产生95%的置信椭圆。

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

我在您的循环开始时添加了以下一行:

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

to generate the plot above.

生成上面的图。

#1


1  

Like this?

像这样的吗?

用ggplot2从纯素包中绘制ordiellipse。

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.

问题是,对于其中一个组(BND_NO), ord[[g]] = NULL。这可能是因为当盆地==密西根时,该组只有2行数据,而这还不足以产生95%的置信椭圆。

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

我在您的循环开始时添加了以下一行:

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

to generate the plot above.

生成上面的图。