Commit 27b9465f authored by Alex Nunes's avatar Alex Nunes

Updated notebooks for analysis and plotting

Changes to be committed:
	deleted:    Access and Analysis of Acoustic Telemetry Data.ipynb
	modified:   Part II - R Programming Language.ipynb
	modified:   Part III - Data Cleaning and Preprocessing.ipynb
	modified:   Part IV - Data Analysis.ipynb
parent 4b11e370
This diff is collapsed.
......@@ -123,6 +123,13 @@
"data <- read.csv(\"data//nsbs_matched_detections_2014.csv\")\n",
"stringr::str_detect(data$unqdetecid, \"release\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
......
......@@ -175,6 +175,13 @@
"filtered_detections <- nr_detections_with_filter %>% filter(passed_filter != FALSE)\n",
"filtered_detections"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
......
......@@ -21,6 +21,7 @@
"source": [
"library(dplyr)\n",
"library(glatos)\n",
"library(stringr)\n",
"\n",
"detections <- glatos::read_otn_detections(\"data/nsbs_matched_detections_2014.csv\") \n",
"detections <- detections %>% filter(!stringr::str_detect(unqdetecid, \"release\"))\n",
......@@ -34,7 +35,7 @@
"metadata": {},
"outputs": [],
"source": [
"detection_events <- glatos::detection_events(filtered_detections)\n",
"detection_events <- glatos::detection_events(filtered_detections, location_col = 'station')\n",
"detection_events"
]
},
......@@ -76,7 +77,6 @@
"metadata": {},
"outputs": [],
"source": [
" individual detection event\n",
"for(event in detection_events$event) {\n",
" \n",
" detection_events$overlaps_with[event] = paste( # We use paste to create a string of other events\n",
......@@ -122,16 +122,111 @@
"metadata": {},
"outputs": [],
"source": [
"detection_events %>% group_by(location) %>% summarise(detection_count = sum(num_detections),\n",
" num_unique_tags = n_distinct(individual),\n",
" total_residence_time_in_seconds = sum(detection_interval))"
"summary_data <- detection_events %>% group_by(location) %>% summarise(detection_count = sum(num_detections),\n",
" num_unique_tags = n_distinct(individual),\n",
" total_residence_time_in_seconds = sum(detection_interval),\n",
" latitude = mean(mean_latitude),\n",
" longitude = mean(mean_longitude))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plotting"
"## Plotting\n",
"\n",
"Plotting is necessary for most research and analysis. It's just an easier way for our brains to digest the information.\n",
"\n",
"We will be using [Plotly](https://plot.ly), a plotting package that allows for exports and interactivity.\n",
"\n",
"We will create an abacus plot first. An abacus plot will show us a timline of what animal is seen when by the receivers. We put ``detection_timestamp_utc`` on the x axis and the ``animal_id`` on the y axis"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"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",
"embed_notebook(abacus_plot)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Maps\n",
"\n",
"You can also plot your data against a map to give more geospatial context. Below is an example of a geospatial plot from plotly. The ``geo`` list sets the style and parameters for the map and will be passed into the layout function.\n",
"\n",
"The scope of the map will determine what boundaries are drawn. You can also change the projection of the map: https://plot.ly/r/reference/#layout-geo-projection"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"geo <- list(\n",
"# scope = 'north america',\n",
" showland = TRUE,\n",
" landcolor = toRGB(\"grey83\"),\n",
" subunitcolor = toRGB(\"white\"),\n",
" countrycolor = toRGB(\"white\"),\n",
" showlakes = TRUE,\n",
" lakecolor = toRGB(\"white\"),\n",
" showcountries = TRUE,\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",
"\n",
"map <- summary_data %>% \n",
" filter(str_detect(location, \"HFX\") & !str_detect(location, \"lost\")) %>% \n",
" plot_geo(lat = ~latitude, lon = ~longitude, color = ~detection_count, height = 900, width=1600 )%>%\n",
" add_markers(text = ~paste(location), hoverinfo = ~paste(location, ': ', detection_count,' detections')) %>%\n",
" layout(title = \"Detections on the Halifax Line\",geo = geo)\n",
"\n",
"\n",
"embed_notebook(map)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## MapBox\n",
"\n",
"Mapbox is a Live Location Platform that can serve up map tiles for use.\n",
"\n",
"You can create a free account and get an access token here: https://mapbox.com\n",
"\n",
"Below we set the access token as an environment variable that Plotly can call."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Sys.setenv('MAPBOX_TOKEN' = 'Your Token Here')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"From there, we can just call the ``plot_mapbox()`` function and pass whatever arguments we need for the map. https://plot.ly/r/scattermapbox/"
]
},
{
......@@ -140,7 +235,20 @@
"metadata": {},
"outputs": [],
"source": [
"glatos::abacus_plot(filtered_detections, location_col = 'animal_id')"
"mapbox <- summary_data %>% \n",
" filter(str_detect(location, \"HFX\") & !str_detect(location, \"lost\")) %>% \n",
" plot_mapbox(lat = ~latitude, lon = ~longitude, color = ~detection_count, size = , height = 900, width=1600) %>%\n",
" 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",
" )%>%\n",
" layout( mapbox = list(zoom = 7,\n",
" center = list(lat = ~median(latitude),\n",
" lon = ~median(longitude))\n",
" ))\n",
"\n",
"embed_notebook(mapbox)"
]
},
{
......
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