leafletGeo函数,从data.frame创建sp对象
实例
# Thu Oct 22 15:39:11 2020 -
# 字符编码:UTF-8
# R 版本:R x64 4.0.3 for window 10
# cgh163email@163.com
# 个人笔记不负责任
# —— 拎了个梨🍐
.rs.restartR()
rm(list=ls());gc()
# leafletGeo函数,从data.frame创建sp对象
require(leaflet)
require(leafletCN)
dat = data.frame(regionNames("china"),
runif(34)) # 区划名和随机数
# regionNames..china.. runif.34.
# 1 新疆维吾尔自治区 0.8818702
# 2 西藏自治区 0.8256165
# 3 内蒙古自治区 0.8350657
# 4 青海省 0.2994787
#
map = leafletGeo("china", dat) # 构建sp对象
pal <- colorNumeric(
palette = "Blues",
domain = map$value) # 渐变调色板
# Thu Oct 22 15:46:17 2020 --作图
leaflet(map) %>% addTiles() %>%
addPolygons(
stroke = TRUE,
smoothFactor = 1,
fillOpacity = 0.7,
weight = 1,
color = ~ pal(value),
popup = ~ htmltools::htmlEscape(popup)
) %>%
addLegend(
"bottomright",
#图例四个方位 c("topright", "bottomright", "bottomleft","topleft")
pal = pal,
values = ~ value,
title = "图例标题🍐",
labFormat = leaflet::labelFormat(prefix = c('aaaa', '图例文字')),
opacity = 1
)
# Thu Oct 22 15:50:44 2020 --