0

I created an NMDS plot using ggplot with ellipses corresponding to my different groups (I have three years and two treatments; different treatment have different ellipse outline and symbol while different years have different ellipse color and symbol color).

Here is my code:

  ggplot(nmds, aes(x=nmds1, y=nmds2, col=group)) +
      geom_point(aes(shape=group)) +
      stat_ellipse(aes(x = nmds1,y=nmds2, fill=group, linetype=group), geom="polygon", alpha=0.2, segments=201) +
      scale_linetype_manual(values=c(1,2,1,2,1,2)) +        scale_fill_manual(values=c("maroon","maroon","steelblue2","steelblue2","seagreen", "seagreen")) +  scale_colour_manual(values=c("maroon","maroon","steelblue2","steelblue2","seagreen", "seagreen")) +
      scale_shape_manual(values=c(1,2,1,2,1,2))

I want to see if my three ellipses for one treatment are significantly smaller than the three ellipses for the other treatment. How can I compute their area? I could then use a t-test to test for significant difference.

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
mathz
  • 1
  • 2
  • 3
    Hi and welcome to SO! It would help if you provide an example that is reproducible (this page might help: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Your code is not reproducible, since we don't know how `nmds` looks like. – Qaswed Aug 06 '19 at 11:04
  • 1
    Calculating the areas of the ellipses won't let you run a t-test. Maybe use `vegan::betadisper` to test your hypothesis more directly – Richard Telford Aug 06 '19 at 15:13
  • I got p=0.04 so significant at 5%. Is that enough to "prove" smaller ellipses in one of the treatments? Also, do you know how this could ecologically be interpreted? – mathz Aug 07 '19 at 12:03

1 Answers1

0

This is what I used to separate samples by their ellipse colour, but it should be pretty easy to adapt it to work for a combination of colour and shape as well. Just adjust the subset option by adding another nested loop that selects for unique shapes.

This code is based on the answer from here: How to calculate the area of ellipse drawn by ggplot2?

# Get ellipse coordinates from plot
pb = ggplot_build(data)
el = pb$data[[2]][c("colour", 
                    #"shape",
                    "x",
                    "y")] #If you separated by shape as well then include it here as well and then separate by shape as well

#Make an empty dataframe where your data is going to end up
ellipseAreaList = data.frame()

#Identify unique groups (in my case colour)
group_list = unique(el$colour)

#Start the loop
for (i in 1:length(group_list)) {

  #Subset to separate into individual ellipse groups - if you used shape in addition to colour you'll want to change this as well
  Object = subset(el, 
                  colour == group_list[i])

  #Remove grouping column
  Object = Object[-1]

  # Center of ellipse
  ctr = MASS::cov.trob(Object)$center  

  # Calculate distance to center from each point on the ellipse
  dist2center <- sqrt(rowSums((t(t(Object)-ctr))^2))

  # Calculate area of ellipse from semi-major and semi-minor axes. 
  # These are, respectively, the largest and smallest values of dist2center. 
  value = pi*min(dist2center)*max(dist2center)

  #Store in the area list
  ellipseAreaList = rbind(ellipseAreaList, data.frame(group_list[i], value))
}