Animate map in R with leaflet and xts

Felipe picture Felipe · May 21, 2015 · Viewed 7.2k times · Source

I would like to build an animated map with a time cursor in R.

I have time series (xts) that I would like to represent on map.

library(xts)
library(leaflet)
date<-seq(as.POSIXct("2015-01-01"), as.POSIXct("2015-01-10"), by=86400)
a<-xts(1:10,order.by=date)
b<-xts(5:14,order.by=date)
df = data.frame(Lat = 1:10, Long = rnorm(10),Id=letters[1:10])
leaflet() %>% addCircles(data = df,popup =df$Id)
#popup =paste(df$Id, xts value)  time cursor on the map

Is there a way to do this with the leaflet package? I didn't try rmaps package yet.

Thanks

EDIT:https://github.com/skeate/Leaflet.timeline

Answer

Felipe picture Felipe · Aug 10, 2016

There is a simple example

Library:

library(shiny)
library(xts)
library(leaflet)
library(dplyr)

Data:

date<-seq(as.Date("2015-01-01"), as.Date("2015-01-10"), by="day")
a<-xts(1:10,order.by=date)
df = data.frame(Lat = rnorm(1)+10, Long = rnorm(1),Id=a)

data_a<-data.frame(a)
data_a1<-data_a %>%  
  mutate("Lat" =as.numeric(df[1,1]),"Long"=as.numeric(df[2,1]),"Date"=rownames(data_a))

shinyapp:

 ui <- fluidPage(
  sliderInput("time", "date",min(date), 
          max(date),
          value = max(date),
          step=1,
          animate=T),
  leafletOutput("mymap")
)

server <- function(input, output, session) {
  points <- reactive({
      data_a1 %>% 
       filter(Date==input$time)
   })

   output$mymap <- renderLeaflet({
    leaflet() %>%
    addMarkers(data = points(),popup=as.character(points()$a))
   })
}

shinyApp(ui, server)