-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMantel_correlogram_2205.R
100 lines (85 loc) · 3.57 KB
/
Mantel_correlogram_2205.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#### Run Mantel correlogram to evaluate relationship between geo dist and gen dist ####
########################################################
###### Create Mantel correlograms ######
########################################################
##### Load required packages #####
library(vegan)
library(mmod)
#install_github("jaredhomola/VPLandscapeGenetics")
library(VPLandscapeGenetics)
library(tidyverse)
library(geosphere)
data(VPLandscapeGenetics)
# Read in 2205 genetic data
Data_2205 <- read.genepop("X:/2205_BKT_feral_broodstock_ID/Thometz_scripts/2205_All_63pops.gen",
ncode = 3L,
quiet = FALSE)
# Read in project metadata
Samples_2205 <- read_delim("X:/2205_BKT_feral_broodstock_ID/Thometz_scripts/Samples_2205.csv") %>%
filter(SampleID %in% rownames(Data_2205@tab)) %>%
arrange(match(SampleID, rownames(Data_2205@tab)))
# Fill the pop slots
Data_2205@pop <- as_factor(Samples_2205$WaterbodyName)
##### Calculate genetic distances #####
nei.distances <- pairwise_Gst_Nei(Data_2205, linearized = TRUE)
nei.dist.matrix <- as.matrix(nei.distances)
dimnames(nei.dist.matrix) <- list(1:63, 1:63)
##### Calculate geographic distances #####
Coords <- Samples_2205 %>%
select(Longitude, Latitude) %>%
distinct()
geo.dist.matrix <- distm(Coords, fun = distGeo) %>% as.matrix()
geo.distances <- geo.dist.matrix/1000
dim(geo.distances) <- c(63, 63)
dimnames(geo.distances) <- list(1:63, 1:63)
###### Run test and produce default plot ######
break.pts <- c(0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300, 320, 340, 360)
correlogram <- mantel.correlog(nei.dist.matrix,
D.geo = geo.distances,
mult = "BH",
nperm = 1000,
break.pts = break.pts,
cutoff = TRUE)
# Basic plot
plot(correlogram, alpha = 0.05)
## Assign pch based on corrected p value using a new column
bkt.results <- correlogram$mantel.res %>%
as.data.frame() %>%
drop_na() %>%
rename("p.corrected" = "Pr(corrected)") %>%
mutate(Significance = case_when(p.corrected <= 0.05 ~ "Significant",
.default = "Non-significant"))
##### Publication plot #####
pub.plot <- bkt.results %>%
ggplot(aes(x = class.index, y = Mantel.cor)) +
geom_hline(yintercept = 0,
color = "red") +
geom_line() +
geom_point(aes(fill = as_factor(Significance)),
color = "black",
shape = 21, # shape = 21 is imperative for color and fill to work together
show.legend = FALSE,
size = 3) +
scale_fill_manual(values = c("black", "white")) +
scale_y_continuous(expand = c(0,0),
limits = c(-0.03, 0.08)) +
scale_x_continuous(expand = c(0,0),
limits = c(0, 200)) +
labs(x = "Distance class (km)",
y = "Mantel correlation coefficient") +
theme_classic() +
theme(plot.margin = margin(10, 10, 10, 10))
ggsave(filename = "Mantel_correlation.pdf",
plot = pub.plot,
device = "pdf",
path = "X:/2205_BKT_feral_broodstock_ID/Thometz_scripts/Polished_plots_figures",
height = 4,
width = 5,
units = "in")
ggsave(filename = "Mantel_correlation.png",
plot = pub.plot,
device = "png",
path = "X:/2205_BKT_feral_broodstock_ID/Thometz_scripts/Polished_plots_figures",
height = 4,
width = 5,
units = "in")