Maps with Time Slider?
Asked Answered
H

2

19

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
Homochromous answered 19/2, 2016 at 18:26 Comment(6)
here is an idea, as you suggested you could split your dataset by month and add these subsets as layers on your map (rstudio.github.io/leaflet/showhide.html); you can then click/unclick the layer you want to be shownRowel
There's an example here. The slider is at the top right corner of the map. (It's not an R implementation though...)Contravene
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.Homochromous
It's worth checking out mapview as well - see hereContravene
Here you probably need a different approach. Package rmaps allow to create animated choropleth (rmaps.github.io/blog/posts/animated-choropleths/index.html). Another option would be to build a shiny app...Non
Another alternative is to create a .gif showing the change over time. Take a look at the animation package.Non
V
4

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))
Varitype answered 15/9, 2021 at 12:57 Comment(0)
D
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

Decree answered 5/7, 2016 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.