joey711 / phyloseq

phyloseq is a set of classes, wrappers, and tools (in R) to make it easier to import, store, and analyze phylogenetic sequencing data; and to reproducibly share that data and analysis with others. See the phyloseq front page:
http://joey711.github.io/phyloseq/
579 stars 188 forks source link

Convex hull for Bray Curtis PCoA in phyloseq #1198

Open elisemillar opened 5 years ago

elisemillar commented 5 years ago

I would like to make a convex hull connecting all outermost points for a single Location as well as fill by Location. Here is what I have so far:

# Transform data to proportions for Bray-Curtis distances
bray.dat <- transform_sample_counts(dat, function(otu) otu/sum(otu))

# Data ordination
bray_ordinate = ordinate(bray.dat, method = "PCoA", distance = "bray")

# Plot Bray-Curtis
bray_plot = plot_ordination(bray.dat, bray_ordinate, color="Location", title="Bray Curtis", axes = c(1,2))
bray_plot

Bray Curtis

I have tried a method by joey711 in an earlier post, but this produces random jagged looking shapes instead of a clean polygon. So close!

bray_plot + geom_polygon(aes(fill=Location, alpha = 0.2))

Convex hull

I have also tried using the chull function but am having issues with this. Maybe someone can see where I am going wrong. I am not sure where to use the phyloseq object vs the ordination in this code or whether I need something else entirely since neither are dataframes.

find_hull <- function(df) df[chull(df$Axis.1, df$Axis.2), ]
library(plyr)
hulls <- ddply(somedata, "Location", find_hull)

plot + geom_polygon(data = hulls, alpha = 0.2)

Thanks for your help!

yzhong005 commented 2 years ago

try this one: L<-unique(df$Location) for (i in length(L) {S<-df[df$Location == L[i], ][chull(df[df$Location == L[i], c("Axis.1", "Axis.2")]), ] S<-rbind(NULL,S)}