# Multiple Trajectories

Open the file `examples/cain/WilkinsonSMfSB/ch06-lv.xml`, which models the Lotka-Volterra system. In the method editor, change the recording time to 50 and set the number of frames to 501 so that we record the state every 0.1 time units. In the launcher panel, set the number of trajectories to 5 and then run the simulation by clicking the launch button . Next click the export button in the simulation output panel. Export the species trajectories and use "lv" for the base name in the file dialog.

Launch R and set the working directory to where you have saved the data file. Read the CSV (Comma Separated Values) file and have a look at the first few lines.

    > setwd('Development/cain')
    > lv.df <- read.csv('lv.csv')
    > head(lv.df)
      Time Prey_1 Prey_2 Prey_3 Prey_4 Prey_5 Predator_1 Predator_2 Predator_3 Predator_4
    1  0.0     50     50     50     50     50        100        100        100        100
    2  0.1     48     55     55     52     53         99         97         89         92
    3  0.2     47     58     56     56     55         99         95         80         88
    4  0.3     51     66     58     57     63         95         95         72         82
    5  0.4     52     67     63     72     71         94         94         68         80
    6  0.5     54     71     67     70     73         88         91         68         86
      Predator_5
    1        100
    2         95
    3         96
    4         93
    5         90
    6         87

To make a time series data structure, we drop the first column. However, we use the times in the first column to define the starting time and the time increment. By default, plotting the time series data will yield separate plots for each species and each trajectory.

    > lv.ts <- ts(lv.df[,-1], start=lv.df[1,1], deltat=lv.df[2,1]-lv.df[1,1])
    > plot(lv.ts)

Next we plot all of the prey trajectories together. The prey trajectories are column 1 through 5 in the time series data structure. Instead of using this fact, we show a more general method of selecting the appropriate columns below. We use the `grep()` function, which stands for Get Regular ExPression, to select the columns by matching the column names.

    > cols <- grep('Prey', colnames(lv.ts))
    > plot(lv.ts[,cols], plot.type='single', ylab='Prey')

We can also plot all of the prey trajectories and all of the predator trajectories in a split window.

    > split.screen(c(2,1))
    [1] 1 2
    > screen(1)
    > cols <- grep('Prey', colnames(lv.ts))
    > plot(lv.ts[,cols], plot.type='single', ylab='Prey')
    > screen(2)
    > cols <- grep('Predator', colnames(lv.ts))
    > plot(lv.ts[,cols], plot.type='single', ylab='Predator')
    > close.screen(all.screens=T)