knitr::opts_chunk$set(
  fig.width = 7,
  collapse = TRUE,
  comment = "#>"
)
library(ggplot2)
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.

For this tutorial we will use a single bird in the included tracks dataset.

data("tracks")

head(tracks)

The MakeTrip function requiers a column with distances to the colony for each point. The Dist2Colony function will calculate these distances.

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 treshold distance DistCutOff away from the colony and the points where the animal returns within that threshold and annotates the data.frame with two new column, one indicating those "In" and "Out" points and another with trip numbers.

tracks_w_trips<-trakR::MakeTrip(tracks = tracks,
                                ID = "CaptureID",
                                DistCutOff = 0.1,
                                Dist2Colony = "Dist2Col",
                                NumLocCut = 3)
head(tracks_w_trips)

You can see that the track was split into multiple 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 leg. This can be done tediously by anotating each trip by hand or you can try using the InOutPoints function in trakR.

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")

Extract Depth along a track

Sometimes it is helpful to attach sea floor depth or terrestial elevation to a location or series of locations (track). The ETOPO1 dataset can be accessed with the marmap package witht he get.NOAA.bathy function. This is a wrapper around that function that downloads the grid of elevation/depth data from the NOAA servers and then extracts the value for each cell that a point over lays and returns a vector of the same length as the data.

min(tracks_w_trips$Longitude)
max(tracks_w_trips$Longitude)
min(tracks_w_trips$Latitude)
max(tracks_w_trips$Latitude)
tracks_w_trips$depth<-DepthETOPO(tracks,
                     dataLat="Latitude",
                     dataLon="Longitude",
                     left=min(tracks_w_trips$Longitude),
                     right= max(tracks_w_trips$Longitude),
                     top=min(tracks_w_trips$Latitude),
                     bottom=max(tracks_w_trips$Latitude),resolution = 1, keep=F)

# plot
ggplot(tracks_w_trips[tracks_w_trips$TripNum%in%c(5,7),],
       aes(Longitude,Latitude,col=depth))+
  geom_point(size=1)+
  scale_color_viridis_c()+
  facet_wrap(~TripNum)+
  theme_classic(base_size = 16)+labs(color="TripNum")

Label events

Sometimes it is useful to number events consecutively such as forageing events for a seabird. often times these events have multiple rows per event/bout. This function numbers these evens consecutively by bird and by trip nested within bird. The function label_event is writen just for this purpose. In this example we will label foraging events (in this case when birds drop below 5 km/h) with a consecutive number. It returns a data.frame with two new columns event_trip and even_bird

tracks_w_trips$dtime<-InterpointTime(tracks_w_trips,ID = 'CaptureID',DateTime ='DateTime' )
tracks_w_trips$ddist<-InterpointDist(tracks_w_trips,ID = 'CaptureID',lat = 'Latitude',lon = 'Longitude' )
tracks_w_trips$ddist<-tracks_w_trips$ddist 
tracks_w_trips$speed<-Speed(Dist = tracks_w_trips$ddist,Time = tracks_w_trips$dtime)
head(tracks_w_trips)
hist(tracks_w_trips$speed,breaks=300)
tracks_w_trips$foraging<-tracks_w_trips$speed<5&tracks_w_trips$Dist2Col>10
tracks_w_trips<-label_events(dat=tracks_w_trips[!is.na(tracks_w_trips$foraging),],
                                                               BirdID = 'CaptureID',
                                                               TripID = 'TripNum',
                                                               eventCol = 'foraging')
head(tracks_w_trips)

ggplot(tracks_w_trips[tracks_w_trips$TripNum%in%c(5,7),],
       aes(Longitude,Latitude,col=factor(event_bird)))+
  geom_point(size=1)+
  scale_color_viridis_d()+
  facet_wrap(~TripNum)+
  theme_classic(base_size = 16)+labs(color="TripNum")


abfleishman/trakR documentation built on Nov. 21, 2022, 7:59 a.m.