raw <- tidytuesdayR::tt_load(2020, 26)
## --- Downloading #TidyTuesday Information for 2020-06-23 ----
## --- Identified 2 files available for download ----
## --- Downloading files ---
## --- Download complete ---
ind<-raw$individuals

#filter the location data to a representative 3 years
#add year and month
loc <- raw$locations%>% 
  mutate(datestamp = as.Date(timestamp),
         year = lubridate::year(datestamp)) %>% 
  filter(year %in% c(2011, 2012, 2013)) %>% 
  left_join(ind %>% select(animal_id, sex))
## Joining, by = "animal_id"
#find the top animals by number of observations
top_bou<-loc %>% group_by(animal_id) %>% 
  count() %>% 
  arrange(desc(n)) %>% 
  ungroup() %>% 
  top_n(5,n)

#filter the loc data for the top animals
top_bou_pts<-loc %>% filter(animal_id %in%top_bou$animal_id) %>% 
  left_join(ind %>% select(animal_id, sex)) %>% 
  arrange(datestamp)
## Joining, by = c("animal_id", "sex")
#get a bounding box based on the lat and lon ranges in the data
bc_box <- ggmap::make_bbox(data = loc, lon = longitude, lat = latitude)

#download
bc_stamen <-ggmap::get_stamenmap(maptype = 'watercolor',
                                 bbox = bc_box,
                                 # bbox = c(left = -124.1616,bottom = 54, right = -119.75, top = 56), 
                                 crop = TRUE,zoom = 7)
## Source : http://tile.stamen.com/watercolor/7/19/39.jpg
## Source : http://tile.stamen.com/watercolor/7/20/39.jpg
## Source : http://tile.stamen.com/watercolor/7/21/39.jpg
## Source : http://tile.stamen.com/watercolor/7/19/40.jpg
## Source : http://tile.stamen.com/watercolor/7/20/40.jpg
## Source : http://tile.stamen.com/watercolor/7/21/40.jpg
bou_map <-ggmap::ggmap(bc_stamen)+
  stat_density2d(aes(x = longitude, 
                     y = latitude, 
                     # color=study_site,
                     alpha = stat(level)),
                 fill = "darkblue",
                 geom = "polygon",
                 bins = 20, 
                 data = loc,
                 show.legend = FALSE) +
  geom_line(data = subset(loc, animal_id %in% top_bou$animal_id),
             aes(x=longitude, y=latitude, color=animal_id))+
  facet_grid(rows = vars(year), 
             cols = vars(season),
             shrink = TRUE,switch = "y")+
  theme_void()+
  theme(legend.position = "bottom")+
  labs(colour = "Animal ID",
       title = "Range of Caribou in British Columbia",
       subtitle = "including path data for top 5 'bou",
       caption = "Seip DR, Price E (2019) Data from: Science update for the South Peace Northern Caribou\n(Rangifer tarandus caribou pop. 15) in British Columbia.\nMovebank Data Repository. https://doi.org/10.5441/001/1.p5bn656k\n
       viz: @WireMonkey Alyssa Goldberg\n
       TidyTuesday2020 week 26")+
  # scale_color_viridis_d()+
  NULL

ggsave("caribou_range.png",width = 11, height = 11)
bou_map