基于纬度-经度的销售数据的交互式地图

时间:2018-08-28 13:57:54

标签: r shiny leaflet maps

我有一个只有3列的数据框,即纬度,经度和销售额。这些纬度和经度属于“沙特阿拉伯”。我想基于纬度和经度创建交互式销售热图。

类似闪亮的“ Zip-Explorer”示例

 http://shiny.rstudio.com/gallery/superzip-example.html

我尝试了各种示例

1. http://www.geo.ut.ee/aasa/LOOM02331/heatmap_in_R.html 

这不是交互式地图,因此请尝试使用Shiny-leaflet示例。

2. https://stackoverflow.com/questions/37881107/shiny-is-there-a-way-to-enable-mouse-wheel-zoom-only-after-click-on-map-in-shin?rq=1 

在这种情况下,将地区从美国更改为沙特阿拉伯或南非无济于事

 Country = map("world", fill = TRUE, plot = FALSE, regions="USA", exact=TRUE)

简而言之,我不知道如何在R中绘制交互式地图。

任何建议都会有所帮助。示例数据帧如下。

data <- data.frame(latitude=c(37.78,24.77,21.56,24.77,21.56,21.56,21.47,21.48,26.39,24.77),longitude=c(-100,46.74,39.19,46.74,39.2,39.2,39.23,39.19,49.98,46.74),sales=c(830,128,120,73,41,37,35,31,29,28))

1 个答案:

答案 0 :(得分:1)

Leaflet非常适合这种类型的映射,并且很容易针对这种用例进行扩展。仅靠纬度和经度点很难制作热图。在此示例中,我只是使用用colorFactor上色的圆圈来使其反映data$sales中的值:

library(leaflet)

data <- data.frame(latitude=c(37.78,24.77,21.56,24.77,21.56,21.56,21.47,21.48,26.39,24.77),
                   longitude=c(-100,46.74,39.19,46.74,39.2,39.2,39.23,39.19,49.98,46.74),
                   sales=c(830,128,120,73,41,37,35,31,29,28))

pal <- colorFactor(
  palette = 'Blues', domain = data$sales)

leaflet(data) %>% addTiles() %>%
  addCircleMarkers(lat = ~latitude, lng = ~longitude, color= ~pal(sales))

生成您的数据点图,这是重点领域:

enter image description here