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!
cats <- read_csv("cats_uk.csv")
cats_ref <- read_csv("cats_uk_reference.csv")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
cat_names <- cats %>%
select(tag_id) %>%
summarize(name = unique(tag_id)) %>%
arrange(name)
barney <- cats %>%
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.
gradientFunction <- colorRampPalette(c("white", "black"))
time_col = gradientFunction(dim(barney)[1])
barney <- barney %>%
mutate(nextLat = lead(location_lat),
nextLong = lead(location_long),
color = time_col)cat_map <- leaflet(barney) %>%
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_mapInstead, I’ll follow this stack overflow post which uses for loops.
cat_map2 <- leaflet(barney) %>%
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_map2Movement of Barney the cat around his neighborhood in July of 2017.