Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8973301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:24:53+00:00 2026-06-15T18:24:53+00:00

Instead of the normal plot function I am using ggplot2 to create NMDS plots.

  • 0

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))

How can I add ordiellipse to the NMDS plot created with ggplot2?

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)

Unfortunately, I don’t understand enough about how the veganCovEllipse function works to be able to adjust the script myself.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:24:54+00:00Added an answer on June 15, 2026 at 6:24 pm

    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)
    

    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)
    

    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))
      }
    

    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)
    

    Idea for ellipse plotting was adopted from another stackoverflow question.

    enter image description here

    UPDATE – solution that works in both cases

    First, make NMDS data frame with group column.

    NMDS = data.frame(MDS1 = sol$points[,1], MDS2 = sol$points[,2],group=MyMeta$amt)
    

    Next, save result of function ordiellipse() as some object.

    ord<-ordiellipse(sol, MyMeta$amt, display = "sites", 
                       kind = "se", conf = 0.95, label = T)
    

    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))
    }
    

    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)
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Instead of having nested callbacks in JS, I would like to fire and listen
I'm looking to change several images onclick using JQuery instead of normal JavaScript and
I have a simple question that whether only using pointers instead of normal variables
I am plotting data from an aircraft on a map and I would like
In the following code, the main function uses normal function instead of Template function.
Is there any difference in using Generic HttpHandler(ashx) instead of normal aspx Page for
How do I read value from INI file without using sections? So instead of
When shall i use malloc instead of normal array definition in C? I can't
i know that django uses unicode strings all over the framework instead of normal
Im thinking about to use a DB for my logs instead of a normal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.