将 for 循环的输出添加到 data.frame 中,这样我就可以在 r 中使用 ggplot2 绘制它们
Adding outputs from a for loop into a data.frame so i can graph them with ggplot2 in r
我想在一致的时间段后计算猎物-捕食者系统的数量,为此我已经根据这个公式创建了我的代码:
Formula
r中的代码:
matrixA <- matrix(c(0.4,-0.5,0.3,1.2),nrow = 2) #population change matrix
basepop <- matrix(c(10,30),nrow = 2) #base population
plot(basepop[1,1],basepop[2,1])
for(k in 1:50){
population <- matrixA %*% basepop #population[1,2] - predator
basepop <- population #population[2,1] - prey
print(population)
}
现在我只想将此循环的结果添加到数据框中,其中 x 坐标表示捕食者的数量,y 坐标表示猎物的数量。
您可以将人口矩阵的每次迭代添加到数据框中进行绘图:
matrixA <- matrix(c(0.4,-0.5,0.3,1.2),nrow = 2) #population change matrix
basepop <- matrix(c(10,30),nrow = 2) #base population
plot(basepop[1,1],basepop[2,1])
continued.df <- data.frame("predator" = rep(NA, times = 50), "prey" = rep(NA, times = 50))
for(k in 1:50){
population <- matrixA %*% basepop #population[1,2] - predator
basepop <- population #population[2,1] - prey
print(population)
continued.df$prey[k] <- population[2]
continued.df$predator[k] <- population[1]
}
plot(continued.df)
然后绘制 continued.df
会给你 x
上的捕食者和 y
上的猎物(假设我理解正确:猎物数量从 30 开始,捕食者在 10.
与@JustinCocco 的回答类似,但有点不同。
- 我将 population 设为列数等于模拟周期数的矩阵
- 我在完成模拟后转换为数据帧。
matrixA <- matrix(c(0.4,-0.5,0.3,1.2),nrow = 2) #population change matrix
basepop <- matrix(c(10,30),nrow = 2) #base population
# Init matrix
population <- matrix(NA, nrow = 2, ncol = 50)
population[,1] <- basepop
for(k in 2:50) {
population[,k] <- matrixA %*% population[,k-1]
}
# Make dataframe
population_df <- data.frame(k = seq(ncol(population)), predator = population[1,], prey = population[2,])
# Plot
library(ggplot2)
ggplot(population_df, aes(predator, prey, color = k)) +
geom_point()
我想在一致的时间段后计算猎物-捕食者系统的数量,为此我已经根据这个公式创建了我的代码: Formula
r中的代码:
matrixA <- matrix(c(0.4,-0.5,0.3,1.2),nrow = 2) #population change matrix
basepop <- matrix(c(10,30),nrow = 2) #base population
plot(basepop[1,1],basepop[2,1])
for(k in 1:50){
population <- matrixA %*% basepop #population[1,2] - predator
basepop <- population #population[2,1] - prey
print(population)
}
现在我只想将此循环的结果添加到数据框中,其中 x 坐标表示捕食者的数量,y 坐标表示猎物的数量。
您可以将人口矩阵的每次迭代添加到数据框中进行绘图:
matrixA <- matrix(c(0.4,-0.5,0.3,1.2),nrow = 2) #population change matrix
basepop <- matrix(c(10,30),nrow = 2) #base population
plot(basepop[1,1],basepop[2,1])
continued.df <- data.frame("predator" = rep(NA, times = 50), "prey" = rep(NA, times = 50))
for(k in 1:50){
population <- matrixA %*% basepop #population[1,2] - predator
basepop <- population #population[2,1] - prey
print(population)
continued.df$prey[k] <- population[2]
continued.df$predator[k] <- population[1]
}
plot(continued.df)
然后绘制 continued.df
会给你 x
上的捕食者和 y
上的猎物(假设我理解正确:猎物数量从 30 开始,捕食者在 10.
与@JustinCocco 的回答类似,但有点不同。
- 我将 population 设为列数等于模拟周期数的矩阵
- 我在完成模拟后转换为数据帧。
matrixA <- matrix(c(0.4,-0.5,0.3,1.2),nrow = 2) #population change matrix
basepop <- matrix(c(10,30),nrow = 2) #base population
# Init matrix
population <- matrix(NA, nrow = 2, ncol = 50)
population[,1] <- basepop
for(k in 2:50) {
population[,k] <- matrixA %*% population[,k-1]
}
# Make dataframe
population_df <- data.frame(k = seq(ncol(population)), predator = population[1,], prey = population[2,])
# Plot
library(ggplot2)
ggplot(population_df, aes(predator, prey, color = k)) +
geom_point()