Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, I added column group to your NMDS data frame.</p> <pre><code> NMDS = data.frame(MDS1 = sol$points[,1], MDS2 = sol$points[,2],group=MyMeta$amt) </code></pre> <p>Second data frame contains mean MDS1 and MDS2 values for each group and it will be used to show group names on plot</p> <pre><code> NMDS.mean=aggregate(NMDS[,1:2],list(group=group),mean) </code></pre> <p>Data frame <code>df_ell</code> contains values to show ellipses. It is calculated with function <code>veganCovEllipse</code> which is hidden in <code>vegan</code> package. This function is applied to each level of NMDS (group) and it uses also function <code>cov.wt</code> to calculate covariance matrix.</p> <pre><code> veganCovEllipse&lt;-function (cov, center = c(0, 0), scale = 1, npoints = 100) { theta &lt;- (0:npoints) * 2 * pi/npoints Circle &lt;- cbind(cos(theta), sin(theta)) t(center + scale * t(Circle %*% chol(cov))) } df_ell &lt;- data.frame() for(g in levels(NMDS$group)){ df_ell &lt;- 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)) } </code></pre> <p>Now ellipses are plotted with function <code>geom_path()</code> and <code>annotate()</code> used to plot group names.</p> <pre><code> 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) </code></pre> <p>Idea for ellipse plotting was adopted from another stackoverflow <a href="https://stackoverflow.com/questions/2397097/how-can-a-data-ellipse-be-superimposed-on-a-ggplot2-scatterplot">question</a>.</p> <p><img src="https://i.stack.imgur.com/nhva1.jpg" alt="enter image description here"></p> <h2>UPDATE - solution that works in both cases</h2> <p>First, make NMDS data frame with group column.</p> <pre><code>NMDS = data.frame(MDS1 = sol$points[,1], MDS2 = sol$points[,2],group=MyMeta$amt) </code></pre> <p>Next, save result of function <code>ordiellipse()</code> as some object.</p> <pre><code>ord&lt;-ordiellipse(sol, MyMeta$amt, display = "sites", kind = "se", conf = 0.95, label = T) </code></pre> <p>Data frame <code>df_ell</code> contains values to show ellipses. It is calculated again with function <code>veganCovEllipse</code> which is hidden in <code>vegan</code> package. This function is applied to each level of NMDS (group) and now it uses arguments stored in <code>ord</code> object - <code>cov</code>, <code>center</code> and <code>scale</code> of each level.</p> <pre><code>df_ell &lt;- data.frame() for(g in levels(NMDS$group)){ df_ell &lt;- rbind(df_ell, cbind(as.data.frame(with(NMDS[NMDS$group==g,], veganCovEllipse(ord[[g]]$cov,ord[[g]]$center,ord[[g]]$scale))) ,group=g)) } </code></pre> <p>Plotting is done the same way as in previous example. As for the calculating of coordinates for elipses object of <code>ordiellipse()</code> is used, this solution will work with different parameters you provide for this function.</p> <pre><code>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) </code></pre> <p><img src="https://i.stack.imgur.com/Q5NPs.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload