# add Linear Regression Lines to Mantel Test

**URL:** https://forum.qiime2.org/t/add-linear-regression-lines-to-mantel-test/31494
**Category:** Community Contributions
**Created:** [September 24, 2024, 2:35pm UTC](https://forum.qiime2.org/t/add-linear-regression-lines-to-mantel-test/31494 "2024-09-24T14:35:16Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![CarlyRae](https://forum.qiime2.org/user_avatar/forum.qiime2.org/carlyrae/32/12745_2.png) [@CarlyRae](https://forum.qiime2.org/u/CarlyRae)
#### Post date: [September 24, 2024, 2:35pm UTC](https://forum.qiime2.org/t/add-linear-regression-lines-to-mantel-test/31494/1 "2024-09-24T14:35:16Z")

</div>

Response to: [Is there a way to add Linear Regression Lines to Mantel Test?](https://forum.qiime2.org/t/is-there-a-way-to-add-linear-regression-lines-to-mantel-test/28570)

I had to do some searching around and thinking about this and figured out the code and wanted to share. Code is in R.

```r
library(phyloseq)
data(GlobalPatterns)

## 26 samples
GlobalPatterns

sample_data(GlobalPatterns)$X.SampleID

BrayForPlotBac <- distance(GlobalPatterns, method ="bray")
BrayForMantelBac <- as.matrix(distance(GlobalPatterns, method ="jaccard", binary = T))

## Create another matrix, say each row is a different viral species and are counts 
## in each of the 26 samples (columns)
M1<-matrix(sample(0:60,156,replace=TRUE),ncol=26, nrow = 6)
M1

colnames(M1) <- sample_data(GlobalPatterns)$X.SampleID
 

M1_T = otu_table(M1, taxa_are_rows = TRUE)
physeq_vir <- phyloseq(M1_T)

physeq_vir

BrayForPlotVir <- distance(physeq_vir, method ="bray")
BrayForMantelVir <- as.matrix(distance(physeq_vir, method ="bray"))

library(vegan)
mantel(BrayForMantelBac, BrayForMantelVir, method = "pearson", permutations = 10000)

## Not significant, but let's plot!

dataP = data.frame(cbind(BrayForPlotBac, BrayForPlotVir))

library(ggplot2)
p <- ggplot(dataP, aes(BrayForPlotVir, BrayForPlotBac)) +
  geom_point() +theme_bw()+ theme_classic() + 
  stat_smooth(method = lm, se = T, color = "black") + 
  geom_point() + theme(text = element_text(size = 16)) + 
  xlab("\nDistance between pairs in viral ASVs (Bray)") +
  ylab("Distance between pairs in bacterial ASVs (Bray)\n")

p

library(ape)
## Make plots with pc axis 1 of each
## NOTE this removes a lot, a lot of variation in the data as 
## likely PCoA only explains small amount of variation. Can plot to figure that out
## or sure some other code to figure out how much variation explained in 1st axis

p_pcoa <- pcoa(BrayForMantelBac)

str(p_pcoa$vectors)

pc1 <- p_pcoa$vectors[,1]
pc1

p_pcoa2 <- pcoa(BrayForMantelVir)

pc2 <- p_pcoa2$vectors[,1]
pc2

dfPCoA <- cbind(pc1, pc2)

pP <- ggplot(data = dfPCoA, aes(x = pc2, y = pc1)) +theme_bw()+ theme_classic() + 
  stat_smooth(method = lm, se = T, color = "black") + geom_point() + 
  theme(text = element_text(size = 16)) + 
  xlab("\nPCoA 1 viral ASVs") +ylab("PCoA 1 bacterial ASVs\n")

pP

```

---

<div class="post-metadata">

### Author: ![colinbrislawn](https://forum.qiime2.org/user_avatar/forum.qiime2.org/colinbrislawn/32/6221_2.png) [@colinbrislawn](https://forum.qiime2.org/u/colinbrislawn)
#### Post date: [September 24, 2024, 7:35pm UTC](https://forum.qiime2.org/t/add-linear-regression-lines-to-mantel-test/31494/2 "2024-09-24T19:35:38Z")

</div>

Thank you for sharing this, Carly!

(I edited your post to add a code block. If you prefer it another way, feel free to change it back!)
