【发布时间】:2019-11-06 08:03:42
【问题描述】:
好的,所以我使用了 par 函数来组合多个图。 我添加了图例,这一切都很有趣和游戏,直到某些情节需要与其他情节不同的图例,而我无法弄清楚使其工作所需的编程体操。
我在“这是我需要帮助的部分”的名称下包含了我的图例部分,如您所见,其中有四个,分别为 N1、N2、K1 和 K2,带有 abline 和每个文本功能。
我尝试制作串联列表等,但效果不如我的 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。谢谢大家的帮助!
山姆
【问题讨论】:
-
首先感谢您提供可重现的示例!但是你能在最后一句话中阐明你的需求吗?你需要那些 N* 和 K* 来代替传说中的物种 1 和 2 吗?
-
不客气!不,我不需要它们来替换任何东西,其余的传说都很好,因为它们适用于每个情节,只是那些情节与下一个情节不同。现在它们适用于每个地块,我想要的是:-Plot 1 -> N1eq 和 N2eq 行(abline),上面有文本名称(文本)“n1*”和“n2*”,x=0 和 h=分别为 N1eq 和 N2eq 线。情节 2 -> K1 abline 和文本;情节 3 -> K2 abline 和文本; Plot 4 -> K1 abline 和 text.
-
您能否使用一些图形软件为您想要的结果创建视觉效果?然后,您可以将图像或屏幕截图链接到您的帖子。展示总是比讲述更好。
-
当然可以,在这里,我在 Paint 和 Photoshop 上快速完成了这项工作。对不起,昨天是休息日! puu.sh/DKXrM/d23ed641b6.png
-
喂?对不起,我仍然需要一个答案
标签: r plot model legend population