leaflet绘制地图

R语言
地图
可视化
leaflet包介绍
作者

不止BI

发布于

2024年4月1日

leaflet包是R语言中一个用于创建交互式地图的强大工具包。它基于Leaflet JavaScript库,可以轻松创建具有各种功能的地图,例如:

添加图层,如底图、标记、线、面等 控制地图的缩放、平移、旋转等操作 添加交互式控件,如搜索、图层控制等 支持多种地图类型,如 OpenStreetMap、Google Maps、高德地图等

基本用法

生成图层

library(leaflet)
library(leafletCN)
library(leaflet.extras)
library(tidyverse)
m <- leaflet() %>%
  # 添加高德地图底图
  amap(group = "高德") %>%
  # 设置地图中心和缩放级别
  setView(
    lng = 121.33739,
    lat = 31.13533,
    zoom = 12
  )
m

添加测距工具

m <- m %>% addMeasure(localization = "cn", primaryLengthUnit = "kilometers", secondaryLengthUnit = "meters")
m

添加地点图标

df_home_location <- tribble(
  ~Name, ~WGS84Longitude, ~WGS84Latitude,
  "地点1", 121.33739, 31.13533,
  "地点2", 121.33539, 31.15533
)
m <- m %>%
  addAwesomeMarkers(
    lng = ~WGS84Longitude,
    lat = ~WGS84Latitude,
    label = ~Name,
    popup = ~Name,
    group = "home",
    icon = awesomeIcons(icon = "home"),
    data = df_home_location,
    clusterOptions = markerClusterOptions(),
    clusterId = "storeCluster"
  )
m

添加地点半径范围

m <- m %>% addCircles(
  lng = ~WGS84Longitude,
  lat = ~WGS84Latitude,
  radius = 2000,
  label = ~Name,
  popup = ~Name,
  group = "home2km",
  data = df_home_location
)
m

添加新的图标图层

df_car <- tribble(
  ~Car, ~WGS84Longitude, ~WGS84Latitude, ~CarNo,
  "车辆1", 121.33439, 31.14533, "车牌1",
  "车辆2", 121.33039, 31.10533, "车牌2"
)
df_car <- df_car %>% mutate(info = str_c(Car, CarNo, sep = "<br/>"))
m <- m %>% addAwesomeMarkers(
  lng = ~WGS84Longitude,
  lat = ~WGS84Latitude,
  # label = ~ info,
  label = ~CarNo,
  popup = ~info,
  group = "car",
  # clusterOptions = markerClusterOptions(),
  # clusterId = "carCluster",
  icon = awesomeIcons(icon = "automobile", library = "fa", markerColor = "orange"),
  data = df_car
)
m

添加搜索功能

m <- m %>% addSearchFeatures(targetGroups = "home")
m

添加图层控制

m <- m %>% addLayersControl(
  baseGroups = "home",
  overlayGroups = c("car", "home", "home2km")
)
m

添加小地图

m <- m %>%
  addMiniMap()
m

形状地图

从geoJson读取形状

省份地图

library(geojson)
filePath <- system.file("geojson/china.json", package = "leafletCN")
china_geo_shape <- read.geoShape(filePath)
## 在geojson中加入自定义数据
china_geo_shape$randvalue <- runif(length(china_geo_shape$name), 0, 100)
china_geojson <- as.geojson(china_geo_shape)

leaflet() %>%
  addTiles() %>%
  setView(
    lng = 120.33739,
    lat = 31.13533, zoom = 3
  ) %>%
  amap() %>%
  addBootstrapDependency() %>%
  enableMeasurePath() %>%
  addGeoJSONChoropleth(
    china_geojson,
    valueProperty = "randvalue",
    labelProperty = "name",
    popupProperty = propstoHTMLTable(
      props = c("name", "size", "childNum", "randvalue"),
      table.attrs = list(class = "table table-striped table-bordered"),
      drop.na = TRUE
    ),
    color = "#ffffff", weight = 1, fillOpacity = 0.7,
    highlightOptions = highlightOptions(
      weight = 2, color = "#000000",
      fillOpacity = 1, opacity = 1,
      bringToFront = TRUE, sendToBack = TRUE
    )
  ) %>%
  addPulseMarkers(
    lng = 116.4551, lat = 40.2539,
    label = "This is a label",
    icon = makePulseIcon(heartbeat = 0.5)
  )

城市地图

filePath <- system.file("geojson/city.json", package = "leafletCN")
city_geo_shape <- read.geoShape(filePath)
## 在geojson中加入自定义数据
city_geo_shape$randvalue <- runif(length(city_geo_shape$name), 0, 100)
city_geojson <- as.geojson(city_geo_shape)

city_geo_map <- leaflet() %>%
  addTiles() %>%
  setView(
    lng = 120.33739,
    lat = 31.13533, zoom = 3
  ) %>%
  amap() %>%
  addBootstrapDependency() %>%
  enableMeasurePath() %>%
  addGeoJSONChoropleth(
    city_geojson,
    valueProperty = "randvalue",
    labelProperty = "name",
    popupProperty = propstoHTMLTable(
      props = c("id", "name", "randvalue"),
      table.attrs = list(class = "table table-striped table-bordered"),
      drop.na = TRUE
    ),
    color = "#ffffff", weight = 1, fillOpacity = 0.7,
    highlightOptions = highlightOptions(
      weight = 2, color = "#000000",
      fillOpacity = 1, opacity = 1,
      bringToFront = TRUE, sendToBack = TRUE
    ), popupOptions = popupOptions(noHide = TRUE)
  ) %>%
  addFullscreenControl() %>%
  addDrawToolbar(
    targetGroup = "draw",
    editOptions = editToolbarOptions(
      selectedPathOptions = selectedPathOptions()
    )
  ) %>%
  addLayersControl(
    overlayGroups = c("draw"),
    options = layersControlOptions(collapsed = FALSE)
  ) %>%
  addStyleEditor()
city_geo_map

热力地图

df_location <- tribble(
  ~Name, ~WGS84Longitude, ~WGS84Latitude, ~value,
  "地点1", 121.33739, 31.13533, 100,
  "地点2", 121.33539, 31.15533, 300
)
leaflet(df_location) %>%
  # 添加高德地图底图
  amap(group = "高德") %>%
  # 设置地图中心和缩放级别
  setView(
    lng = 121.33739,
    lat = 31.13533,
    zoom = 12
  ) %>%
  addHeatmap(
    lng = ~WGS84Longitude, lat = ~WGS84Latitude, intensity = ~value,
    blur = 20, max = 0.05, radius = 15
  )

轨迹地图

# 生成轨迹数据
latlngs <- data.frame(lng = sort(rnorm(
  n = 100, mean = 120.33739, sd = 0.1
)), lat = sort(rnorm(
  n = 100, mean = 31.13533, sd = 0.1
)))

leaflet() %>%
  amap(group = "高德") %>%
  setView(lng = 120.33739, lat = 31.13533, zoom = 10) %>%
  addPolylines(
    data = latlngs,
    lng = ~lng,
    lat = ~lat,
    color = "blue",
    weight = 5,
    dashArray = "5,10" # 设置虚线
  )
回到顶部