...
 
Commits (2)
......@@ -68,7 +68,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that we have an interval column, we can go row by row and look at each location to figure out if more than one animal was seen at a station. This is the beginning of a cohort analysis."
"Now that we have an interval column, we can go row by row and look at each location to figure out if more than one animal was seen at a station. This is useful to find co-located detections."
]
},
{
......@@ -161,7 +161,8 @@
"abacus_plot <- filtered_detections %>% \n",
" filter(str_detect(station, \"HFX\") & !str_detect(station, \"lost\")) %>% # filter out everything not on the halifax line\n",
" plot_ly(x = ~detection_timestamp_utc, y = ~animal_id,type = \"scatter\", mode = \"markers\",text = ~station, marker=list(color = ~deploy_lat, colorscale=\"Viridis\", showscale=TRUE)) # Use the marker argument to color by latitude\n",
"embed_notebook(abacus_plot)"
"\n",
"abacus_plot"
]
},
{
......@@ -207,7 +208,7 @@
" layout(title = \"Detections on the Halifax Line\",geo = geo)\n",
"\n",
"\n",
"embed_notebook(map)"
"map"
]
},
{
......@@ -229,7 +230,7 @@
"metadata": {},
"outputs": [],
"source": [
"Sys.setenv('MAPBOX_TOKEN' = 'Your token here')"
"Sys.setenv('MAPBOX_TOKEN' = 'your token here')"
]
},
{
......@@ -258,8 +259,15 @@
" lon = ~median(longitude))\n",
" ))\n",
"\n",
"embed_notebook(mapbox)"
"mapbox"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
......
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Network Analysis\n",
"\n",
"Network Analysis, based in graph theory, provides a means to easily interpret and analyze the linked connections between individual movements and receivers [(Griffen et al., 2018).](https://www.sciencedirect.com/science/article/pii/S0165783618301073?via%3Dihub#fig0025) Network analysis in general is being applied to biotelemetry by looking at the relations within animal movements as a social network [(Finn et al., 2014).](https://www.sciencedirect.com/science/article/pii/S0304380013006017)\n",
"\n",
"Pull in the necessary libraries:"
]
},
{
"cell_type": "code",
"execution_count": null,
......@@ -8,10 +19,16 @@
"source": [
"library(dplyr)\n",
"library(glatos)\n",
"library(stringr)\n",
"library(plotly)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We are going to load the sample walleye fish detections from the Great Lakes, filter them, and then compress them."
]
},
{
"cell_type": "code",
"execution_count": null,
......@@ -27,87 +44,93 @@
"detection_events <- glatos::detection_events(filtered_detections, location_col = 'station')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Below we create 2 data frames. One to create the network edges and the other to create the vertices."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# create a data frame of directed movements\n",
"detection_events %>% \n",
"arrange(first_detection) %>% \n",
"group_by(individual) %>% \n",
"mutate(to = lead(location)) %>% \n",
"mutate(to_latitude = lead(mean_latitude)) %>% \n",
"mutate(to_longitude = lead(mean_longitude)) %>% \n",
"group_by(location, to) %>%\n",
"summarise(moves = n(), \n",
"arrange(first_detection) %>% # Arrange in sequential order by time of arrival\n",
"group_by(individual) %>% # Group the data animal\n",
"mutate(to = lead(location)) %>% # Create a next location column by using the lead\n",
"mutate(to_latitude = lead(mean_latitude)) %>% # Create a next latitude column by using the lead\n",
"mutate(to_longitude = lead(mean_longitude)) %>% # Create a next longitude column by using the lead\n",
"group_by(location, to) %>% # Group by unique sets of movements/vertices\n",
"summarise(visits = n(), \n",
" latitude = mean(mean_latitude), \n",
" longitude=mean(mean_longitude), \n",
" to_latitude=mean(to_latitude),\n",
" to_longitude=mean(to_longitude),\n",
" res_time_seconds = mean(res_time_sec)\n",
" res_time_seconds = mean(res_time_sec) # Summarise the data into data frame of moves by counting them and averaging the the rest of the values\n",
" ) %>% \n",
"rename(from=location) %>%\n",
"na.omit() -> network_analysis_data\n",
"rename(from=location) %>% # Rename the originating station to 'from'\n",
"na.omit() -> network_analysis_data # Omit any rows with empty values\n",
"\n",
"# Create a data frame of receiver vertices.\n",
"receivers <- network_analysis_data %>% \n",
" group_by(from) %>%\n",
" summarise(\n",
" latitude = mean(latitude), \n",
" longitude = mean(longitude),\n",
" visits = sum(moves),\n",
" latitude = mean(latitude), \n",
" longitude = mean(longitude),\n",
" visits = sum(visits),\n",
" res_time_seconds = mean(res_time_seconds)\n",
" ) \n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Below we plot out the segments and verticews using plotly."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"geo <- list(\n",
" projection = list(type = 'azimuthal equal area'),\n",
" showland = TRUE,\n",
" showland = TRUE,\n",
" landcolor = toRGB(\"gray95\"),\n",
" countrycolor = toRGB(\"gray80\"),\n",
" showlakes =TRUE,\n",
" lakecolor = toRGB(\"#A0AAB4\"),\n",
" resolution = 50,\n",
" center = list(lat = ~median(latitude),\n",
" lon = ~median(longitude)),\n",
" lonaxis = list(range=c(~min(longitude)-1, ~max(longitude)+1)),\n",
" lataxis = list(range=c(~min(latitude)-1, ~max(latitude)+1))\n",
")\n",
"Sys.setenv('MAPBOX_TOKEN' = 'your toke here')\n",
"\n",
"network <- network_analysis_data %>%\n",
" plot_geo(height=800) %>%\n",
" plot_mapbox(height=800) %>%\n",
" add_segments(\n",
" x = ~longitude, xend = ~to_longitude,\n",
" y = ~latitude, yend = ~to_latitude,\n",
" alpha = 0.4, size = I(1.5), hoverinfo = \"none\", color=I(\"red\")\n",
" size = I(1.5), hoverinfo = \"none\", color=I(toRGB(\"#AA3939\"))\n",
" ) %>%\n",
" add_markers(\n",
" data=receivers,\n",
" x = ~longitude, y = ~latitude, text = ~paste(from, \":\", visits, ' visits & ', res_time_seconds, ' seconds of residence time on average'),\n",
" size = ~res_time_seconds, hoverinfo = \"text\", alpha = 0.5, color=~visits\n",
" size = ~res_time_seconds/mean(network_analysis_data$res_time_seconds) + 100, hoverinfo = \"text\", color=~visits\n",
" ) %>%\n",
" layout(\n",
" title = 'Walleye Salmon Network Plot',\n",
" geo = geo, showlegend = FALSE\n",
" showlegend = FALSE,\n",
" mapbox = list(zoom = 6,\n",
" center = list(lat = ~median(latitude),\n",
" lon = ~median(longitude)),\n",
" style='light'\n",
" )\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"embed_notebook(network)"
"Show the completed network graph."
]
},
{
......@@ -116,8 +139,7 @@
"metadata": {},
"outputs": [],
"source": [
"library(cowsay)\n",
"cowsay::say(\"Any questions?\",by=\"shark\",)"
"network"
]
},
{
......@@ -125,10 +147,14 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"library(cowsay)\n",
"cowsay::say(\"Any questions?\",by=\"shark\",)"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "R [conda env:anaconda3]",
"language": "R",
......@@ -141,6 +167,35 @@
"name": "R",
"pygments_lexer": "r",
"version": "3.4.1"
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
......