R:带有时间滑块的地图?

R: Maps with Time Slider?

有没有办法在 R 中为 Leaflet 或任何其他交互式地图库实现时间滑块?我有按时间序列排列的数据,并希望将其集成到 "motion" 地图中,其中绘图点随时间动态变化。

我正在考虑将我的数据分成几部分,使用子集来捕获每个月的相应数据 table。但是我如何在对应于不同月份的不同数据集之间移动?

就目前而言,我取了平均值并绘制了这些点,但我宁愿制作一张整合时间序列的地图。

到目前为止,这是我的代码:

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

提前致谢!任何 guidance/insight 将不胜感激。

是一个可能有帮助的答案。

或者,您可以使用 mapview::popupGraph 提供点的时间序列作为弹出图。还可以向 popupGraph

提供基于 htmlwidget 的交互式图表

认识到这是一个非常古老的问题,万一还有人想知道...

leaflet.extras2 有一些可能有用的功能。这是一个示例,它使用一些 tidyverse 函数、sfleaflet.extras2::addPlayback() 在渥太华附近生成一些有趣的 GPS 轨迹并制作动画。

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