综合运用

在 R 中使用 leaflet 构建交互式地图

Rich Majerus

Vice President of Strategy & Planning, Queens University of Charlotte

回顾

  • Leaflet 与 htmlwidgets
  • 底图
  • 圆形标记
  • 颜色调色板
  • 多边形
  • 图层
  • 修饰

北卡州对数收入分级统计图 2

北卡州对数收入分级统计图 3

在 R 中使用 leaflet 构建交互式地图

综合运用

leaflet() %>% 
  addTiles(group = "OSM") %>% 
  addProviderTiles("CartoDB", group = "Carto") %>% 
  addProviderTiles("Esri", group = "Esri") %>%

addPolygons(data = shp, weight = 1, fillOpacity = .75, color = ~nc_pal(log(mean_income)), label = ~paste0("Mean Income: ", dollar(mean_income)), group = "Mean Income") %>%
在 R 中使用 leaflet 构建交互式地图

综合运用

  addCircleMarkers(data = nc_public, radius = 2, 
                   label = ~htmlEscape(name), 
                   color = ~pal(sector_label),group = "Public") %>% 
  addCircleMarkers(data = nc_private, radius = 2, 
                   label = ~htmlEscape(name),
                   color = ~pal(sector_label), group = "Private") %>% 
  addCircleMarkers(data = nc_profit, radius = 2, 
                   label = ~htmlEscape(name),
                   color = ~pal(sector_label),
                   group = "For-Profit") %>% 

addLayersControl(baseGroups = c("OSM", "Carto", "Esri"), overlayGroups = c("Public", "Private", "For-Profit", "Mean Income"))
在 R 中使用 leaflet 构建交互式地图

综合运用

北卡州对数收入分级统计图 4

在 R 中使用 leaflet 构建交互式地图

保存地图

# Store leaflet map in object
m <- leaflet() %>% 
     addTiles() %>%
     addMarkers(data = ipeds,
                clusterOptions = markerClusterOptions()) %>% 
     addPolygons(data = shp)
# save leaflet object as html file
library(htmlwidgets)

saveWidget(m, file="myMap.html")
在 R 中使用 leaflet 构建交互式地图

Passons à la pratique !

在 R 中使用 leaflet 构建交互式地图

Preparing Video For Download...