-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPractice 6.2 3.0.Rmd
78 lines (60 loc) · 1.85 KB
/
Practice 6.2 3.0.Rmd
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
---
title: "Practice 6.2 3.0"
author: "rpp"
date: "2023-10-30"
output: html_document
---
```{r setup}
library(animation)
library(ggplot2)
library (gganimate)
growhw <- function(growth.rateN1, growth.rateN2, plot_N1 = FALSE, plot_N2 = FALSE) {
num_gen <- 30
N1 <- rep(0, num_gen)
N2 <- rep(0, num_gen)
generation <- 1:num_gen
N1[1] <- 1
N2[1] <- 1
K1 <- 150
K2 <- 180
a12 <- 2
a21 <- 2
for (i in 2:num_gen) {
# N[i] = N[i-1]+ (growth.rate*N[i-1]*((100-N[i-1])/100)){
N1[i] = N1[i - 1] + (growth.rateN1 * N1[i - 1] * ((K1 - N1[i - 1] - a12 * N2[i - 1]) / K1))
N2[i] = N2[i - 1] + (growth.rateN2 * N2[i - 1] * ((K2 - N2[i - 1] - a21 * N2[i - 1]) / K2))
}
if (plot_N1) {
plot(generation, N1, xlim=c(0,30), ylim=c(0,180), type = 'b',
main = paste("Rate =", growth.rateN1, "- N1"), xlab = "Generation", ylab = "N1")
}
if (plot_N2) {
plot(generation, N2, xlim=c(0,30), ylim=c(0,180), type = 'b',
main = paste("Rate =", growth.rateN2, "- N2"), xlab = "Generation", ylab = "N2", col = "red")
}
}
#plot Species 1 alone
growhw(1,0, plot_N1 = TRUE)
text(4,150,"Species 1 alone")
#plot Species 2 alone, but the population reached saturation before reaching K2
growhw(0,1, plot_N2 = TRUE)
text(4,150,"Species 2 alone")
#plot Both Species competing, but the plot only show one line at a time
growhw(1,1, plot_N1 = TRUE,plot_N2 = TRUE)
text(8,150,"Both Species competing")
saveGIF({
for (i in seq(0.1, 0.5, by = 2)){
growhw(i,0, plot_N1 = TRUE) # 生成 N1 的 GIF
}
}, movie.name = "N1.gif")
saveGIF({
for (i in seq(0.1, 0.5, by = 2)){
growhw(0,i, plot_N2 = TRUE) # 生成 N2 的 GIF
}
}, movie.name = "N2.gif")
#two lines didn't show at the same time
saveGIF({
for (i in seq(0.1, 0.5, by = 2)){
growhw(i,i, plot_N1 = TRUE, plot_N2 = TRUE)
}
}, movie.name = "N1_and_N2.gif")