leafletCN:基于leaflet的中国扩展包

leafletCN:基于leaflet的中国扩展包

本文是学习Lchiffon/leafletCN十行代码看到空气质量指数的笔记。

根据leafletCN的作者郎大为介绍

leafletCN是一个基于leaflet的中国扩展包,里面保存了一些适用于中国的区域划分数据以及一些有帮助的函数,地理区划数据来源于github的geojson-map-china项目,数据细分到县级市。

安装

R
1
2
3
4
# 稳定版
install.packages("leafletCN")
# 开发版
devtools::install_github("lchiffon/leafletCN")

建议安装稳定版,开发版似乎有点问题。

使用

regionNames: 返回某个地图区域名

如果不传入对象, 会自动返回 300 多个支持的名字列表, 包括各个城市, 省, 以及三个特殊的名字:

  • world:世界地图
  • china:中国分省份地图
  • city:中国分城市地图

不传参数:

R
1
2
3
4
5
6
7
8
9
> regionNames()
[1] "新疆维吾尔自治区" "西藏自治区" "内蒙古自治区"
······
[361] "伊犁" "博尔塔拉" "乌鲁木齐"
[364] "克拉玛依" "city" "china"
[367] "world"

These are valid mapName~
[1] "NULL"

我家乡的区县名称:

R
1
2
> regionNames("阜阳")
[1] "阜南县" "界首市" "临泉县" "太和县" "颍东区" "颍泉区" "颍上县" "颍州区"

世界各国的名字:

R
1
2
3
4
5
6
> regionNames("world")
[1] "Afghanistan" "Angola" "Albania"
[4] "UnitedArabEmirates" "Argentina" "Armenia"
·····
[172] "Vietnam" "Vanuatu" "Yemen"
[175] "SouthAfrica" "Zambia" "Zimbabwe"

中国各省级行政区的名字:

R
1
2
3
4
5
6
7
8
9
10
11
12
13
> regionNames("china")
[1] "新疆维吾尔自治区" "西藏自治区" "内蒙古自治区"
[4] "青海省" "四川省" "黑龙江省"
[7] "甘肃省" "云南省" "广西壮族自治区"
[10] "湖南省" "陕西省" "广东省"
[13] "吉林省" "河北省" "湖北省"
[16] "贵州省" "山东省" "江西省"
[19] "河南省" "辽宁省" "山西省"
[22] "安徽省" "福建省" "浙江省"
[25] "江苏省" "重庆市" "宁夏回族自治区"
[28] "海南省" "台湾省" "北京市"
[31] "天津市" "上海市" "香港特别行政区"
[34] "澳门特别行政区"

中国各城市的名字:

R
1
> city <- regionNames("city") %>% as.data.frame()

demomap():传入城市名/省名显示这个城市的示例地图

R
1
demomap("阜阳")

geojsonmap()

geojsonmap()函数将一个数据框显示在要展示的地图上,由于区域名称只识别前两个字,所以不用担心要不要“省”的问题了。

例如绘制我的好友的省份分布:

R
1
2
3
4
5
6
7
8
9
10
11
12
13
df <- read.csv("myfriends.csv", stringsAsFactors = F)['Province']
df <- summarise(group_by(df, Province), count = length(Province))
df$Province <- df$Province %>% gsub(pattern = "[a-z, A-Z]", replacement = "")
df <- subset(df, df$Province != "")
df <- subset(df, df$Province != "'")
df$count <- as.numeric(df$count)
df <- data.frame(df)
geojsonMap(df, "china",
namevar = ~ Province,
valuevar = ~ count,
palette = "YlOrRd",
legendTitle = "好友数量",
popup = paste0("好友数量:", df$count, "人"))

微信好友的省份分布

再例如我的好友的市县分布:

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 示例:好友的市级分布
df <- read.csv("myfriends.csv", stringsAsFactors = F)['City']
df <- summarise(group_by(df, City), count = length(City))
df$City <- df$City %>% gsub(pattern = "[a-z, A-Z]", replacement = "")
df <- subset(df, df$City != "")
df <- subset(df, df$City != "'")
df$count <- as.numeric(df$count)
df <- data.frame(df)
geojsonMap(df, "city",
namevar = ~ City,
valuevar = ~ count,
palette = "YlOrRd",
legendTitle = "好友数量",
popup = paste0(df$City, "好友数量:", df$count, "人"))

微信好友的市县分布

amap():在leaflet上叠加一个高德地图

