2

好的,所以我使用 par 函数来组合多个图。我添加了传说,这一切都很有趣和游戏,直到有些情节需要与其他情节不同的传说,我无法弄清楚使其工作所需的编程体操。

我已经将我的图例包含在名称“这是我需要帮助的部分”下的部分,如您所见,其中有四个,用于 N1、N2、K1 和 K2,具有 abline 和 text 功能每个。

我试图制作串联列表等,但它的效果不如我的 alpha,而且我花了太长时间研究如何让它工作。

##II - Species Competition : testing different parameter combinations with a loop

#Clearing workspace
rm(list=ls())
graphics.off()

#Parameters set on types (lambda 1, lambda 2, alphas...)

years <- 50
Lambda1set<-c(5,5,3,3.2)
Lambda2set<-c(4,4,5,2.5)
Alphaset<-list(matrix(c(0.01, 0.007, 0.0045, 0.01),ncol = 2, byrow = TRUE),
               matrix(c(0.01, 0.004, 0.009, 0.01),ncol = 2, byrow = TRUE),
               matrix(c(0.01, 0.0065, 0.003, 0.01),ncol = 2, byrow = TRUE),
               matrix(c(0.005, 0.005, 0.0045, 0.003),ncol = 2, byrow = TRUE))

#Creating vectors

TimeVec<-seq(1,years+1)
N1<-matrix(0,4,years+1)
N2<-matrix(0,4,years+1)

#INITIALIZATION
N0 <- 40
##N1[1] <-N0
##N2[1] <-N0
for(i in 1:4){
  N1[i,1] <-N0
  N2[i,1] <-N0
}

for (t in 1:years){
  for(i in 1:4){ 
    alphs <- Alphaset[[i]]

    N1[i,t+1]<-(Lambda1set[i]*N1[i,t])/(1+alphs[1,1]*N1[i,t]+alphs[1,2]*N2[i,t])
    N2[i,t+1]<-(Lambda2set[i]*N2[i,t])/(1+alphs[2,1]*N1[i,t]+alphs[2,2]*N2[i,t])
  }
}

## Plotting
titles<-c("Both species coexist","Species 1 wins","Species 2 wins",
          "Whoever comes first")
par(mfrow=c(2,2))

for(i in 1:4){

  plot(TimeVec,N1[i,],type="l",main = titles[i], xlab='Time (years)',
       ylab='Population Density',col='blue',lwd = 2,ylim = c(-100,500))
  lines(TimeVec,N2[i,],type="l",col='green', lwd = 2)
  legend("topright", legend = c("Species 1", "Species 2"),lty = 1, 
         col = c("blue", "green"),lwd = 2, bty = "n")

  #Adding equilibria

  R1<-(Lambda1set-1)
  R2<-(Lambda2set-1)
  alphs <- Alphaset[[i]]

  ### This is the part where I need help
  N1eq <- (R1[i]*alphs[2,2]-R2[i]*alphs[1,2])/
    (alphs[1,1]*alphs[2,2]-alphs[2,1]*alphs[1,2])
  abline(h = N1eq, lty = 3)
  text(0, N1eq, "n1*", adj = c(0, 0))

  N2eq <- (R2[i]*alphs[1,1]-R1[i]*alphs[2,1])/
    (alphs[1,1]*alphs[2,2]-alphs[2,1]*alphs[1,2])
  abline(h = N2eq, lty = 3)
  text(0, N2eq, "n2*", adj = c(0, 0))

  K1 <- R1[i]/alphs[1,1]
  K2 <- R2[i]/alphs[2,2]

  abline(h = K1, lty = 3)
  text(0, K1, "K1", adj = c(0, 0))
  abline(h = K2, lty = 3)
  text(0, K2, "K2", adj = c(0, 0))

  #Invasion scores
  Inv1<-((R1[i])/(R2[i]))*(alphs[2,2]/alphs[1,2])
  Inv2<-((R2[i])/(R1[i]))*(alphs[1,1]/alphs[2,1])
  print(c(Inv1,Inv2))

  #abline(h = 1/alphs[1, 1], lty = 3)
  #text(0, 1/alphs[1, 1], "K", adj = c(0, 0))

}

绘图输出

我希望我在第一个地块上有 N1 和 N2,在第二个和第四个地块上是 K1,在第三个地块上是 K2。谢谢大家的帮助 !

山姆

4

1 回答 1

0

ablines考虑根据i循环迭代器有条件地绘制所需的图。请参阅下面的帮助部分中的调整

  ### This is the part where I need help
  if(i == 1) {
    N1eq <- (R1[i]*alphs[2,2]-R2[i]*alphs[1,2])/
      (alphs[1,1]*alphs[2,2]-alphs[2,1]*alphs[1,2])
    abline(h = N1eq, lty = 3)
    text(0, N1eq, "n1*", adj = c(0, 0))

    N2eq <- (R2[i]*alphs[1,1]-R1[i]*alphs[2,1])/
      (alphs[1,1]*alphs[2,2]-alphs[2,1]*alphs[1,2])
    abline(h = N2eq, lty = 3)
    text(0, N2eq, "n2*", adj = c(0, 0))
  }

  if (i %in% c(2,4)) {
    K1 <- R1[i]/alphs[1,1]
    abline(h = K1, lty = 3)
    text(0, K1, "K1", adj = c(0, 0))
  }

  if (i == 3) {
    K2 <- R2[i]/alphs[2,2]
    abline(h = K2, lty = 3)
    text(0, K2, "K2", adj = c(0, 0))
  }

绘图输出

于 2019-06-26T17:05:44.077 回答