18

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

Thanks in advance! Any guidance/insight would be greatly appreciated.

Riley Hun
  • 2,541
  • 5
  • 31
  • 77
  • 1
    here is an idea, as you suggested you could split your dataset by month and add these subsets as layers on your map (https://rstudio.github.io/leaflet/showhide.html); you can then click/unclick the layer you want to be shown – MLavoie Feb 19 '16 at 18:32
  • There's an example [here](http://dwilhelm89.github.io/LeafletSlider/). The slider is at the top right corner of the map. (It's not an R implementation though...) – jbaums Feb 19 '16 at 18:38
  • Thanks. I really like the ideas! I'm going to try using the layer suggestion. I'd prefer the time slider, but I think it's a java application, which sadly isn't in my field of knowledge. – Riley Hun Feb 19 '16 at 19:48
  • It's worth checking out `mapview` as well - see [here](http://environmentalinformatics-marburg.github.io/web-presentations/20150723_mapView.html) – jbaums Feb 20 '16 at 01:07
  • Here you probably need a different approach. Package `rmaps` allow to create animated choropleth (http://rmaps.github.io/blog/posts/animated-choropleths/index.html). Another option would be to build a shiny app... – G. Cocca Feb 20 '16 at 11:08
  • 1
    Another alternative is to create a .gif showing the change over time. Take a look at the `animation` package. – G. Cocca Feb 20 '16 at 11:25

2 Answers2

3

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))
1

Here is an answer that may be of help.

Alternatively, you could provide the time series of a point as a popup graph using mapview::popupGraph. It is also possible to provide interactive, htmlwidget based graphs to popupGraph

Community
  • 1
  • 1
TimSalabim
  • 5,604
  • 1
  • 25
  • 36