Animate map in R with leaflet and xts

☆樱花仙子☆ 提交于 2019-12-09 06:43:00

问题


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


回答1:


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)


来源:https://stackoverflow.com/questions/30370840/animate-map-in-r-with-leaflet-and-xts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!