I have added an animation to see spatio-temporal distribution of crashes for a day in region of London.
How can I see the time of crash to better appreciate the animation? A simpler method is to plot crash number and time as text on heading or inside the plot. But thats not desired.
Can someone please help me to create a time axis at the bottom which has points representing each crash and corresponding datetime. Here is an example of desired timeline link. The point corresponding to the crash changes color in sync with animation below.
install.package("stats19")
library(stat19)
library(sf)
library(tidyverse)
library(stats19)
library(tmap)
library(tmaptools)
library(spData)
library(lubridate)
# crash date of UK
crashes_2019 <- get_stats19(year = 2019, type ="accidents")
# crash data for a day
crashes <- crashes_2019 %>%
drop_na() %>%
filter(date == as_date("2019-01-05")) %>%
st_as_sf(coords = c("longitude", "latitude"), crs = 4326)
# creating a polygon
my_polygon <- cycle_hire %>%
slice(1:5) %>%
st_buffer(dist = 10000) %>%
st_union()
# open street map of the region
bb <- st_bbox(my_polygon)
osm_map <- read_osm(bb)
# crash inside polygon
crashes_zone <- st_intersection(crashes,
my_polygon)
# animation
m1 <- lapply(seq_along(1:nrow(crashes_zone)), function(point){
x <- crashes_zone[point,] ## bracketted subsetting to get only 1 point
m1 <- tm_shape(my_polygon) +
tm_polygons() +
tm_shape(osm_map) +
tm_rgb(alpha = 0.6) +
tm_shape(x) + ## single point
tm_sf(col = 'red', size = 0.1)
})
# Render the animation
tmap_animation(m1, width = 300, height = 600, delay = 100)