Search code examples
rr-leaflet

Maps with Time Slider?


Is there a way to implement a time slider for Leaflet or any other interactive map library in R? I have data arranged in a time series, and would like to integrate that into a "motion" map where the plot points change dynamically over time.

I was thinking of breaking my data into pieces, using subset to capture the corresponding data table for each month. But how would I move between the different data sets corresponding to different months?

As it stands now, I took the average and plotted those points, but I'd rather produce a map that integrates the time series.

Here is my code so far:

data<-read.csv("Stericycle Waste Data.csv")
library(reshape2)
library(ggplot2)
library(plyr)
library(ggmap)
names(data)<-c("ID1","ID2", "Site.Address", "Type", "City", "Province", "Category", "Density", "Nov-14", "Dec-14", "Jan-15", "Feb-15", "Mar-15", "Apr-15", "May-15", "Jun-15", "Jul-15", "Aug-15", "Sep-15", "Oct-15", "Nov-15", "Dec-15", "Jan-16")
data<-melt(data, c("ID1","ID2", "Site.Address","Type", "City", "Province", "Category", "Density")) 
data<-na.omit(data)
data_grouped<-ddply(data, c("Site.Address", "Type","City", "Province", "Category", "Density", "variable"), summarise, value=sum(value))
names(data_grouped)<-c("Site.Address", "Type", "City", "Province", "Category", "Density", "Month", 'Waste.Mass')

dummy<-read.csv('locations-coordinates.csv')
geodata<-merge(data_grouped, dummy, by.x="Site.Address", by.y="Site.Address", all.y=TRUE)

library(leaflet)
d = geodata_avg$density_factor
d = factor(d)
cols <- rainbow(length(levels(d)), alpha=NULL)
geodata_avg$colors <- cols[unclass(d)]
newmap <- leaflet(data=geodata_avg) %>% addTiles() %>%
addCircleMarkers(lng = ~lon, lat = ~lat, weight = 1, radius = ~rank*1.1, color = ~colors,  popup = paste("Site Address: ", geodata_avg$Site.Address, "<br>", "Category: ", geodata_avg$Category, "<br>", "Average Waste: ", geodata_avg$value))
newmap

Solution

  • Recognizing this is a very old question, in case anyone's still wondering...

    The package leaflet.extras2 has some functions that might help. Here's an example that uses some tidyverse functions, sf, and leaflet.extras2::addPlayback() to generate and animate some interesting GPS tracks near Ottawa.

    library(magrittr)
    library(tibble)
    library(leaflet)
    library(leaflet.extras2)
    library(sf)
    library(lubridate)
    
    # how many test data points to create
    num_points <- 100
    
    # set up an sf object with a datetime column matching each point to a date/time
    # make the GPS tracks interesting
    df <- tibble::tibble(temp = (1:num_points),
                         lat = seq(from = 45, to = 46, length.out = num_points) + .1*sin(temp),
                         lon = seq(from = -75, to = -75.5, length.out = num_points) + .1*cos(temp),
                         datetime = seq(from = lubridate::ymd_hms("2021-09-01 8:00:00"),
                                        to = lubridate::ymd_hms("2021-09-01 9:00:00"),
                                        length.out = num_points)) %>%
      sf::st_as_sf(coords = c("lon", "lat"), crs = "WGS84", remove = FALSE)
    
    # create a leaflet map and add an animated marker
    leaflet() %>%
      addTiles() %>%
      leaflet.extras2::addPlayback(data = df,
                                   time = "datetime",
                                   options = leaflet.extras2::playbackOptions(speed = 100))