Learn R Programming

deSolve (version 1.8.1)

plot.deSolve: Plot and Histogram Method for deSolve Objects

Description

Plot the output of numeric integration routines.

Usage

## S3 method for class 'deSolve':
plot(x, which = 1:(ncol(x)-1), ask = NULL, ...)
## S3 method for class 'deSolve':
hist(x, which = 1:(ncol(x)-1), ask = NULL, ...)
## S3 method for class 'deSolve':
image(x, which = NULL, ask = NULL, 
  add.contour = FALSE, grid = NULL, method="image", ...)  
plot.1D (x, which=NULL, ask=NULL, grid=NULL, xyswap = FALSE, ...)

Arguments

x
an object of class deSolve, as returned by the integrators, and to be plotted.
which
the name(s) or the index to the variables that should be plotted. Default = all variables.
ask
logical; if TRUE, the user is asked before each plot, if NULL the user is only asked if more than one page of plots is necessary and the current graphics device is set interactive, see
add.contour
if TRUE, will add contours to the image plot.
method
the name of the plotting method to use, one of "image", "filled.contour", "persp", "contour".
grid
only for image plots and for plot.1D: the 1-D grid as a vector (output generated with ode.1D, or the x- and y-grid, as a list for output generated with ode.2D.
xyswap
if TRUE, then x-and y-values are swapped and the y-axis is from top to bottom. Useful for drawing vertical profiles.
...
additional graphics arguments passed to plot.default, image or hist

Details

The number of panels per page is automatically determined up to 3 x 3 (par(mfrow = c(3, 3))). This default can be overwritten by specifying user-defined settings for mfrow or mfcol. Set mfrow equal to NULL to avoid the plotting function to change user-defined mfrow or mfcol settings.

Other graphical parameters can be passed as well. Parameters xlab and ylab are vectorized, so it is possible to assign specific axis labels to individual plots. Image plots will only work for 1-D and 2-D variables, as solved with ode.1D and ode.2D. In the first case, an image with times as x- and the grid as y-axis will be created. in second case, an x-y plot will be created, for all times. Unless ask = FALSE, the user will be asked to confirm page changes. For images, it is possible to pass an argument method which can take the values "image" (default), "filled.contour", "contour" or "persp", in order to use the respective plotting method. plot will always have times on the x-axis. For problems solved with ode.1D, it may be more useful to use plot.1D which will plot how spatial variables change with time. These plots will have the grid on the x-axis.

See Also

print.deSolve, ode, deSolve

Examples

Run this code
## =======================================================================
## A Predator-Prey model with 4 species in matrix formulation
## =======================================================================

LVmatrix <- function(t, n, parms) {
  with(parms, {
    dn <- r * n + n * (A %*% n)
    return(list(c(dn)))
  })
}
parms <- list(
  r = c(r1 = 0.1, r2 = 0.1, r3 = -0.1, r4 = -0.1),
  A = matrix(c(0.0, 0.0, -0.2, 0.01,      # prey 1
               0.0, 0.0, 0.02, -0.1,      # prey 2
               0.2, 0.02, 0.0, 0.0,       # predator 1; prefers prey 1
               0.01, 0.1, 0.0, 0.0),      # predator 2; prefers prey 2
               nrow = 4, ncol = 4, byrow=TRUE)
)
times <- seq(from = 0, to = 500, by = 0.1)
y     <- c(prey1 = 1, prey2 = 1, pred1 = 2, pred2 = 2)

out <- ode(y, times, LVmatrix, parms)

## Basic line plot
plot(out, type = "l")

## User-specified axis labels
plot(out, type = "l", ylab = c("Prey 1", "Prey 2", "Pred 1", "Pred 2"),
  xlab = "Time (d)", main = "Time Series") 

## Set user-defined mfrow
pm <- par (mfrow = c(2, 2))
## "mfrow=NULL" keeps user-defined mfrow
plot(out, which = c("prey1", "pred2"), mfrow = NULL, type = "l", lwd = 2)

plot(out[,"prey1"], out[,"pred1"], xlab="prey1", 
  ylab = "pred1", type = "l", lwd = 2)
plot(out[,"prey2"], out[,"pred2"], xlab = "prey2", 
  ylab = "pred2", type = "l",lwd = 2)

## restore graphics parameters
par ("mfrow" = pm)


hist(out, col = "darkblue", breaks = 50)

## =======================================================================
## The Aphid model from Soetaert and Herman, 2009.
## A practical guide to ecological modelling.
## Using R as a simulation platform. Springer.
## =======================================================================

## 1-D diffusion model

## ================
## Model equations
## ================
Aphid <- function(t, APHIDS, parameters) {
  deltax  <- c (0.5, rep(1, numboxes - 1), 0.5)
  Flux    <- -D * diff(c(0, APHIDS, 0))/deltax
  dAPHIDS <- -diff(Flux)/delx + APHIDS * r
  list(dAPHIDS)
}
  
## ==================
## Model application
## ==================

## the model parameters:
D         <- 0.3    # m2/day  diffusion rate
r         <- 0.01   # /day    net growth rate
delx      <- 1      # m       thickness of boxes
numboxes  <- 60 

## distance of boxes on plant, m, 1 m intervals
Distance  <- seq(from = 0.5, by = delx, length.out = numboxes)

## Initial conditions, ind/m2
## aphids present only on two central boxes
APHIDS        <- rep(0, times = numboxes)
APHIDS[30:31] <- 1
state         <- c(APHIDS = APHIDS)      # initialise state variables 
                  
## RUNNING the model:
times <- seq(0, 200, by = 1)   # output wanted at these time intervals
out   <- ode.1D(state, times, Aphid, parms = 0, nspec = 1)

image(out, grid = Distance, main = "Aphid model", ylab = "distance, m")

image(out, grid = Distance, main = "Aphid model", ylab = "distance, m", 
  method = "persp", border = NA, theta = 30)
plot(out,ask = FALSE, mfrow = c(1, 1))
  plot.1D(out, ask = FALSE, type = "l", lwd = 2, xyswap = TRUE)

Run the code above in your browser using DataLab