library(tidyverse)
library(janitor)
library(praise)
library(leaflet)
Cats in the UK
The Data
The data this week comes from the Movebank for Animal Tracking Data via Data is Plural. Thanks @jthomasmock for the suggestion!
<- read_csv("cats_uk.csv")
cats <- read_csv("cats_uk_reference.csv") cats_ref
%>%
cats group_by(tag_id) %>%
summarize(count = n()) %>%
arrange(desc(count))
# A tibble: 101 × 2
tag_id count
<chr> <int>
1 Gracie_2-Tag 963
2 Bits-Tag 867
3 Teddy-Tag 791
4 Ginge-Tag 445
5 Maxwell-Tag 404
6 Tom-Tag 360
7 Bella-Tag 318
8 Jago2-Tag 307
9 Lightening Bugg-Tag 302
10 Winnie-Tag 279
# … with 91 more rows
<- cats %>%
cat_names select(tag_id) %>%
summarize(name = unique(tag_id)) %>%
arrange(name)
<- cats %>%
barney filter(tag_id == "Barney-Tag") %>%
mutate(start = min(timestamp)) %>%
mutate(time_out = (lubridate::minute(timestamp) - lubridate::minute(start))/60)
Where did Barney go?
I’d like to color of the lines to change over time, but there doesn’t seem to be a default way to do it using leaflet.
<- colorRampPalette(c("white", "black"))
gradientFunction = gradientFunction(dim(barney)[1])
time_col
<- barney %>%
barney mutate(nextLat = lead(location_lat),
nextLong = lead(location_long),
color = time_col)
<- leaflet(barney) %>%
cat_map addTiles() %>%
addPolylines(lng = ~location_long, lat = ~location_lat, opacity = 0.5) %>%
addCircles(lng = ~location_long, lat = ~location_lat, color = time_col, opacity = 0.1)
cat_map
Instead, I’ll follow this stack overflow post which uses for loops.
<- leaflet(barney) %>%
cat_map2 addTiles()
for(i in 1:nrow(barney)) {
<- cat_map2 %>%
cat_map2 addPolylines(lng = as.numeric(barney[i,c("location_long", "nextLong")]),
lat = as.numeric(barney[i, c("location_lat", "nextLat")]),
color = as.character(barney[i, c("color")]))
}
cat_map2
Movement of Barney the cat around his neighborhood in July of 2017.