问题描述
我使用 ggplot2 来创建NMDS图,而不是正常的绘图函数。我想使用素食包中的函数 ordiellipse()在NMDS图中显示组。
Instead of the normal plot function I am using ggplot2 to create NMDS plots. I would like to display groups in the NMDS plot using the function ordiellipse() from the vegan package.
示例数据:
Example data:
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))
如何添加ordiellipse到 ggplot2 ?
How can I add ordiellipse to the NMDS plot created with ggplot2?
创建的NMDS图下方,Didzis Elferts的回答很好。谢谢!不过,我现在有兴趣将以下ordiellipse绘制到由 ggplot2 创建的NMDS图中:
Didzis Elferts' answer below works great. Thank you! However, I am now interested in plotting the following ordiellipse to the NMDS plot created with ggplot2:
ordiellipse(sol,MyMeta $ amt,display =sites,kind =se,conf = 0.95,label = T)
不幸的是,我不太了解 veganCovEllipse 函数如何能够自己调整脚本。
Unfortunately, I don't understand enough about how the veganCovEllipse function works to be able to adjust the script myself.
推荐答案
首先,我将列组添加到您的NMDS数据框。
First of all, I added column group to your NMDS data frame.
NMDS = data.frame(MDS1 = sol$points[,1], MDS2 = sol$points[,2],group=MyMeta$amt)
第二个数据框包含每个组的平均MDS1和MDS2值,它将用于在图上显示组名称
Second data frame contains mean MDS1 and MDS2 values for each group and it will be used to show group names on plot
NMDS.mean=aggregate(NMDS[,1:2],list(group=group),mean)
数据框 df_ell 包含显示省略号的值。它使用隐藏在 vegan 包中的函数 veganCovEllipse 进行计算。这个函数适用于NMDS(组)的每个级别,它还使用 cov.wt 来计算协方差矩阵。
Data frame df_ell contains values to show ellipses. It is calculated with function veganCovEllipse which is hidden in vegan package. This function is applied to each level of NMDS (group) and it uses also function cov.wt to calculate covariance matrix.
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)) }
现在省略号用函数 geom_path()和注释()用于绘制组名。
Now ellipses are plotted with function geom_path() and annotate() used to plot group names.
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)
椭圆绘图的想法是从另一个stackoverflow采用的。
Idea for ellipse plotting was adopted from another stackoverflow question.
首先,将NMDS数据框与组列。
First, make NMDS data frame with group column.
NMDS = data.frame(MDS1 = sol$points[,1], MDS2 = sol$points[,2],group=MyMeta$amt)
接下来,保存函数 ordiellipse( )作为一些对象。
Next, save result of function ordiellipse() as some object.
ord<-ordiellipse(sol, MyMeta$amt, display = "sites", kind = "se", conf = 0.95, label = T)
数据框 df_ell 包含显示椭圆。它再次用隐藏在 vegan 包中的函数 veganCovEllipse 进行计算。这个函数适用于NMDS(组)的每一级,现在它使用存储在 ord object - cov 中的参数, center 和 scale 每个级别。
Data frame df_ell contains values to show ellipses. It is calculated again with function veganCovEllipse which is hidden in vegan package. This function is applied to each level of NMDS (group) and now it uses arguments stored in ord object - cov, center and scale of each level.
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)) }
如前面的例子。至于使用 ordiellipse()的elipses对象的坐标计算,此解决方案将使用您为此函数提供的不同参数。
Plotting is done the same way as in previous example. As for the calculating of coordinates for elipses object of ordiellipse() is used, this solution will work with different parameters you provide for this function.
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)
这篇关于从纯素包中绘制ordiellipse函数到ggplot2中创建的NMDS图上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!