library(tidyverse)
## ── Attaching packages ──────────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.1     ✓ purrr   0.3.4
## ✓ tibble  3.0.1     ✓ dplyr   1.0.0
## ✓ tidyr   1.1.0     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.5.0
## ── Conflicts ─────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(tidytuesdayR)
library(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(chron)
## NOTE: The default cutoff when expanding a 2-digit year
## to a 4-digit year will change from 30 to 69 by Aug 2020
## (as for Date and POSIXct in base R.)
## 
## Attaching package: 'chron'
## The following objects are masked from 'package:lubridate':
## 
##     days, hours, minutes, seconds, years
library(ggthemes)
library(leaflet)
library(RColorBrewer)

individuals <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-06-23/individuals.csv')
## Parsed with column specification:
## cols(
##   animal_id = col_character(),
##   sex = col_character(),
##   life_stage = col_character(),
##   pregnant = col_logical(),
##   with_calf = col_logical(),
##   death_cause = col_character(),
##   study_site = col_character(),
##   deploy_on_longitude = col_double(),
##   deploy_on_latitude = col_double(),
##   deploy_on_comments = col_character(),
##   deploy_off_longitude = col_double(),
##   deploy_off_latitude = col_double(),
##   deploy_off_type = col_character(),
##   deploy_off_comments = col_character()
## )
locations <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-06-23/locations.csv')
## Parsed with column specification:
## cols(
##   event_id = col_double(),
##   animal_id = col_character(),
##   study_site = col_character(),
##   season = col_character(),
##   timestamp = col_datetime(format = ""),
##   longitude = col_double(),
##   latitude = col_double()
## )
#Separate the timestamp column into year, month, day, time columns.

locations_timeformatted <- locations %>% 
  mutate(year = year(timestamp),
         month = month(timestamp),
         day = day(timestamp),
         time = chron::times(strftime(timestamp,"%H:%M:%S", tz = "UTC")))
#Filter on the year and season.

locations_map <- locations_timeformatted %>% 
  filter(year == 2010:2016,
         season == "Winter")
## Warning in year == 2010:2016: longer object length is not a multiple of shorter
## object length
#Set color palate to use in leaflet addCircleMarkers to identify study_site.

pal <- colorFactor(
  palette = 'Dark2',
  domain = locations_map$study_site)

leaflet(locations_map) %>% 
  addTiles() %>% 
  addCircleMarkers(lat = ~latitude,
                   lng = ~longitude,
                   popup = paste("<b>Animal_ID:</b>", locations_map$animal_id,"</br>",
                                 "<b>Study Site:</b>", locations_map$study_site, "</br>",
                                 "<b>Timestamp:</b>", locations_map$timestamp),
                   color = ~pal(study_site))