Creating Convex Hulls

I've created an NMDS plot using vegan but I'm having trouble adding convex hulls. I need three hulls: one for Decatur (D610F, D15F, D15B, etc.), one for Hickory Road (HR610F, HR15F, HR15B, etc.), and one for Cedartown (C610F, C15F, C15B, etc.). Any help would be greatly appreciated. I'll add the codes I've used below along with a screenshot of my graph. Thank you in advance.

#NMDS analyses
WD <- "C:/Users/jacob/OneDrive/Documents/R/win-library/3.6/vegan/data"
setwd(WD)
require(vegan)
phylum.dat <- read.csv("Phylum data for R.csv", stringsAsFactors = T, header = T, row.names = 1)
diversity(phylum.dat, index = "shannon")
specnumber(phylum.dat) #species number
beta <- vegdist(phylum.dat, "jaccard", binary = TRUE) #jaccard distance
mean(beta)
dist(beta) #distance matrix
NMDS <- metaMDS(beta, distance = "jaccard", k = 2)
plot(NMDS, display = "sites", type = "text") #adds labels to plot

image

Please see the FAQ: What's a reproducible example (`reprex`) and how do I create one? Using a reprex, complete with representative data will attract quicker and more answers.

Here's an example of convex hull construction from this S/O post

library(tidyverse)

# Find the convex hull of the points being plotted
hull <- mtcars %>%
  slice(chull(mpg, wt))

# Define the scatterplot
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point(shape = 21)

# Overlay the convex hull
p + geom_polygon(data = hull, alpha = 0.5)

Created on 2020-03-25 by the reprex package (v0.3.0)

Update: I've created the convex hulls with different colored coordinates but I can't seem to color them in.

#convex hull
treat=c(rep("Treatment1",4),rep("Treatment2",4),rep("Treatment3",4))
ordihull(NMDS,groups=treat,draw="polygon",col="grey90",label=F)
orditorp(NMDS, display = "sites", col = c(rep("red",4), rep("blue",4), rep("green",4)))

You need to follow the link @technocrat posted and add a reprex if you want people to help you.

Having said that, there's also ggforce which might be useful: https://www.data-imaginist.com/2019/the-ggforce-awakens-again/

1 Like

Reading just a bit further down the link

suppressPackageStartupMessages(library(dplyr)) 
library(ggplot2)

hull_cyl <- mtcars %>%
  group_by(cyl) %>%
  slice(chull(mpg, wt))

# Define the scatterplot
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point(shape = 21)

# Update the plot with a fill group, and overlay the new hulls

p + aes(fill = factor(cyl)) + geom_polygon(data = hull_cyl, alpha = 0.5)

Created on 2020-03-25 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.