-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19_Gillespie_plots.R
163 lines (139 loc) · 6.77 KB
/
19_Gillespie_plots.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
####################################################################################################################
# Plasmid rumen network analysis
#
# Script 19: Plot results of Gillespie dynamical models
#
#
# Script tested for R version 4.1.1
####################################################################################################################
####################################################################################################################
# SCRIPT SET-UP
####################################################################################################################
# Set working directory to wherever your files are located
# Load the necessary packages:
library(tidyverse)
library(ggplot2)
library(viridis)
# Load data created on the server:
# Central plasmids (created in script 16_Gillespie_central_hpc2.R)
load("sim.df.hi.Rda")
load("sim.df.hi.mean.Rda")
# Peripheral plasmids (created in script 18_Gillespie_peripheral_hpc2.R)
load("sim.df.low.Rda")
load("sim.df.low.mean.Rda")
####################################################################################################################
# Section 1: PLOT FOR CENTRAL PLASMIDS
####################################################################################################################
pd <- position_dodge(0.4)
plot1.high <- ggplot(sim.df.hi.mean, aes(x=time.step, y=mean.gene, color = contact_loss)) +
geom_point(size=1.75, position = pd) +
geom_line(size=1.25, position = pd) +
scale_colour_manual(values = c("#f3ee27","#fad824","#feba2c",
"#f2645c","#de4968","#c03a76",
"#a1307e","#6e1e81","#2f1163"),
name = "",
labels = c("Low contact, no loss", "Low contact, intermediate loss", "Low contact, high loss",
"Intermediate contact, no loss","Intermediate contact, intermediate loss", "Intermediate contact, high loss",
"High contact, no loss","High contact, intermediate loss","High contact, high loss")) +
theme_classic() +
xlab("Time step") +
ylab("Cows with gene (mean)") +
theme(legend.position="bottom",
legend.text=element_text(size=11),
axis.text=element_text(size=12),
axis.title=element_text(size=14)) +
guides(colour = guide_legend(nrow = 3))
# Export
ggsave(plot1.high, filename="plot1.high.png", dpi = 1800, width = 9, height = 6, units = "in")
ggsave(plot1.high, filename="plot1.high.pdf", width = 9, height = 6, units = "in")
####################################################################################################################
####################################################################################################################
# Section 2: PLOT FOR PERIPHERAL PLASMIDS
####################################################################################################################
pd <- position_dodge(0.4)
plot1.low <- ggplot(sim.df.low.mean, aes(x=time.step, y=mean.gene, color = contact_loss)) +
geom_point(size=1.75, position = pd) +
geom_line(size=1.25, position = pd) +
scale_colour_manual(values = c("#f3ee27","#fad824","#feba2c",
"#f2645c","#de4968","#c03a76",
"#a1307e","#6e1e81","#2f1163"),
name = "",
labels = c("Low contact, no loss", "Low contact, intermediate loss", "Low contact, high loss",
"Intermediate contact, no loss","Intermediate contact, intermediate loss", "Intermediate contact, high loss",
"High contact, no loss","High contact, intermediate loss","High contact, high loss")) +
theme_classic() +
xlab("Time step") +
ylab("Cows with gene (mean)") +
theme(legend.position="bottom",
legend.text=element_text(size=11),
axis.text=element_text(size=12),
axis.title=element_text(size=14)) +
guides(colour = guide_legend(nrow = 3))
# Save plots
ggsave(plot1.low, filename="plot1.low.png", dpi = 1800, width = 9, height = 6, units = "in")
ggsave(plot1.low, filename="plot1.low.pdf", width = 9, height = 6, units = "in")
####################################################################################################################
####################################################################################################################
# Section 3: RESULTS OF GILLESPIE DYNAMICAL MODELS
####################################################################################################################
# Time to getting to all 21, central plasmids
last.step.high <- sim.df.hi %>%
filter(with.gene == 21) %>%
ungroup() %>%
group_by(sim.rep, plasmid.rep,contact_loss, .groups=T) %>%
slice_min(time.step)
#slice_head()
#summarise(min.time.step=min(time.step))
# Percent of simulations in which all 21 cows infected with gene, per parameter combination
last.step.high2 <- last.step.high %>%
group_by(contact_loss) %>%
mutate(mean.time.step = mean(time.step),
per.reps.21 = n()/3000*100) %>%
ungroup() %>%
select(contact_loss, mean.time.step, per.reps.21) %>%
distinct()
# Time-step at which gene reaches all 21 cow, per simulation, starting plasmid, and parameter combination
last.step.high <- sim.df.hi %>%
filter(with.gene == 21) %>%
ungroup() %>%
group_by(sim.rep, plasmid.rep,contact_loss, .groups=T) %>%
slice_min(time.step)
#slice_head()
#summarise(min.time.step=min(time.step))
# Repeat for peripheral plasmids
# Time to getting to all 21, central plasmids
last.step.low <- sim.df.low %>%
filter(with.gene == 21) %>%
ungroup() %>%
group_by(sim.rep, plasmid.rep,contact_loss, .groups=T) %>%
slice_min(time.step)
last.step.low.not21 <- sim.df.low %>%
filter(with.gene != 21) %>%
ungroup() %>%
group_by(sim.rep, plasmid.rep,contact_loss, .groups=T) %>%
slice_max(time.step)
# Percent of simulations in which all 21 cows infected with gene, per parameter combination
last.step.low2 <- last.step.low %>%
group_by(contact_loss) %>%
mutate(mean.time.step = mean(time.step),
per.reps.21 = n()/3000*100) %>%
ungroup() %>%
select(contact_loss, mean.time.step, per.reps.21) %>%
distinct()
# Mean number of cows with gene at the last step
last.step.low3 <- sim.df.low %>%
filter(time.step == 300) %>%
group_by(contact_loss) %>%
mutate(mean.inf.300 = mean(with.gene))%>%
select(contact_loss, mean.inf.300) %>%
distinct()
# Max number of cows with gene in simulations where gene did not reach all 21 cows
last.step.low.under21 <- sim.df.low %>%
filter(with.gene != 21) %>%
ungroup() %>%
group_by(sim.rep, plasmid.rep, .groups=T) %>%
slice_max(time.step)
last.step.infected <- sim.df.low %>%
ungroup() %>%
group_by(sim.rep, plasmid.rep, .group=T) %>%
slice_max(time.step)