R语言:leafletCN的应用


leafletCN
1、安装

## 稳定版
install.packages("leafletCN")
## 开发版
devtools::install_github("lchiffon/leafletCN")

主函数介绍:
regionNames:找地名函数,极其方便
demomap:有地名就给你输出一个交互、动态的区域图,真方便
geojsonMap:作用是分区块标色

2、主函数介绍
(1)regionNames函数(regionNames的粒度是省级、县级)

regionNames("北京")
 [1] "密云县"   "怀柔区"   "房山区"   "延庆县"   "门头沟区" "昌平区"   "大兴区"  
 [8] "顺义区"   "平谷区"   "通州区"   "朝阳区"   "海淀区"   "丰台区"   "石景山区"
[15] "西城区"   "东城区"   "宣武区"   "崇文区"

(2)主函数——demomap:区域式地图

library(rgeos)
demomap("china")
demomap("北京")


(3)主函数——geojsonmap:地图标色函数

pal <- substr(rainbow(34),1,7)  #颜色,彩虹色
dat = data.frame(name = regionNames("china"),
                 value = 1:34)
geojsonMap(dat,"china",pal=pal,fillOpacity = 0.2)

pal <- substr(rainbow(18),1,7)
dat = data.frame(name = regionNames("北京"),
                 value = 1:18)
geojsonMap(dat,"北京",pal=pal,fillOpacity = 0.2)

3、辅助函数
1)辅助函数——amap:高德地图底图

leaflet() %>%   amap()

(2)辅助函数——read.geoShape:genjson格式转化

  if(require(sp)){
  filePath = system.file("geojson/china.json",package = "leafletCN")
  map = read.geoShape(filePath)
  plot(map)
}

读入了china.json格式的内容。
(3)辅助函数——leafletGeo:地图+小显示框
(1)单点标注:地图+标点

leaflet() %>%
  amap() %>%  
  addMarkers(lng=116.41, lat=39.88 ,popup="天坛")
# 经度:lng
# 维度:lat
#popup:点的名称

(2)多点标注:地图+标点+带icoon

#iconList 生成小框框
iconList = awesomeIconList(
  "home" = makeAwesomeIcon(icon = "home",markerColor = "skyblue"),
  "weixin" = makeAwesomeIcon(icon = "cutlery",markerColor = "red"),
  "bank" = makeAwesomeIcon(icon = "plus-sign",markerColor = "orange"),
  "automobile" = makeAwesomeIcon(icon = "trash",markerColor = "purple"),
  "coffee" = makeAwesomeIcon(icon = "book")
)
#每个小框框的坐标,经纬度
geo = data.frame(lon = rep(121.44, 5),
                 lat = rep(31.22, 5),
                 city = rep("Shanghai", 5))

#坐标进行微调,使得有差异
geo$lon = geo$lon+rnorm(5,0,0.003)
geo$lat = geo$lat+rnorm(5,0,0.003)

#小框框的属性
geo$type = c("home",
             "weixin",
             "bank" ,
             "automobile", 
             "coffee" )

#在高德地图上进行绘制
leaflet(geo) %>% amap() %>%
  addMiniMap() %>% 
  addAwesomeMarkers(icon = ~iconList[type])


图像icon下载网站:
(http://ionicons.com/)
(http://fontawesome.io/icons/)

(3)地图+分区域显示+色彩

if(require(leaflet)){
  region=regionNames("安徽")
  dat = data.frame(region,runif(length(region)))

  map = leafletGeo("安徽", dat)

  #涂色环节
  pal <- colorNumeric(
    palette = "Blues",
    domain = map$value)

  #载入高德地图amap
  leaflet(map) %>% amap() %>%
  #加入框边界及颜色
    addPolygons(stroke = TRUE,
                smoothFactor = 1,
                fillOpacity = 0.7,
                weight = 1,
                color = ~pal(value),
                popup = ~htmltools::htmlEscape(popup)
    ) %>%
    #加入右下角边框
    addLegend("bottomright", pal = pal, values = ~value,
              title = "legendTitle",
              labFormat = leaflet::labelFormat(prefix = ""),
              opacity = 1)

map 阶段,是导入数据,当然这里你也可以导入省级、县级。
addPolygons为加入边界;addLegend加入右下角的程度显示框。

最后
除此之外,还有更牛的leaflet包,其学习可参照下列网址,本文不做详述。
leaflet官网:http://leafletjs.com/index.html
详细R文档:http://rstudio.github.io/leaflet/markers.html
github地址:https://github.com/Leaflet/Leaflet.markercluster

本文仅限作者平时笔记使用,且对原作者原文稍有修改,涉及版权问题请原作者见谅。

转载自:https://blog.csdn.net/zhangkx91/article/details/77805588

You may also like...

退出移动版