我正在尝试做一些类似于旧帖子的事情:绘图 - 原始帖子
在我的分析中,我感兴趣的是不同的哺乳动物宿主是否有不同的跳蚤群落。我链接到的原始帖子有两种不同的椭圆解决方案。我的问题是,当我运行第一个解决方案和通用解决方案时,我得到的图看起来截然不同,但我认为它们应该非常相似。下面是我的代码。
我的问题是:我是否做错了什么或者哪个代码产生了正确的数字?或者我应该使用更好的新代码来显示宿主物种跳蚤群落的差异?
谢谢,
阿曼达
Link to ARG_comm数据
链接到ARG_env data
library(vegan)
library(BiodiversityR)
library(MASS)
comm_mat <- read.csv("d:/fleaID/ARG_comm.csv",header=TRUE)
env <- read.csv("d:/fleaID/ARG_env.csv",header=TRUE)
library(ggplot2)
sol <-metaMDS(comm_mat)
MyMeta=env
#originalresponse
NMDS = data.frame(MDS1 = sol$points[,1], MDS2=sol$points[,2],group=MyMeta$host)
NMDS.mean=aggregate(NMDS[,1:2],list(group=NMDS$group),mean)
veganCovEllipse<-function (cov, center = c(0, 0), scale = 1, npoints = 100)
{
theta <- (0:npoints) * 2 * pi/npoints
Circle <- cbind(cos(theta), sin(theta))
t(center + scale * t(Circle %*% chol(cov)))
}
df_ell <- data.frame()
for(g in levels(NMDS$group)){
df_ell <- rbind(df_ell, cbind(as.data.frame(with(NMDS[NMDS$group==g,],
veganCovEllipse(cov.wt(cbind(MDS1,MDS2),wt=rep(1/length(MDS1),length(MDS1)))$cov,center=c(mean(MDS1),mean(MDS2)))))
,group=g))
}
ggplot(data = NMDS, aes(MDS1, MDS2)) + geom_point(aes(color = group)) +
geom_path(data=df_ell, aes(x=MDS1, y=MDS2,colour=group), size=1, linetype=2)+
annotate("text",x=NMDS.mean$MDS1,y=NMDS.mean$MDS2,label=NMDS.mean$group)
#update - can use se (standard error) or sd (standard dev)
#update
NMDS = data.frame(MDS1 = sol$points[,1], MDS2 = sol$points[,2],group=MyMeta$host)
plot.new()
ord<-ordiellipse(sol, MyMeta$host, display = "sites",
kind = "se", conf = 0.95, label = T)
df_ell <- data.frame()
for(g in levels(NMDS$group)){
df_ell <- rbind(df_ell, cbind(as.data.frame(with(NMDS[NMDS$group==g,],
veganCovEllipse(ord[[g]]$cov,ord[[g]]$center,ord[[g]]$scale)))
,group=g))
}
ggplot(data = NMDS, aes(MDS1, MDS2)) + geom_point(aes(color = group)) +
geom_path(data=df_ell, aes(x=NMDS1, y=NMDS2,colour=group), size=1, linetype=2)