群落密度制约:寻找圆盘和圆环内的点

在基于个体水平的样地数据分析中,有时候需要找到某一个个体周围的所有个体。特别是在进行密度制约相应研究时,寻找这样的点就更为重要。

为了减少运算量,我们需要先找出某一个点上下左右(各变化所给半径后)所围成的正方形内的所有物种,再基于此小数据集,计算每个个体周围的个体数量。下面是给出两个函数,分别计算从每个个体开始,所给半径的圆内,所有出现个体的ID,以及所给圆环内,所有出现个体的ID.

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
## Individuals within the round plate given the radius from each individual

round.id <- function (dat, r = 20){
gx <- dat$gx
gy <- dat$gy
id.list <- list() for (i in 1:nrow(dat)){
x <- gx[i]
y <- gy[i]
dat.sub <- dat[(gx < x + r) &
(gx > x - r) &
(gy < y + r) &
(gy > y - r), ]
if(nrow(dat.sub) > 1){
sp <- c()
for(j in 1:nrow(dat.sub)){
mat <- data.frame(c(x, dat.sub$gx[j]),
c(y, dat.sub$gy[j]))
sp[j] <- (dist(mat) <= r)
}
id.include <- dat.sub$ID[sp]
} else {
id.include <- 0
}
id.list[[i]] <- id.include
}
return(id.list)
}

####

Ringring.id <- function (dat, r = c(20, 25)){
r1 <- max(r)
r2 <- min(r)
gx <- dat$gx
gy <- dat$gy
id.list <- list()

for (i in 1:nrow(dat)){
x <- gx[i]
y <- gy[i]
dat.sub <- dat[(gx < x + r1) &
(gx > x - r1) &
(gy < y + r1) &
(gy > y - r1), ]

if(nrow(dat.sub) > 1){
sp <- c()
for(j in 1:nrow(dat.sub)){
mat <- data.frame(c(x, dat.sub$gx[j]),
c(y, dat.sub$gy[j]))
sp[j] <- (dist(mat) <= r1 & dist(mat) >= r2)
}
id.include <- dat.sub$ID[sp]
} else {
id.include <- 0
}
id.list[[i]] <- id.include
}
return(id.list)
}
### Example
ID <- 1:300
gx <- runif(300, 0, 100)
gy <- runif(300, 0, 100)
dat <- data.frame(ID, gx, gy)
res.round <- round.id(dat, r = 25)
par(mfrow = c(1, 2))
### Circle
plot(gy ~ gx, data = dat, col = "gray" )
vt = seq(-pi,pi,by=0.002);
x = 25 * sin(vt) + gx[10];
y = 25 * cos(vt) + gy[10];

lines(y ~ x);

points(gx[10] , gy[10] , pch = 19, col = 2)
round10 <- res.round[[10]]
points(gx[round10], gy[round10], col = "green")

##### Ring

res.ring <- ring.id(dat, c(15, 25))
plot(gy ~ gx, data = dat, col = "gray" )
vt = seq(-pi,pi,by=0.002);
x1 = 25 * sin(vt) + gx[10];
y1 = 25 * cos(vt) + gy[10];

lines(y1 ~ x1);
x2 = 15 * sin(vt) + gx[10];
y2 = 15 * cos(vt) + gy[10];

lines(y2 ~ x2);
points(gx[10] , gy[10] , pch = 19, col = 2)
ring10 <- res.ring[[10]]

points(gx[ring10], gy[ring10], col = "blue")