R
1
2
3
4
5
6
library(leaflet)
leaflet() %>%
amap() %>%
addMarkers(lng = 113.41451,
lat = 23.017382,
popup = "暨南大学")

比较惹人烦的就是这个地图像素渣渣啊!

暨南大学的位置.html

read.geoShape()

这个函数可以将一个geojson格式的数据读取为一个SpatialPolygonsDataFrame对象,方便sp或者leaflet包调用。

R
1
2
3
4
filePath <- system.file("geojson/china.json", package = "leafletCN")
library(sp)
map <- read.geoShape(filePath)
plot(map)

leafletGeo()

这个函数可以把一个数据框和一个地图组合起来,方便leaflet调用,其中名字的变量为name,数值的变量为value。

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
df <- read.csv("myfriends.csv", stringsAsFactors = F)['Province']
df <- summarise(group_by(df, Province), count = length(Province))
df$Province <- df$Province %>% gsub(pattern = "[a-z, A-Z]", replacement = "")
df <- subset(df, df$Province != "")
df <- subset(df, df$Province != "'")
df$count <- as.numeric(df$count)
df <- data.frame(df)
map <- leafletGeo("china", df,
namevar = ~Province,
valuevar = ~count)
pal <- colorNumeric(
pal = "Blues",
domain = map$value
)

(p <- leaflet(map) %>%
addTiles() %>%
addPolygons(stroke = T,
smoothFactor = 1,
fillOpacity = 0.7,
weight = 1,
color = ~pal(value),
popup = paste0(map$name, "好友数量:", map$value, "人")) %>%
addLegend("bottomright",
pal = pal,
values = ~value,
title = "好友数量",
labFormat = leaflet::labelFormat(prefix = ""),
opacity = 1))

微信好友的省份分布

由于leaflet创建的是一个htmlwidgets,所以可以保存成一个html:

R
1
2
# 保存为html文件
p %>% htmlwidgets::saveWidget("我的好友分布.html")

我的好友分布.html

AQI数据爬取与可视化

最后作为示例,作者爬取了空气质量排行榜(根据AQI排序)网站并将数据展示在地图上,这个网站是这样的:

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 爬取网站:http://www.pm25.in/rank
library(XML)
library(leafletCN)
df <- readHTMLTable("http://www.pm25.in/rank",
encoding = "UTF-8",
stringAsFactor = F)[[1]]
# 只要AQI的数据
df <- df[, 2:3]
names(df) <- c("city", "aqi")
df$aqi <- as.numeric(df$aqi)

# 绘制地图
geojsonMap(df, "city",
popup = paste0(df$city, ": ", df$aqi),
palette = "Reds", legendTitle = "AQI")

geojson与echarts4r

我在前面写给一篇博客:使用ggplot2和echarts4r绘制我的微信好友分布图中讲述了如何使用geojson数据绘制地图。这里再复习一下。
这次我们使用郎大为提到的这个仓库longwosion/geojson-map-china里面的数据绘制地图。

首先画一幅中国地图:

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
library(echarts4r)
library(echarts4r.maps)
library(jsonlite)
chinajson <- read_json("https://raw.githubusercontent.com/longwosion/geojson-map-china/master/china.json")

# 需要先把省份的名字修改一下
df$Province <- paste0(df$Province, "省")
df$Province <- df$Province %>% gsub(pattern = "澳门省", replacement = "澳门特别行政区")
df$Province <- df$Province %>%
gsub(pattern = "香港省", replacement = "香港特别行政区")
df$Province <- df$Province %>%
gsub(pattern = "北京省", replacement = "北京市")
df$Province <- df$Province %>%
gsub(pattern = "重庆省", replacement = "重庆市")
df$Province <- df$Province %>%
gsub(pattern = "内蒙古省", replacement = "内蒙古自治区")
df$Province <- df$Province %>%
gsub(pattern = "上海省", replacement = "上海市")

df %>%
e_charts(Province) %>%
e_map_register("China", chinajson) %>%
e_map(count, map = "China") %>%
e_visual_map(count,
color = c(rev(RColorBrewer::brewer.pal(3, "Greens"))),
left = '90%',
top = '50%') %>%
e_title("我的微信好友分布",
textStyle = list("fontSize" = 30, "fontFamily" = "STSong"),
textAlign = "middle", left = "50%")

# R

评论

程振兴

程振兴 @czxa.top
截止今天,我已经在本博客上写了658.8k个字了!

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×