Birdsnest Track Plot using R
This follows on from the Introduction to R tutorial - where we learnt to load and manipulate the numeric data contained in trials. In this tutorial we show how to generate plots of location over the trial, and to overlay these positions to show exactly where the subject travels.
Plotting the Data:
From the first tutorial, remember we can load our CSV data from an experiment and can organise it into a matrix for the body position:
> trial <- read.csv(file="Trial_1_Data.csv",head=TRUE)
> body <- matrix(c(trial$BodyCenter_X..cm,-trial$BodyCenter_Y..cm),length(times),2
Notice again that we invert the y coordinate for correctness, since all ActualTrack results are based on an imaging coordinate system that locates the origin in the top left corner. In doing this, we are not then confused why the plot doesn't look like the original video playback.
We can now instantly plot our position data for this trial by typing:
> plot(body,type="l",col="black",axes=FALSE,ann=FALSE)
to simply draw a continuous black (col="black") line (type="l") between all co-ordiante points for the body position. We also don't want any axes (axes=FALSE) or axes annotations (ann=FALSE) to be shown. An example is shown in Figure 1.

Figure 1: Raw plot of x and y trajectory data.
We can also plot the position (in the case of this tracking data) where multiple parts are also tracked. First by similarly organising the data:
> tail <- matrix(c(trial$TailBase_X..cm,-trial$TailBase_Y..cm),length(times),2)
> nose <- matrix(c(trial$NosePoint_X..cm,-trial$NosePoint_Y..cm),length(times),2)
And then by adding them to the plot. Here we use the lines command (since using plot will create an entirely new figure as shown in Figure 2):
> lines(nose,col="red")
> lines(tail,col="blue")

Figure 2: Multiple tracks overlaid on the same plot.
As a final step we can also add a bounding box to the plot with:
> box()
And, to also add a title with:
> title(main="Spatial Tracks of Location by Part")
And a legend:
> legend(-10,max(body[,2])+5,c("Body","Tail","Nose"),col=c("black","blue","red"),lwd=1)
This is quite a complex command, because we also have to specify where to place the legend (the first two values, provide a list of the legend titles and their respective colors, and indicate the thickness/type of line to use.
The resulting plot is shown in Figure 3:

Figure 3: Final "birdsnest" plot of location.
Exporting the Plot:
Finally, if you wanted to save your plot (for inclusion into a report or paper) you can in most cases just copy directly from the displayed image. Alternatively, you can also specify that output should go to a PDF file, instead of the screen, by doing the following commands before and after the above plotting commands:
> pdf(file="myplot.pdf")
> ... Various plotting commands ...
> dev.off()
This produces an extremely high quality pdf file (called "myplot.pdf") in the same directory as the experiment. Notice that you will not see the effects on the display - since the output is being written to file (so it is usually best to include all your commands in a single script file). It is crucial to call dev.off() to let the pdf writer know when to close the file as it is being generated.