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/
586 stars 186 forks source link

Weighted and Unweighted PCoA have the exact same values #1767

Closed goodguynickpt closed 3 months ago

goodguynickpt commented 3 months ago

Hello, everyone.

I am currently running this code:

library(vegan) library(phyloseq) library(ggplot2) library(gridExtra) library(tidyverse)

Function to calculate distances

calculate_distances <- function(ps, method) { distances <- list() distances[["unweighted"]] <- phyloseq::distance(ps, method = method, weighted = FALSE) distances[["weighted"]] <- phyloseq::distance(ps, method = method, weighted = TRUE) return(distances) }

Calculate distances for Bray-Curtis and Jaccard

distances_bray <- calculate_distances(ps, method = "bray") distances_jaccard <- calculate_distances(ps, method = "jaccard")

Perform PCoA for different distance and weighting combinations

ordination_bray_unweighted <- ordinate(ps, method="PCoA", distance=distances_bray[["unweighted"]]) ordination_bray_weighted <- ordinate(ps, method="PCoA", distance=distances_bray[["weighted"]]) ordination_jaccard_unweighted <- ordinate(ps, method="PCoA", distance=distances_jaccard[["unweighted"]]) ordination_jaccard_weighted <- ordinate(ps, method="PCoA", distance=distances_jaccard[["weighted"]])

Create a function for plotting

plot_ordination_with_type <- function(ordination_unweighted, ordination_weighted, type) { p1 <- plot_ordination(ps, ordination_unweighted, color = "Infection") + geom_point() + ggtitle(paste(type, "PCoA - Unweighted"))

p2 <- plot_ordination(ps, ordination_weighted, color = "Infection") + geom_point() + ggtitle(paste(type, "PCoA - Weighted"))

grid.arrange(p1, p2, ncol = 2) }

Create a function to extract PCoA scores and create a table

extract_pcoa_scores <- function(ordination) { scores <- data.frame(ordination$vectors) scores$Sample <- rownames(scores) return(scores) }

Extract PCoA scores and create tables

scores_bray_unweighted <- extract_pcoa_scores(ordination_bray_unweighted) scores_bray_weighted <- extract_pcoa_scores(ordination_bray_weighted) scores_jaccard_unweighted <- extract_pcoa_scores(ordination_jaccard_unweighted) scores_jaccard_weighted <- extract_pcoa_scores(ordination_jaccard_weighted)

Plot Bray-Curtis and Jaccard comparisons

plot_bray <- plot_ordination_with_type(ordination_bray_unweighted, ordination_bray_weighted, "Bray-Curtis") plot_jaccard <- plot_ordination_with_type(ordination_jaccard_unweighted, ordination_jaccard_weighted, "Jaccard")

Display plots

plot_bray plot_jaccard

Print PCoA scores tables

print(scores_bray_unweighted) print(scores_bray_weighted) print(scores_jaccard_unweighted) print(scores_jaccard_weighted)

My objective is to have the weighted and unweighted PCoa, using Bray-Curtis. Whenever I run it, however, I am greeted with this: Rplot01 Rplot02

I have tried picking Bray Curtis, Jaccard and Euclidean... The problem still stands - there is no difference between the weighted and unweighted results.

Am I missing something? I have changed it over and over again, checking my dataset constantly to check if there was anything wrong.

Thank you for your time!