-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
210 lines (169 loc) · 9.5 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
---
title: "Basic Trip Segmentation"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Trip segmentation for animal tracking data
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.6588612.svg)](https://doi.org/10.5281/zenodo.6588612)
![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/abfleishman/trakR.svg?color=blue&label=Version)
Cite as:
Fleishman, A.B., Orben, R.A., and Gilmour, M. E. 2022. trakR: Basic Animal Tracking Data Analysis Tools. Version 0.0.10. GitHub repository. <https://github.com/abfleishman/trakR> DOI:10.5281/zenodo.6588612
```{r load packages,echo=TRUE,results='hide',message=FALSE}
library(ggplot2)
library(maps)
library(mapdata)
library(dplyr)
library(stringr)
library(lubridate)
```
# Install trakR package
```{r install trakR}
# install.packages("devtools") # for installing packages from github
# devtools::install_github("abfleishman/trakR",upgrade = "ask") # install my package
library(trakR)
```
# Load tracks
This should be one file for all the tracks or create a data.frame with all the tracks.
The function `MakeTrip` is set up for processing multiple animals at once, but you
can use it one a single track as well, it is just not as elegant.
For this tutorial we will use a single bird in the included `tracks` dataset.
```{r load data}
data("tracks")
head(tracks)
```
The `MakeTrip` function requires a column with distances to the colony for each point. The `Dist2Colony` function will calculate these distances. It requires the `argosfilter` package to run.
```{r dist2col}
tracks$Dist2Col<-trakR::Dist2Colony(tracks = tracks,
dataLat = "Latitude",
dataLon = "Longitude",
ColonyLat = 56.60329,
ColonyLong = -169.6760)
```
# Split Trips
The `MakeTrip` function finds the points where a animal moves some threshold distance `DistCutOff` away from the colony and the points where the animal returns within that threshold that is at least some number of points long `NumLocCut`. The function annotates the dataframe with two new column, one indicating those "In" and "Out" points and another with trip numbers.
```{r make trips}
tracks_w_trips<-trakR::MakeTrip(tracks = tracks,
ID = "CaptureID",
DistCutOff = 0.1,
Dist2Colony = "Dist2Col",
NumLocCut = 3)
head(tracks_w_trips)
# Plot a bird to check
ggplot(tracks_w_trips,
aes(Longitude,
Latitude,
col=factor(TripNum)))+
geom_path(size=.7)+
geom_point(data=tracks_w_trips[tracks_w_trips$ColonyMovement%in%c("Out","In"),])+
theme_classic(base_size = 16)+
labs(color="TripNum")
```
## trip segmentation
Another common task in processing tracking data is to segment a trip into a outbound, middle, and inbound legs. This can be done tediously by annotating each trip by hand or you can try using the `InOutPoints` function in `trakR`. This function also relies on having a columns with the distance to the colony. There are also a few more esoteric parameters that take some fiddling to get right.
From the function documentation that can be accessed by `?InOutPoints`:
This function's intention is to partition a track into an inbound and outbound leg demarcated when the bird first stops moving away from the colony. The first stopping point is identified by taking the change in dist2col first derivative, smoothing it with a left aligned rolling mean, and rounding to 1 digit. The first point where the bird is not changing the distance to the colony is the end of the outbound leg, while the last point where the birds is not getting close to the colony is the inbound leg.
Trip departure and return segments are identified by calculating delta-d
, the change in distance from the colony, lagged by `lag` gps points, and smoothed
by calculating a `nPointsToSmooth` left aligned rolling mean. These values are rounded
to 1 significant digit. Trips are segmented by assigning points between the
beginning of a trip and the first point delta-d which equaled zero as the “out” leg
and the last point delta-d that equaled zero until the final point of the trip as
the “in” leg. The colony is buffered so that the first and last points delta-d
can not be within `minDist2Col` km of the island. If there is no point on the trip
that delta-d reached zero, i.e. the bird continuously moved away from the colony
until it began to return, the trip is split at the apex point where the bird
was furthest from the colony. This method effectively finds the first and last
points that the bird ceases to move away from the colony over a given period the duration of which is dictated by
`nPointsToSmooth`*interpoint duration.
```{r trip-spliting}
tracks_w_trips$INOut<-InOutPoints(tracks=tracks_w_trips,
CaptureID="CaptureID",
DateTime="DateTime",
TripID="TripNum",
dist2colony="Dist2Colony",
lag=3,
nPointsToSmooth =10,
minDist2Col = 10,
Lon = "Longitude",
Lat="Latitude",
Plot = T,
pdfName = "inout_plots.pdf")
ggplot(tracks_w_trips[tracks_w_trips$TripNum%in%c(5,7),],
aes(Longitude,
Latitude,
col=factor(TripNum)))+
geom_path(aes(lty=INOut),size=1)+
scale_linetype_manual(values = c(1,2,3))+
facet_wrap(~TripNum)+
theme_classic(base_size = 16)+
labs(color="TripNum")
```
## Trip (foraging location) fidelity
Sometimes you might be interested in understanding whther or not an animal returns to the same areas over multiple trips. Below we will calculate a simple metrics per birds of fidelity.
This function compares maximum displacement (distance and angle) of two separate
trips from a point of origin. Multiple displacement values can be compared.
These displacement values are centered and scaled to create an index of site
fidelity that ranges -1 to 1. A positive value mean trips are similar and a negative value means the are different.
Used for calculation of Fidelity Index as described by: Gilmour, ME, JA
Castillo-Guerrero, AB Fleishman, S Hernandez-Vazquez, HS Young, SA Shaffer.
2018. Plasticity of foraging behaviors in response to diverse environmental
conditions. Ecosphere 9(7):e02301. https://doi.org/10.1002/ecs2.2301 (open
access)
The equations used here are modified from:
- Willis-Norton et al. 2015. Deep-Sea Research Part II: Topical Studies in Oceanography 113:260-267
- Hazen et al. 2016. Journal of Applied Ecology 54:1415-1428, and
- Shaffer et al. 2017. Movement Ecology 5:27 https://doi.org/10.1186/s40462-017-0118-9.
```{r add-bear}
# add a colony site name and join on inf0 about that site. this is set up for
# having multiple capture sites in a single dataframe
tracks_w_trips$site<-"Village"
capture_sites<-data.frame(site="Village",
colony_lon=-169.6760,
colony_lat= 56.60329,
stringsAsFactors = F)
# join on the capture sites and calculate the bearing from each point to the colony
library(geosphere)
tracks_w_trips<-tracks_w_trips %>%
left_join(capture_sites,by="site") %>%
mutate(bear2col=geosphere::bearing(p1 = cbind(colony_lon,colony_lat),
p2 = cbind(Longitude,Latitude)))
```
#### create the fidelity index.
This returns a list with two dataframes. one with all the data used to calculate the index and one row per combination of trips for each animal. This dataframe also has the fidelity index (FI colomn) for each pair of trips. The second data.frame i the list has a mean fidelity index per birds with the total N trip combinations used to calculate the mean.
```{r fidelity}
# remove "trips" with TripNum==0 because these are actually times when the bird was at the colony
tracks_with_trips_fidelity<-tracks_w_trips %>% filter(TripNum!=0)
# run fidelity
fidelity<-trakR::gilmour_fidelity(dat = tracks_with_trips_fidelity,
Longitude = "Longitude",
Latitude = "Latitude",
animal_id = "CaptureID",
trip_id = "TripNum",
distance2colony = "Dist2Colony",
bear2col = "bear2col")
# rose plot of fidelity index per trip combo (this is just a single bird)
ggplot(fidelity$fidelity_index_trips)+
geom_histogram(aes(x=FI),binwidth = .25)+
coord_polar() +
theme_minimal()
fidelity$fidelity_index_animal
```
### Attach depth info
```{r depth}
tracks_w_trips$depth<-DepthETOPO(tracks = tracks_w_trips,
dataLat = "Latitude",
dataLon = "Longitude",
left = min(tracks_w_trips$Longitude)-1,
right= max(tracks_w_trips$Longitude)+1,
bottom= min(tracks_w_trips$Latitude)-1,
top= max(tracks_w_trips$Latitude)+1,
resolution=30,
keep=F)
ggplot(tracks_w_trips %>%
filter(TripNum!=0),
aes(x=DateTime,y=depth, col=factor(TripNum)))+
geom_path()+
theme_minimal()
```