而不是我ggplot2用来创建NMDS图的正常绘图功能.我想在使用功能的NMDS阴谋显示组ordiellipse()从vegan包.
示例数据:
library(vegan)
library(ggplot2)
data(dune)
# calculate distance for NMDS
sol <- metaMDS(dune)
# 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)
# draw dispersion ellipses around data points
ordiellipse(sol, MyMeta$amt, display = "sites", kind = "sd", label = T)
# 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))
Run Code Online (Sandbox Code Playgroud)
如何将ordiellipse添加到使用ggplot2?创建的NMDS图中?
Didzis Elferts的回答非常有用.谢谢!但是,我现在有兴趣将以下ordiellipse绘制到创建的NMDS图ggplot2:
ordiellipse(sol, MyMeta$amt, display = "sites", kind = "se", conf = 0.95, label = T)
不幸的是,我不太了解该veganCovEllipse功能如何能够自己调整脚本.
首先,我将列组添加到您的NMDS数据框中.
NMDS = data.frame(MDS1 = sol$points[,1], MDS2 = sol$points[,2],group=MyMeta$amt)
Run Code Online (Sandbox Code Playgroud)
第二个数据框包含每个组的平均MDS1和MDS2值,它将用于在图上显示组名
NMDS.mean=aggregate(NMDS[,1:2],list(group=group),mean)
Run Code Online (Sandbox Code Playgroud)
数据框df_ell包含显示省略号的值.它是用veganCovEllipse隐藏在vegan包中的功能计算的.此功能应用于NMDS(组)的每个级别,并且还使用函数cov.wt来计算协方差矩阵.
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))
}
Run Code Online (Sandbox Code Playgroud)
现在用函数绘制椭圆,geom_path()并annotate()用于绘制组名.
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)
Run Code Online (Sandbox Code Playgroud)
椭圆绘图的想法是从另一个stackoverflow 问题中采用的.

首先,使用组列制作NMDS数据框.
NMDS = data.frame(MDS1 = sol$points[,1], MDS2 = sol$points[,2],group=MyMeta$amt)
Run Code Online (Sandbox Code Playgroud)
接下来,将函数的结果保存ordiellipse()为某个对象.
ord<-ordiellipse(sol, MyMeta$amt, display = "sites",
kind = "se", conf = 0.95, label = T)
Run Code Online (Sandbox Code Playgroud)
数据框df_ell包含显示省略号的值.它再次使用veganCovEllipse隐藏在vegan包中的功能进行计算.该函数被施加到NMDS(组)的每个电平,现在,它使用存储在参数ord对象- cov,center和scale每个级别的.
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))
}
Run Code Online (Sandbox Code Playgroud)
绘图的方式与前面的例子相同.至于ordiellipse()使用elipses对象的坐标计算,此解决方案将使用您为此功能提供的不同参数.
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)
Run Code Online (Sandbox Code Playgroud)
