## Interactive graphs using R

During a recent presentation, I was asked how the interactive plots for several process models were generated. The details of the models can’t be discussed, but the graphing technique can. In the following “rpanel” example, the proprietary process equations were replaced with typical Lotka-Volterra (LV) equations.

From the code below, when the panel is active, the LV differential equations are integrated from the initial conditions over the full time period. The slider bars are used to vary the model parameters.

The code can easily be modified to provide interactive plots of all types.

```#==========================================================================
# revgr.com
#
# This script provides an interactive panel for testing the
# Lotka-Volterra equations
#==========================================================================

library(deSolve) #Load package deSolve to integrate the initial value equations.
library(rpanel) #Load package rpanel to build the Interactive Panel

#Function lotvolmod contains the Lotka-Volterra differential equations that
#are to be integrated. This function is called by the ode(...) statement in
#a later code section.
#
#The inital conditions for the two LV equations can be changed by the
# +- buttons in the Interactive Panel, and the 4 parameters (a0, a1, b0, b1)
#can be changed by sliders in the Interactive Panel.
lotvolmod <- function(t, x, parval) {

#Calculate the derivatives
with(as.list(c(parval, x)), {
dprey <- ((a0 - (a1*pred)) * prey)
dpred <- ((b0 - (b1*prey)) * pred)

#Return the derivatives as a list
list(c(dprey, dpred))
})
}

#Collect the parameters, initial conditions, and time frame from the Interactive
#Panel so the Lotka-Volterra differential equations can be integrated, and
#the plot can be drawn.
lotvolmoddra <- function(panel) {

#Separate the variables so they can be fed into the integrator
parval <- c(a0=panel\$a0, a1=panel\$a1, b0=panel\$b0, b1=panel\$b1)
xsta <- c(prey=panel\$prey, pred=panel\$pred)
tim <- panel\$tim

#Call the integrator
outmat <- ode(y=xsta, times=tim, func=lotvolmod, parms=parval, atol=0.001)

#Plot the results
matplot(x=outmat[ ,1], y=outmat[ ,2:3], type="n", main="", xlab="time", ylab="Prey and Predator")
mtext(text=expression(frac(dPrey, dt) == (a0 - (a1 ~x~ Pred)) ~x~ Prey ~~~~~~~~~~~~~~~~~~~ frac(dPred, dt) == (b0 - (b1 ~x~ Prey)) ~x~ Pred), cex=1.2, font=2, line=0.5, side=3)
rect(par("usr"), par("usr"), par("usr"), par("usr"), col="white")
lines(x=outmat[, 1], y=outmat[, 2], lty=1, col=1)
lines(x=outmat[, 1], y=outmat[, 3], lty=2, col=2)
legend("topright", legend=c("Prey", "Pred"), col=c(1, 2), lty=c(1, 2), bg="white")

#Return panel
panel
}

#Redraw the plot.  This function will be called every time something is changed
#in the Interactive Panel.
lotvolmodred <- function(panel) {

#Replot the results
rp.tkrreplot(panel=panel, name=tkrp)

#Return panel
panel
}

#Build the Interactive Panel, draw the plot, and add the buttons and sliders
panel <- rp.control(times=seq(0, 50, 0.1), prey=0.1, pred=0.1, a0=1.4, a1=8, b0=-2.5, b1=-25, title="Lotka-Volterra Interactive Panel", size=c(800, 730))
rp.tkrplot(panel=panel, name=tkrp, plotfun=lotvolmoddra, hscale=2.2, vscale=1.5, pos=c(5, 200, 800, 700))
rp.doublebutton(panel=panel, var=prey, step=0.01, title="Prey Initial Value", action=lotvolmodred, range=c(0.01, 0.2), initval=0.01, pos=c(10, 5, 220, 50), showvalue=TRUE)
rp.doublebutton(panel=panel, var=pred, step=0.01, title="Pred Initial Value", action=lotvolmodred, range=c(0.01, 0.2), initval=0.01, pos=c(410, 5, 220, 50), showvalue=TRUE)
rp.slider(panel=panel, var=a0, from=0.1, to=3, action=lotvolmodred, pos=c(10, 40, 380, 100), showvalue=TRUE)
rp.slider(panel=panel, var=a1, from=1, to=15, action=lotvolmodred, pos=c(10, 105, 380, 100), showvalue=TRUE)
rp.slider(panel=panel, var=b0, from=-4, to=-1, action=lotvolmodred, pos=c(410, 40, 380, 100), showvalue=TRUE)
rp.slider(panel=panel, var=b1, from=-50, to=-1, action=lotvolmodred, pos=c(410, 105, 380, 100), showvalue=TRUE)

```