Commit 1ebc2fc2 authored by Alex Nunes's avatar Alex Nunes

Added network analysis

parent a0c3f119
......@@ -52,6 +52,13 @@
"data <- read.csv(\"data//nsbs_matched_detections_2014.csv\")\n",
"data\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
......
......@@ -129,6 +129,15 @@
" longitude = mean(mean_longitude))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"summary_data"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -150,8 +159,8 @@
"source": [
"library(plotly)\n",
"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",
" 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)"
]
},
......@@ -220,7 +229,7 @@
"metadata": {},
"outputs": [],
"source": [
"Sys.setenv('MAPBOX_TOKEN' = 'your token here')"
"Sys.setenv('MAPBOX_TOKEN' = 'Your token here')"
]
},
{
......@@ -242,7 +251,7 @@
" add_markers(\n",
" text = ~paste(location, ': ', detection_count,'detections', ' & ', total_residence_time_in_seconds, ' seconds of residence time'), \n",
" hoverinfo = \"text\",\n",
" size = ~c(detection_count/10 )# + total_residence_time_in_seconds/3600)\n",
" size = ~c(detection_count/10 + total_residence_time_in_seconds/3600)\n",
" )%>%\n",
" layout( mapbox = list(zoom = 7,\n",
" center = list(lat = ~median(latitude),\n",
......@@ -251,16 +260,6 @@
"\n",
"embed_notebook(mapbox)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"library(cowsay)\n",
"cowsay::say(\"Any questions?\",by=\"shark\",)"
]
}
],
"metadata": {
......
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"library(dplyr)\n",
"library(glatos)\n",
"library(stringr)\n",
"library(plotly)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"det_file <- system.file(\"extdata\", \"walleye_detections.csv\",\n",
" package = \"glatos\")\n",
"detections <- read_glatos_detections(det_file)\n",
"\n",
"detections <- glatos::false_detections(detections, tf = 3600)\n",
"filtered_detections <- detections %>% filter(passed_filter != FALSE)\n",
"detection_events <- glatos::detection_events(filtered_detections, location_col = 'station')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"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",
" latitude = mean(mean_latitude), \n",
" longitude=mean(mean_longitude), \n",
" to_latitude=mean(to_latitude),\n",
" to_longitude=mean(to_longitude)\n",
" ) %>% \n",
"rename(from=location) %>%\n",
"na.omit() -> network_analysis_data\n",
"\n",
"receivers <- network_analysis_data %>% \n",
" group_by(from) %>%\n",
" summarise(\n",
" latitude = mean(latitude), \n",
" longitude = mean(longitude),\n",
" n = sum(moves)\n",
" ) \n",
"\n",
"\n"
]
},
{
"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",
"\n",
"network <- network_analysis_data %>%\n",
" plot_geo(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",
" ) %>%\n",
" add_markers(\n",
" data=receivers,\n",
" x = ~longitude, y = ~latitude, text = ~paste(from, \":\", n),\n",
" size = ~n, hoverinfo = \"text\", alpha = 0.9, color=~n\n",
" ) %>%\n",
" layout(\n",
" title = 'Walleye Salmon Network Plot',\n",
" geo = geo, showlegend = FALSE\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"embed_notebook(network)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"library(cowsay)\n",
"cowsay::say(\"Any questions?\",by=\"shark\",)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "R [conda env:anaconda3]",
"language": "R",
"name": "conda-env-anaconda3-r"
},
"language_info": {
"codemirror_mode": "r",
"file_extension": ".r",
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "3.4.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment