trajr: An R package for characterisation of animal trajectories
Abstract
Quantitative characterisation of the trajectories of moving animals is an important component of many behavioural and ecological studies, however methods are complicated and varied, and sometimes require well‐developed programming skills to implement. Here, we introduce trajr, an R package that serves to analyse animal paths, from unicellular organisms, through insects to whales. It makes a variety of statistical characterisations of trajectories, such as tortuosity, speed and changes in direction, available to biologists who may not have a background in programming. We discuss a range of indices that have been used by researchers, describe the package in detail, then use movement observations of whales and clearwing moths to demonstrate some of the capabilities of trajr. As an open‐source R package, trajr encourages open and reproducible research. It supports the implementation of additional methods by providing access to trajectory analysis “building blocks,” allows the full suite of R statistical analysis tools to be applied to trajectory analysis, and the source code can be independently validated.
1 INTRODUCTION
Animal movement is studied in fields as diverse as population ecology (Turchin, 1998), foraging theory and resource exploitation (Mouillot & Viale, 2001; Pyke, Pulliam, & Charnov, 1977), and conservation biology (Gulesserian, Slip, Heller, & Harcourt, 2011). The analysis of trajectories, which are recorded tracks of individual animals moving through space and time, has been applied to answer questions about animal foraging, navigation, dispersal ecology, migration and behavioural mimicry (Codling, Plank, & Benhamou, 2008; Shamble, Hoy, Cohen, & Beatus, 2017). Animal trajectories are analysed as a set of time specific discrete locations, generally restricted to two spatial dimensions. As animal motion is continuous, trajectories are obtained by repeatedly sampling the location of an animal over time.
A variety of techniques has been used to sample locations, including GPS tracking coupled with satellite or radio telemetry (Mouillot & Viale, 2001), manual tracking (Angilletta, Roth Ii, Wilson, Niehaus, & Ribeiro, 2008), digitising from video of a moving animal (Shamble et al., 2017), harmonic radar (Mascanzoni & Wallin, 1986) and following tracks in snow (Fortin, Morales, & Boyce, 2005). Location sampling may occur at constant time intervals, or at biologically meaningful step “endpoints,” such as landing sites of a foraging butterfly (Kareiva & Shigesada, 1983). Trajectories can be conveniently represented and analysed as a series of steps, each of which has a length and a direction. The steps correspond to vectors between consecutive locations in the sampled trajectory.
2 TRAJECTORY INDICES
(1)where D is the straight‐line distance between the start and end of the path, and L is the length of the path (Batschelet, 1981). d may be a reliable estimate of the efficiency of a directed path, but unsuitable for random paths (Benhamou, 2004). Batschelet (1981) considers d to be an easily calculable approximation to an alternative straightness index, r, which is the mean vector of turning angles of a trajectory with constant step length.
, which is a dimensionless value that denotes the maximum expected displacement of a random path as a function of the number of steps (Cheung, Zhang, Stricker, & Srinivasan, 2007). Cheung et al. (2007) consider
to be superior to sinuosity as it depends on the distribution of turning angles rather than just the standard deviation. An alternative measure,
, gives the maximum expected displacement in physical dimensions (Cheung et al., 2007):
(2)where
is the mean step length of the trajectory.
A qualitatively different approach for describing sinuosity is the use of fractal dimension (or fractal D), which may identify scale‐independent aspects of animal movement (Turchin, 1996). Fractal dimension relies on the fact that the measured length of a line (such as a coastline or an animal trajectory) depends on the length of the measuring stick used to measure it (Mandelbrot, 1967). One technique to measure the fractal dimension D of a line—the dividers method—is to repeatedly resample to different step lengths, then plot measured path length against step length. The slope of the log‐log regression is then 1 ‐ D. Fractal D varies between 1 for a straight line and 2 for an infinitely variable curve. Nams (2006) has developed corrected methods to reduce bias in the original dividers method. The application of fractal D to animal trajectories may be inappropriate as animal trajectories may not be fractal curves (Benhamou, 2004; Turchin, 1996), (but see Mouillot & Viale, 2001). However, Nams (2005) considers it useful for assessing animals’ responses to environmental variation at different spatial scales.
(3)
is the angle of step
, and
varies from 1 to some upper limit. It is mainly applicable to relatively straight paths, as large bends may distort the analysis (Shamble et al., 2017). The values of C(∆s) and ∆s at the first local minimum in C(∆s) may be used to quantify the amplitude and wavelength of the shortest wavelength repeated sine‐like shape in the trajectory (Shamble et al., 2017).
Speed, that is displacement per unit time, provides a measure of space usage and can be related to the distribution of resources (Laube & Purves, 2011). Speed—and changes in speed—can be used to identify different types of behaviour, for example resting versus foraging (Shamoun‐Baranes, Bouten, Camphuysen, & Baaij, 2011). It can be derived from a trajectory change in location divided by time. Linear acceleration is change in speed divided by time, however noise in trajectory locations may result in a high level of noise in the derived speed, and an even higher level of noise in the derived linear acceleration (Laube & Purves, 2011). Consequently, it may be necessary to first smooth the trajectory, reducing high‐frequency noise before deriving speed or linear acceleration. The Savitzky‐Golay filter is designed to improve the signal‐to‐noise ratio, and maybe an appropriate filter to apply to a trajectory before calculating derivatives (Savitzky & Golay, 1964; Shamble et al., 2017). Many trajectory recording methods suffer from high‐frequency noise, and for such trajectories, smoothing is applicable prior to any analysis.
While there is clearly no shortage of methods and indices for analysing trajectories, implementing them is sometimes not straightforward and may require advanced programming knowledge. Here, we describe the trajr R package that implements a collection of trajectory analysis methods and algorithms. R is an open‐source and multi‐platform programming language and environment which, together with a wealth of user‐contributed packages, provides a wide variety of statistical, graphical and analytic techniques, and is highly extensible (R Core Team, 2016). trajr is also open‐source, and builds on the strengths of R to provide a powerful, open, accessible, verifiable framework for trajectory analysis.
3 THE TRAJR PACKAGE
trajr aims to simplify the analysis, manipulation and visualisation of 2‐dimensional trajectories. It is written entirely in R, so does not require compilation, is easy to install, and highly portable. The stable release of trajr (version 1.0.0 at the time of writing) is available for direct installation from CRAN (http://cran.r-project.org/package=trajr), and the source code and most recent development version are available from GitHub (https://github.com/JimMcL/trajr). The trajr workflow is simple: read in one or more files containing trajectory data, creating objects of the R class Trajectory; optionally modify them (scale, rotate, resample to fixed step lengths); call functions to calculate characteristics of the trajectories; then visualise, evaluate or compare the characteristics, or undertake further statistical analyses using the capabilities of the R environment. trajr does not aid in the capture, creation or translation to discrete representations of trajectories. It also does not provide any general statistical methods, as they are already available within the R environment.
Objects of the class “Trajectory” are created by calling the function TrajFromCoords with a series of spatial locations, specified as (x, y) coordinates. The coordinates may have associated times, or times can be inferred on the assumption that the locations were sampled at constant time intervals. The Trajectory class inherits methods from data.frame, and is documented in the help page for TrajFromCoords. Trajectory objects should not be directly modified—to change the locations or times in a trajectory, either call the methods that exist for that purpose (e.g., TrajScale, TrajRotate, TrajReverse, TrajSmoothSG, TrajRediscretize), or obtain a set of modified (x, y, time) coordinates and call TrajFromCoords to create a new Trajectory. A trajectory trj can be visualised by calling plot(trj). With no additional arguments, the trajectory is plotted as a series of straight‐line segments joining the locations, and a small filled circle is drawn at the start point (Figure 1).

trajr defines multiple functions to characterise trajectories, including several measures of tortuosity and speed (Table 1). Some trajectories may require additional processing before these functions are applied. For example, the straightness index r (i.e., mean vector of turning angles (Batschelet, 1981) assumes a constant step length. To calculate r for a trajectory with a variable step length, it must first be resampled to a constant step length (a process called rediscretisation (Bovet & Benhamou, 1988), which can be achieved by calling the function TrajRediscretize. Similarly, before analysing a noisy trajectory, the function TrajSmoothSG can be called to create a smoothed trajectory by applying a Savitzky‐Golay smoothing filter (Savitzky & Golay, 1964). Consult the help page for each function for any such requirements.
| Function | Description |
|---|---|
| TrajStraightness | Straightness index (Batschelet, 1981)
|
| TrajSinuosity | Sinuosity (Bovet & Benhamou, 1988) |
| TrajSinuosity2 | Corrected sinuosity (Benhamou, 2004) |
| TrajEmax | Maximum expected displacement, and (Cheung et al., 2007)
|
| TrajFractalDimension | Fractal dimension using the corrected dividers method (Nams, 2006) |
| TrajDirectionalChange | Directional change (DC) for all turning angles, mean(DC) and sd(DC) may be used to calculate the mean and standard deviation respectively (Kitamura & Imafuku, 2015) |
| TrajDirectionAutocorrelations TrajDAFindFirstMinimum | Direction autocorrelation function for a range of values of , and the first local minimum in (Shamble et al., 2017)
|
| TrajDerivatives | Speed and linear acceleration |
| TrajSpeedIntervals | Locates steps within the trajectory which satisfy a speed constraint |
trajr provides a number of functions that return lower‐level information about trajectories, which may be useful when calculating measures that have not been implemented within trajr (Table 2). As an example, consider an alternative measure of straightness, r, which is the length of the mean vector of turning angles (assuming a constant step length; Batschelet, 1981). The following expression will calculate r for the trajectory trj, after resampling to a step length of 2: Mod(TrajMeanVectorOfTurningAngles(TrajRediscretize(trj, 2))).
| Function | Description |
|---|---|
| TrajStepLengths | Returns the lengths of each step within the trajectory |
| TrajLength | Returns the total length of the trajectory (or a portion) |
| TrajDistance | Returns the straight‐line distance from the start to the end of the trajectory (or a portion) |
| TrajAngles | Returns the turning angles (radians) of a trajectory |
| TrajMeanVectorOfTurningAngles | Returns the mean vector of the turning angles (Batschelet, 1981) |
| TrajExpectedSquareDisplacement | Returns the expected square displacement of a correlated random walk (Kareiva & Shigesada, 1983) |
As trajectory analyses typically involve multiple trajectories, trajr provides several functions to simplify this situation. TrajsBuild assumes that paths are defined in multiple files. It reads the paths from the files, optionally smooths and scales them, then returns a list of Trajectory objects. TrajsMergeStats assists with constructing a data frame that characterises a list of trajectories, and simplifies the handling of some idiosyncratic R behaviour such as the treatment of NULL values. The R language has two subtly different ways to represent missing or unknown values (such as missing values in a spreadsheet), NULL and NA. NULL values are not allowed in vectors or data frames, so TrajsMergeStats converts NULL values to NA. Functions are also provided to aid in dealing with NA values prior to principal components analysis (TrajsStatsReplaceNAs) and for listing all step lengths in a list of Trajectory objects (TrajsStepLengths).
trajr provides the facility to generate random trajectories to assist with theoretical studies, by calling the function TrajGenerate. Generation is based on the concept of discrete steps with random lengths and turning angles, both of which are generated using user‐controllable distribution functions. This allows creation of trajectories with highly variable statistical characteristics, including correlated random walks, directed walks, Levy walks and Brownian motion (Figure 2).

4 ALTERNATIVES TO TRAJR
EthoVision is a well‐established proprietary commercial software product that automates behavioural observations (L. P. J. J. Noldus, Spink, & Tegelenbosch, 2001). Primarily a video tracking application, it also allows calculation of a range of parameters describing 2‐ or 3‐dimensional trajectories extracted from video. Parameters include “tortuosity” (the inverse of the straightness index d (Batschelet, 1981)), instantaneous (or “sample” velocity) or speed with built‐in smoothing, instantaneous angular change and angular velocity, as well as various measures of behaviour (Noldus, 2018). Raw data can be exported for further analysis within other systems such as Microsoft Excel, SPSS or R (L. P. J. J. Noldus et al., 2001). In summary, EthoVision is intended for analysing behaviour from videos, with only limited capability for characterising trajectories, whereas trajr is focused on characterising trajectories obtained using any method. EthoVision is not open‐source, it is not integrated into a general purpose statistical/analytic environment, and it is mainly intended for interactive use (although it does have a batch analysis mode), so is difficult to include as a component within a reproducible workflow.
adehabitatLT is an open‐source R package for the analysis of animal movements (Calenge, Dray, & Royer‐Carenzi, 2009). It provides a variety of functions to analyse movements, and is particularly concerned with habitat selection by animals as revealed by their movements. adehabitatLT does not directly provide high‐level characterisations of trajectories, but rather provides a set of lower‐level components, which could be used as a basis for such calculations.
5 WORKED EXAMPLE: BEHAVIOURAL RESPONSES OF HUMPBACK WHALES TO WHALE‐WATCHING VESSELS
Humpback Whales (Megaptera novaeangliae Borowski, 1781) change their behaviour in response to whale‐watching traffic, with potential long‐term negative impacts (Parsons, 2012). Behaviours affected by these disruptions include surfacing and diving, tail swiping and breaching, singing behaviour, swimming speed, swimming direction, and feeding or resting behaviours (Parsons, 2012). Here, to illustrate some capabilities of trajr, we present part of an analysis of whale trajectories that were recorded during research into the effects of whale‐watching vessels on whales (Kessler & Harcourt, 2013).
The trajectories were created by combining a vessel GPS track with manual observations of distance and direction of whales from the vessel. The result was multiple CSV files containing the time, latitude, longitude and behaviour (e.g., blow, breach) at each observation, with one file per trajectory (Figure 3). To begin the analysis, we defined a function to read a CSV trajectory file and convert the content to a form suitable for use with trajr.

The result (in the variable named indices) is a data frame with a row for each trajectory, and a column for each index (Table 3). The analytic capabilities of R can now be applied to calculate appropriate statistics on this data frame.
| Trajectory | Breach freq. (breach/hr) | Mean speed (km/hr) | Sinuosity | Date |
|---|---|---|---|---|
| 1 | 0.00 | 7.7 | 0.034 | 2010‐06‐08 p.m. |
| 2 | 0.00 | 6.9 | 0.022 | 2010‐06‐09 p.m. |
| 3 | 0.64 | 7.1 | 0.058 | 2010‐06‐11 a.m. |
| 4 | 0.00 | 8.5 | 0.023 | 2010‐06‐11 p.m. |
| 5 | 0.00 | 9.5 | 0.016 | 2010‐06‐12 a.m. |
6 OTHER EXAMPLES OF USE
To visualise the indices, the first two components from a principal component analysis were plotted, which reveals two clusters, one containing the trajectories of bee mimics and bees, and the other containing wasp mimics and wasps (Figure 4), in agreement with the conclusions of Skowron Volponi et al. (2018).

7 CONCLUSIONS
trajr makes trajectory analysis simple and available to a wide range of researchers, including biologists without a programming background. As open‐source, the implementation can be independently validated, corrected, and extended. Rather than attempt to provide an exhaustive end‐to‐end analysis solution, it provides building blocks that can be used to more easily create additional functionality, and implement additional or new algorithms. It allows for direct integration with the extensive statistical procedures provided by R and the user‐supplied packages. As a scripted analysis, it is ideally suited to be part of a reproducible research workflow, which involves automating analyses from reading raw data through to producing publishable outputs such as plots and tables, or even entire manuscripts, without manual intervention (Stodden, Leisch, & Peng, 2014). In line with this approach, the R source code and raw data used to create all figures and analyses in this document, as well as the complete source code for both examples, is available online at GitHub (https://github.com/JimMcL/trajr-paper).
Further details on using the trajr package can be found in the package vignette that is available online from CRAN, or can be accessed from R (after the package has been installed) by running the command vignette(“trajr‐ vignette”).
8 CITATION OF METHODS IMPLEMENTED IN TRAJR
Many of the functions in trajr implement methods that are described in their original publications. Whenever this is the case, the help pages for the functions identify the original publications. Users of the functions should cite the original sources as well as trajr.
ACKNOWLEDGEMENTS
DJM received funding from the Australian Research Council, Project ID: DP170101617. MASV received funding through a doctoral scholarship ID 2016/20/T/NZ8/00541 from National Science Centre (Poland). The whale research was funded by the Australian Research Council, LP07756840 to R. Harcourt and Bass & Flinders Cruises (Whale Watching Sydney), and was conducted under Macquarie Animal Ethics Approvals 2007/011 and 2010/025 and NSW Office of Environment and Heritage Scientific Permits S12600 and S12735. We would like to thank Tom Tregenza and one anonymous reviewer whose constructive comments allowed us to significantly improve the manuscript.
CONFLICT OF INTEREST
We declare that the authors have no conflict of interest.
REFERENCES
Citing Literature
Number of times cited according to CrossRef: 24
- Michał Krzyżowski, Jacek Francikowski, Bartosz Baran, Agnieszka Babczyńska, The short-chain fatty acids as potential protective agents against Callosobruchus maculatus infestation, Journal of Stored Products Research, 10.1016/j.jspr.2020.101570, 86, (101570), (2020).
- Jocelyn Hunyadi, Todd Currier, Yahya Modarres-Sadeghi, Brooke E. Flammang, Ethan D. Clotfelter, Morphology, performance and fluid dynamics of the crayfish escape response, The Journal of Experimental Biology, 10.1242/jeb.219873, 223, 15, (jeb219873), (2020).
- Dewi Langlet, Vincent M.P. Bouchet, Camille Delaeter, Laurent Seuront, Motion behavior and metabolic response to microplastic leachates in the benthic foraminifera Haynesina germanica, Journal of Experimental Marine Biology and Ecology, 10.1016/j.jembe.2020.151395, 529, (151395), (2020).
- Giovanni Scribano, Alessandro Balestrieri, Andrea Gazzola, Daniele Pellitteri‐Rosa, Strong behavioural defensive responses of endemic Rana latastei tadpoles induced by a native predator's odour, Ethology, 10.1111/eth.13072, 126, 9, (922-930), (2020).
- Georgina M. Ginnaw, Isla K. Davidson, Harry R. Harding, Stephen D. Simpson, Nicholas W. Roberts, Andrew N. Radford, Christos C. Ioannou, Effects of multiple stressors on fish shoal collective motion are independent and vary with shoaling metric, Animal Behaviour, 10.1016/j.anbehav.2020.07.024, 168, (7-17), (2020).
- Georgia Troup, Bruce Doran, Jessie Au, Lucy E. King, Iain Douglas-Hamilton, Robert Heinsohn, Movement tortuosity and speed reveal the trade-offs of crop raiding for African elephants, Animal Behaviour, 10.1016/j.anbehav.2020.08.009, 168, (97-108), (2020).
- Buncha Chuaysi, Supaporn Kiattisin, Fishing Vessels Behavior Identification for Combating IUU Fishing: Enable Traceability at Sea, Wireless Personal Communications, 10.1007/s11277-020-07200-w, (2020).
- Brogan A Amos, Kyran M Staunton, Scott A Ritchie, Ring T Cardé, Attraction Versus Capture: Efficiency of BG-Sentinel Trap Under Semi-Field Conditions and Characterizing Response Behaviors for Female Aedes aegypti (Diptera: Culicidae), Journal of Medical Entomology, 10.1093/jme/tjz243, (2020).
- Malou van der Sluis, Yvette de Haas, Britt de Klerk, T. Bas Rodenburg, Esther D. Ellen, Assessing the Activity of Individual Group-Housed Broilers Throughout Life Using a Passive Radio Frequency Identification System—A Validation Study, Sensors, 10.3390/s20133612, 20, 13, (3612), (2020).
- Michał Krzyżowski, Bartosz Baran, Bartosz Łozowski, Jacek Francikowski, The Effect of Rosmarinus officinalis Essential Oil Fumigation on Biochemical, Behavioral, and Physiological Parameters of Callosobruchus maculatus, Insects, 10.3390/insects11060344, 11, 6, (344), (2020).
- Justin Shenk, Klara J. Lohkamp, Maximilian Wiesmann, Amanda J. Kiliaan, Automated Analysis of Stroke Mouse Trajectory Data With Traja, Frontiers in Neuroscience, 10.3389/fnins.2020.00518, 14, (2020).
- Danila Bobkov, Anastasia Polyanskaya, Anastasia Musorina, Ekaterina Lomert, Sergey Shabelnikov, Galina Poljanskaya, Replicative senescence in MSCWJ-1 human umbilical cord mesenchymal stem cells is marked by characteristic changes in motility, cytoskeletal organization, and RhoA localization, Molecular Biology Reports, 10.1007/s11033-020-05476-6, (2020).
- Andrew P. Colefax, Brendan P. Kelaher, Daniel E. Pagendam, Paul A. Butcher, Assessing White Shark (Carcharodon carcharias) Behavior Along Coastal Beaches for Conservation-Focused Shark Mitigation, Frontiers in Marine Science, 10.3389/fmars.2020.00268, 7, (2020).
- Robin Welsch, Christoph von Castell, Martin Rettenberger, Daniel Turner, Heiko Hecht, Peter Fromberger, Sexual attraction modulates interpersonal distance and approach-avoidance movements towards virtual agents in males, PLOS ONE, 10.1371/journal.pone.0231539, 15, 4, (e0231539), (2020).
- Ulrike Obertegger, Giovanna Flaim, Pierluigi Colangeli, Tracking of algal cells: case study of swimming speed of cold-adapted dinoflagellates, Hydrobiologia, 10.1007/s10750-020-04216-y, (2020).
- Brogan A Amos, Scott A Ritchie, Ring T Cardé, Attraction Versus Capture II: Efficiency of the BG-Sentinel Trap Under Semifield Conditions and Characterizing Response Behaviors of Male Aedes aegypti (Diptera: Culicidae), Journal of Medical Entomology, 10.1093/jme/tjaa065, (2020).
- Rocío Joo, Matthew E. Boone, Thomas A. Clay, Samantha C. Patrick, Susana Clusella‐Trullas, Mathieu Basille, Navigating through the r packages for movement, Journal of Animal Ecology, 10.1111/1365-2656.13116, 89, 1, (248-267), (2019).
- Richard Bischof, Jon Glenn Omholt Gjevestad, Andrés Ordiz, Katrine Eldegard, Cyril Milleret, High frequency GPS bursts and path-level analysis reveal linear feature tracking by red foxes, Scientific Reports, 10.1038/s41598-019-45150-x, 9, 1, (2019).
- Rebecca Krebs, Miriam Linnenbrink, Anja Guenther, Validating standardised personality tests under semi‐natural conditions in wild house mice (Mus musculus domesticus), Ethology, 10.1111/eth.12930, 125, 11, (761-773), (2019).
- Jessica L. Mohlman, Rachel R. Gardner, I. B. Parnell, Nathan G. Wilhite, James A. Martin, Nonconsumptive effects of hunting on a nontarget game bird, Ecology and Evolution, 10.1002/ece3.5479, 9, 16, (9324-9333), (2019).
- Shumpei Hisamoto, Natsumi Hosaka, Yuka Matsunami, Hideo Iwasaki, Route reassessment by transporter ants improves speed and directional accuracy of cooperative transport in Formica japonica, Journal of Ethology, 10.1007/s10164-019-00626-1, (2019).
- Marta A. Skowron Volponi, Donald James McLean, Paolo Volponi, Robert Dudley, Moving like a model: mimicry of hymenopteran flight trajectories by clearwing moths of Southeast Asian rainforests, Biology Letters, 10.1098/rsbl.2018.0152, 14, 5, (20180152), (2018).
- Vincent Raoult, Louise Tosetto, Jane Williamson, Drone-Based High-Resolution Tracking of Aquatic Vertebrates, Drones, 10.3390/drones2040037, 2, 4, (37), (2018).
- Susanne M. M. Mooij, Maartje E. J. Raijmakers, Iroise Dumontheil, Natasha Z. Kirkham, Han L. J. Maas, Error detection through mouse movement in an online adaptive learning environment, Journal of Computer Assisted Learning, 10.1111/jcal.12483, 0, 0, (undefined).



(Batschelet,
and
(Cheung et al.,
for a range of values of
, and the first local minimum in
(Shamble et al., 








