-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFig4_chaos_analysis.R
140 lines (129 loc) · 5.31 KB
/
Fig4_chaos_analysis.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
###############################################################################
### Figure 4 - Spatiotemporal correlations and chaos ##########################
###############################################################################
# Nick Lowery and Tristan Ursell
# 2018
#
# This script generates Figure 4 from our preprint:
# Structured environments fundamentally alter dynamics and stability of ecological communities
# https://www.biorxiv.org/content/early/2018/07/10/366559
#
# The script reads in correlation and metadata files generated from the LV_competition_chaos.m
# and chaos_spatial_correlation_function.m scripts. The functions to generate the data from the
# raw .mat data files are included but commented out.
###############################################################################
# load required libraries
#library(R.matlab)
library(tidyverse)
library(cowplot)
library(colorspace)
# # read in raw data files
# chaos.corr.raw <- list.files("E:/ecological_modelling/3sp_chaos_robust_cor", pattern = ".mat", full.names = T) %>%
# lapply(., readMat)
#
# chaos.metadata.raw <- list.files("E:/ecological_modelling/3sp_chaos_robust_metadata", pattern = ".mat", full.names = T) %>%
# lapply(., readMat)
#
# # organize into tbl
# chaos.corr <- chaos.corr.raw %>% {
# tibble(
# L = map_dbl(., "L"),
# R = map_dbl(., "R"),
# dx = map_dbl(., "dx"),
# dinit = map_dbl(., "dinit"),
# cor.mat = map(., "cor.mat"),
# cor.mean = map(., "cor.mean"),
# cor.sd = map(., "cor.sd")
# )
# } %>%
# filter(dinit %in% c(0.1, 0.01, 0.001, 0.0001)) %>%
# select(-cor.mat) %>%
# unnest() %>%
# group_by(R, dx, dinit) %>%
# mutate(t.step = seq_along(cor.mean))
#
# chaos.metadata <- chaos.metadata.raw %>% {
# tibble(
# P = map_dbl(., "P"),
# pillars = map_dbl(., "pillarq"),
# R = map_dbl(., "R"),
# dx = map_dbl(., "dx"),
# dinit = map_dbl(., "dinit"),
# rep = map_dbl(., "rep"),
# data = map(., `[`, c("meanA.out", "meanB.out", "meanC.out")) %>% map(data.frame)
# )
# } %>%
# unnest() %>%
# rename(A = meanA.out, B = meanB.out, C = meanC.out) %>%
# group_by(R, dx, dinit, rep) %>%
# mutate(t.step = seq_along(A))
#
# # write files for later use
# write_csv(chaos.corr, "3sp_chaos_corr.csv")
# write_csv(chaos.metadata, "3sp_chaos_metadata.csv")
# read in proccessed data files
chaos.corr <- read_csv("3sp_chaos_corr.csv")
chaos.metadata <- read_csv("3sp_chaos_metadata.csv")
pillar.weights <- read_csv("filt_all_weights.csv", col_names = c("L","R","dx","pillar.weight")) %>%
mutate(dx = round(dx*R))
# calculate t to extinction
chaos.ext <- chaos.metadata %>%
left_join(pillar.weights) %>%
mutate(extinct = ifelse(A <= ((2*R)^2 - pi*R^2)/4/pillar.weight |
B <= ((2*R)^2 - pi*R^2)/4/pillar.weight |
C <= ((2*R)^2 - pi*R^2)/4/pillar.weight , 1, 0)) %>%
group_by(R,dx,dinit,rep) %>%
summarise(extinction = max(extinct),
t.extinct = ifelse(max(extinct), which.max(extinct), NA)) %>%
group_by(R,dx,dinit) %>%
summarise(t.stop = floor(min(t.extinct, na.rm = T)/5))
# prep data for plotting
chaos.corr.plot <- chaos.corr %>%
ungroup() %>%
left_join(chaos.ext) %>%
filter(t.step <= t.stop) %>%
mutate(R = round(R/sqrt(15), digits = 2),
R = fct_relevel(factor(R), "0", after = Inf),
dinit = 100*dinit)
# color palette (function generated using choose_palette() GUI from colorspace package)
pal <- function (n, h = c(300, 75), c. = c(35, 95), l = c(15, 90),
power = c(0.8, 1.2), fixup = TRUE, gamma = NULL, alpha = 1,
...)
{
if (!is.null(gamma))
warning("'gamma' is deprecated and has no effect")
if (n < 1L)
return(character(0L))
h <- rep(h, length.out = 2L)
c <- rep(c., length.out = 2L)
l <- rep(l, length.out = 2L)
power <- rep(power, length.out = 2L)
rval <- seq(1, 0, length = n)
rval <- hex(polarLUV(L = l[2L] - diff(l) * rval^power[2L],
C = c[2L] - diff(c) * rval^power[1L], H = h[2L] - diff(h) *
rval), fixup = fixup, ...)
if (!missing(alpha)) {
alpha <- pmax(pmin(alpha, 1), 0)
alpha <- format(as.hexmode(round(alpha * 255 + 1e-04)),
width = 2L, upper.case = TRUE)
rval <- paste(rval, alpha, sep = "")
}
return(rval)
}
# generate plot
chaos.corr.plot %>%
ggplot(aes(x = t.step, y = cor.mean, color = as.factor(dinit), group = dinit)) +
geom_hline(yintercept = 0, linetype = "dashed", size = 1.5, color = "grey50") +
geom_ribbon(aes(ymin = cor.mean - cor.sd/sqrt(10),
ymax = cor.mean + cor.sd/sqrt(10),
fill = as.factor(dinit), color = NULL), alpha = 0.4) +
geom_line(size = 1, lineend = "round") +
facet_wrap(~ R) +
scale_color_manual(values = pal(4)) +
scale_fill_manual(values = pal(4)) +
labs(x = "time step (doubling times)",
y = "mean pairwise correlation",
color = paste("percentage of","reassigned","pixels at t = 0", sep = "\n"),
fill = paste("percentage of","reassigned","pixels at t = 0", sep = "\n"),
title = expression(paste("pillar radius ( R / ", lambda, ")"))) +
theme(plot.title = element_text(size = 14, face = "plain"))