knitr::opts_chunk$set(warning = FALSE,
                      message = FALSE)
library(tidyverse)
library(lubridate)

theme_set(theme_classic(18) +
            theme(legend.position = "bottom"))

# download.file(url = 'https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-06-29/animal_rescues.csv', destfile = "./animal_rescues.csv")
animal_rescues <- readr::read_csv(file = "./animal_rescues.csv")
skimr::skim(animal_rescues)
Data summary
Name animal_rescues
Number of rows 7544
Number of columns 31
_______________________
Column type frequency:
character 26
numeric 5
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
date_time_of_call 0 1 16 16 0 7533 0
fin_year 0 1 7 7 0 14 0
type_of_incident 0 1 15 15 0 1 0
pump_count 0 1 1 4 0 5 0
pump_hours_total 0 1 1 4 0 13 0
incident_notional_cost 0 1 1 4 0 72 0
final_description 0 1 4 100 0 4556 0
animal_group_parent 0 1 3 55 0 28 0
originof_call 0 1 6 21 0 8 0
property_type 0 1 3 78 0 180 0
property_category 0 1 4 17 0 7 0
special_service_type_category 0 1 23 31 0 4 0
special_service_type 0 1 26 58 0 24 0
ward_code 9 1 4 9 0 710 0
ward 9 1 3 37 0 1254 0
borough_code 9 1 9 9 0 37 0
borough 9 1 5 22 0 70 0
stn_ground_name 0 1 4 16 0 107 0
uprn 0 1 4 11 0 1453 0
street 0 1 4 94 0 5804 0
usrn 0 1 4 8 0 5070 0
postcode_district 0 1 2 4 0 272 0
easting_m 0 1 4 6 0 3431 0
northing_m 0 1 4 6 0 3381 0
latitude 0 1 1 11 0 3633 0
longitude 0 1 1 12 0 3634 0

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
incident_number 3478 0.54 91854662.33 54836667.38 4149 49306118 89438626 131567118 233284091 ▆▇▇▅▂
cal_year 0 1.00 2014.88 3.68 2009 2012 2015 2018 2021 ▇▅▇▅▇
hourly_notional_cost 0 1.00 301.26 33.99 255 260 298 333 352 ▇▂▃▃▇
easting_rounded 0 1.00 531242.84 10528.50 500050 524750 531650 537750 571350 ▁▅▇▂▁
northing_rounded 0 1.00 180724.52 8348.27 157050 175150 181250 186750 200750 ▁▃▇▇▂
animal_rescues$animal_group_parent %>% table
## .
##                                                    Bird 
##                                                    1530 
##                                                  Budgie 
##                                                       2 
##                                                    Bull 
##                                                       1 
##                                                     cat 
##                                                      17 
##                                                     Cat 
##                                                    3649 
##                                                     Cow 
##                                                       8 
##                                                    Deer 
##                                                     130 
##                                                     Dog 
##                                                    1194 
##                                                  Ferret 
##                                                       8 
##                                                    Fish 
##                                                       2 
##                                                     Fox 
##                                                     349 
##                                                    Goat 
##                                                       2 
##                                                 Hamster 
##                                                      16 
##                                                Hedgehog 
##                                                       2 
##                                                   Horse 
##                                                     193 
##                                                    Lamb 
##                                                       2 
##                                                  Lizard 
##                                                       3 
##                                                  Pigeon 
##                                                       4 
##                                                  Rabbit 
##                                                      14 
##                                                   Sheep 
##                                                       6 
##                                                   Snake 
##                                                      13 
##                                                Squirrel 
##                                                      65 
##                                                Tortoise 
##                                                       1 
## Unknown - Animal rescue from below ground - Farm animal 
##                                                       1 
##        Unknown - Animal rescue from water - Farm animal 
##                                                       3 
##                        Unknown - Domestic Animal Or Pet 
##                                                     191 
##                        Unknown - Heavy Livestock Animal 
##                                                      49 
##                                   Unknown - Wild Animal 
##                                                      89

1 Selecting columns/cleaning up data for visualisation

subdata = animal_rescues %>% 
  dplyr::transmute(
    incident_number, 
    date_time_of_call = lubridate::dmy_hm(date_time_of_call),
    animal_type = animal_group_parent %>% tolower,
    animal_type = case_when(
      str_detect(animal_type, "farm animal") ~ "farm animal", 
      str_detect(animal_type, "heavy livestock animal") ~ "farm animal", 
      animal_type == "bull" ~ "cow",
      animal_type == "budgie" ~ "bird",
      animal_type == "pigeon" ~ "bird",
      # str_detect(animal_type, "unknown") ~ "unknown",
      TRUE ~ animal_type),
    animal_type = fct_lump(f = animal_type, n = 8),
    animal_type_int = animal_type %>% as.factor() %>% as.integer(),
    latitude = as.numeric(latitude), 
    longitude = as.numeric(longitude)) %>% 
  dplyr::filter(complete.cases(latitude), complete.cases(longitude), latitude > 0)

# subdata
# glimpse(subdata)
subdata$animal_type %>% table
## .
##                             bird                              cat 
##                              804                             1388 
##                             deer                              dog 
##                              122                              788 
##                              fox                            horse 
##                              211                              193 
## unknown - domestic animal or pet            unknown - wild animal 
##                               78                               35 
##                            Other 
##                               81
catdata = subdata %>% 
  dplyr::filter(animal_type == "cat")

2 Mapbox visualisation using plotly

Click legend to select for specific type of animals.

library(plotly)
Sys.setenv('MAPBOX_TOKEN' = Sys.getenv('MAPBOX_AUTH'))

fig = subdata %>% 
  plot_mapbox(lat = ~latitude, lon = ~longitude,
              split = ~animal_type, 
              size = 2,
              mode = 'scattermapbox', 
              hoverinfo = 'animal_type')

fig = fig %>% 
  layout(title = 'Rescue animal types',
         font = list(color='white'),
         plot_bgcolor = '#191A1A', 
         paper_bgcolor = '#191A1A',
         mapbox = list(style = 'dark', 
                       zoom = 7,
                       center = list(
                         lat = median(subdata$latitude),
                         lon = median(subdata$longitude))),
         legend = list(orientation = 'h',
                       font = list(size = 8)),
         margin = list(l = 25, r = 25,
                       b = 25, t = 25,
                       pad = 2)) 

fig

3 Mapbox visualisation using mapdeck

library(mapdeck)
set_token(Sys.getenv("MAPBOX_AUTH"))
# mapdeck(token = mapdeck(token = 'your_token'))
mapdeck::mapdeck(style = mapdeck_style('dark')) %>% 
  mapdeck::add_scatterplot(
    data = subdata,
    lat = "latitude",
    lon = "longitude",
    tooltip = "animal_type",
    radius = 200,
    fill_colour = "animal_type",
    palette = "viridis",
    legend = TRUE
  )