当我用R解决微分方程时,遇到了一个问题:我需要在我的图中添加等值线和方向场,但我不知道该如何做。我应该安装哪个软件包/调用哪个函数/还是手动完成?
primer
包中的 LVCompGames
函数会生成如下图所示:graphics
包和filled.contour
函数。
然后 @Josh O'Brien 建议使用 rasterVis
包中的 vectorplot
函数。
我认为如果您提供更多希望输出的信息,所有答案都可以进行定制。以下是Lotka-Volterra掠食者-猎物系统的可用代码示例:Error in deriv(t = 0, y = c(x[i], y[j]), parameters = parameters) :
unused arguments (y = c(x[i], y[j]), parameters = parameters)
require(deSolve)
require(phaseR)
model.LV <- function(t, y, parameters){
with(as.list(parameters),{
N<-y[1]
P<-y[2]
dN <- a*N - b*N*P
dP <- c*N*P -d*P
list(c(dN,dP))
})
}
params.LV<-c(a=0.4, b=0.3, c=0.1, d=0.2)
data.LV<-as.data.frame(lsoda(c(N=1,P=1),seq(1,250,by=0.5), model.LV, params.LV))
# plot the time series of both populations
plot(data.LV$time,data.LV$N, main="Time series of L-V equations", xlab="time",
ylab="Population densities N, P",
type="l", col="green",
ylim=c(0,max(data.LV$N,data.LV$C)))
lines(data.LV$time,data.LV$P,col="red")
# plot the trajectories of the system
plot(data.LV$N, data.LV$P, type="l", col="blue", main="Trajectory of L-V equations",
xlab="Prey density N", ylab="Predator density P", xlim=c(0,5), ylim=c(0,3))
#add Nullclines
nullclines(model.LV, x.lim=c(0.1,5),y.lim=c(0.1,3), parameters=params.LV, system="two.dim", colour=c("green","red"), add=TRUE)
my.symbols
函数允许您定义自己的符号以添加到现有图形中。支持函数ms.arrows
已经编码了基本箭头以添加到图形中,您可以轻松修改它(或其他ms.函数)以创建不同的符号以添加。
library('sos') findFn('isoclines')
命令可以找到LVCompGames
包,该包可以生成等值线和初始点的图形。 - agstudyprimer
,而LVCompGames是等值线函数。 - agstudyvectorplot()
和contourplot()
。以下是一个示例:http://rastervis.r-forge.r-project.org/#vectorplot。(将margin=FALSE
设置为抑制边缘分布的绘图。) - Josh O'Brien