Adding stat-ellipse to NMDS ordination plots

28 views Asked by At

I am having difficulty adding ellipses around data for my NMDS plot. I'm hoping to have two separate polygon ellipses colour filled with coordinating colours to the site points. When I added stat_ellipse to this, it colors both the same and seems to curl around all the points, not separately. I am not terribly well-versed with NMDS plots so any help is appreciated.

nmds.data_scores %>% 
  ggplot( aes(x=NMDS1, y=NMDS2)) + 
  geom_point(aes(NMDS1, NMDS2, colour = factor(site), shape = factor(Group)), 
             size = 3, alpha=0.7) + 
  geom_text(data = nmds.species_scores, aes(x = NMDS1, y = NMDS2, label = species), alpha = 0) + 
  geom_text(aes(label = site, colour = factor(site)), vjust = 1.7, show.legend = FALSE)  +
  labs(
    title = "Ordination Plot(Years)",
    colour = "Year Communities", 
    shape = "Temp. Grouping") +
  scale_colour_manual(values = years_colors, guide = "none") +  # Remove color legend
  scale_shape_manual(values = years_only$point_shape, name = "Temp. Grouping") +  # Add shape legend
  coord_equal() +
  theme_classic()+ 
  theme(
    panel.background = element_rect(fill = NA, colour = "black", size = 1, linetype = "solid"),
    legend.position = c(0.10, 0.10),
    legend.justification = c(0, 0),
    legend.text = element_text(size = 12),
    legend.title = element_text(size = 10, face = "bold"),
    legend.key = element_blank(),
    legend.box.background = element_rect(color = "black"),
) +
  theme(
    axis.text = element_text(size = 10),
    axis.title = element_text(size=12,color="grey14",face="bold")) +
  theme(plot.title = element_text(color = "#45ADA8", size=15,face="bold",hjust=0.5)) +
          geom_hline(yintercept = 0, linetype = "dashed", color = "grey") +  
          geom_vline(xintercept = 0, linetype = "dashed", color = "grey")+
  annotate("text", x = -1, y = 0.45, 
           label = paste0("stress: ", format(nmds$stress, digits = 4)), hjust = 0)

enter image description here

1

There are 1 answers

0
rw2 On

It seems like you could just use the fill argument of stat_ellipse for this, although it's difficult to tell without example data.

Here's code working with an example dataset, that I think does what you want:

library(ggplot2)
library(vegan)
data(mtcars)

nmds <- metaMDS(mtcars)
nmds.data_scores <- as.data.frame(nmds$points)
nmds.data_scores$cyl <- as.factor(mtcars$cyl)

ggplot(nmds.data_scores, aes(x = MDS1, y = MDS2)) +
  geom_point(aes(color = cyl)) +
  stat_ellipse(aes(fill = cyl, color = cyl), geom = "polygon", alpha = 0.2) +
  theme_classic()

Let me know if this helps, or isn't what you wanted.