使用 hchinamap - highcharter 绘制交互式中国地图及中国各省地图

发布时间:2025-12-09 11:46:59 浏览次数:1

背景:我在 CRAN 上提交了几个 R 包,都是很简单的绘图包,后来觉得没什么意思便不再维护了。长期以来收到了不少使用者的反馈(issues),都是英语的,也懒得看。。。昨天给他们都回复了下(用中文回复的哈哈哈,让他们自己翻译吧哈哈)

hchinamap:快速绘制中国及各个省的地图

该包绘制的地图精度较低,如果你需要学习绘制高精度的中国地图,欢迎加入我的线上培训班获取:欢迎加入 RStata 线上培训班学习使用 R 语言和 Stata 进行数据处理和可视化

中国省级地图

中国市级地图

你可以从 CRAN 上安装这个包:https://cran.r-project.org/web/packages/hchinamap/

install.packages('hchinamap')

使用起来非常简单,首先加载我提供的示例数据:

dir <- tempdir()download.file('https://mdniceczx.oss-cn-beijing.aliyuncs.com/chinadf.rda', file.path(dir, 'chinadf.rda'))load(file.path(dir, 'chinadf.rda'), verbose = TRUE)chinadf#> # A tibble: 527 x 3#>    region name   value#>    <chr>  <chr>  <dbl>#>  1 China  北京      44#>  2 China  天津      28#>  3 China  河北       3#>  4 China  山西      65#>  5 China  内蒙古    18#>  6 China  辽宁      46#>  7 China  吉林      67#>  8 China  黑龙江    80#>  9 China  上海       8#> 10 China  江苏      50#> # … with 517 more rows

绘制中国地图:

library(hchinamap)china <- chinadf %>%   dplyr::filter(region == "China")hchinamap(name = china$name, value = china$value,          width = "100%", height = "400px",          title = "中国地图", region = "China")

还可以绘制各个省级行政单位的:

anhui <- chinadf %>%  dplyr::filter(region == "Anhui")hchinamap(name = anhui$name, value = anhui$value,          width = "100%", height = "500px",          title = "安徽地图", region = "Anhui")

另外你还可以在 Shiny Apps 里面使用:

dir <- system.file("examples", "hchinamap", package = "hchinamap")setwd(dir)shiny::shinyAppDir(".")

使用这个 App 你可以探索各个参数的功能。

关于该包的更多使用方法可以参考:https://cran.r-project.org/web/packages/hchinamap/vignettes/hchinamap.html (原谅我百度翻译的英语文档。。。)

我虽然在 hchinamap 函数里面设置了超过 20 个参数,但是依然不能满足所有人的需要,所以我不再建议大家使用 hchinamap 绘制中国及各个省的地图了。highcharter 可以完成该包提供的所有的功能的!

使用 highcharter 绘制同样的中国及各个省的地图

虽然代码多了点,但是自定义的程度很高!

library(highcharter)library(jsonlite)library(tidyverse)readLines("https://data.jianshukeji.com/jsonp?filename=geochina/china.json", warn = F) %>%   str_match(string = ., pattern = "\((.*)\)") -> textload("https://mdniceczx.oss-cn-beijing.aliyuncs.com/chinadf.rda", verbose = TRUE)china <- chinadf %>%  dplyr::filter(region == "China") %>%   select(-region)chinafromJSON(text[1, 2], simplifyVector = FALSE) -> cnhighchart(type = "map") %>%   hc_add_series_map(map = cn,                    df = china,                     joinBy = "name",                     value = "value",                     name = "随机数据:",                    borderWidth = 0.5,                    borderColor = "gray",                    states = list(hover = list(color = '#bada55')),                    dataLabels = list(enabled = FALSE),                    marginBottom = "200px") %>%   hc_title(text = "使用 highcharter 绘制中国地图") %>%   hc_subtitle(text = "数据来源:随机数据 | 绘制:<a src='https://tidyfriday.cn'>TidyFriday</a>",              useHTML = TRUE) %>%   hc_tooltip(headerFormat = "",             pointFormat = "<b>{point.name}</b><br>随机数据:{point.value}",             borderRadius = 5) %>%   hc_colorAxis(dataClasses = JS('      [{to: 1, color: "#ffffcc", name: "无"},       {from: 1, to: 20, color: "#d9f0a3"},       {from: 20, to: 40, color: "#addd8e"},       {from: 40, to: 60, color: "#78c679"},       {from: 60, to: 80, color: "#31a354"},       {from: 80, color: "#006837"}]')) %>%   hc_legend(align = 'left',            layout = 'vertical',            valueDecimals = 0,            floating = TRUE,            symbolRadius = 0,            x = 20, y = -20,            symbolHeight = 14,             backgroundColor = JS("(Highcharts.theme && Highcharts.theme.legendBackgroundColor) || 'rgba(255, 255, 255, 0.85)'"),            title = list(text = "随机数据")) %>%   hc_add_theme(hc_theme_google()) %>%   hc_exporting(enabled = TRUE) %>%   hc_credits(enabled = TRUE) %>%   hc_mapNavigation(enabled = TRUE)

绘制省份地图的方法类似,例如绘制广东省的:

chinadf %>%  dplyr::filter(region == "Guangdong") %>%   select(-region) -> gdgdreadLines("https://data.jianshukeji.com/jsonp?filename=geochina/guangdong.json", warn = F) %>%   str_match(string = ., pattern = "\((.*)\)") -> textfromJSON(text[1, 2], simplifyVector = FALSE) -> gdmaphighchart(type = "map") %>%   hc_add_series_map(map = gdmap,                    df = gd,                     joinBy = "name",                     value = "value",                     name = "随机数据:",                    borderWidth = 0.5,                    borderColor = "gray",                    states = list(hover = list(color = '#bada55')),                    dataLabels = list(enabled = FALSE),                    marginBottom = "200px") %>%   hc_title(text = "使用 highcharter 绘制广东地图") %>%   hc_subtitle(text = "数据来源:随机数据 | 绘制:<a src='https://tidyfriday.cn'>TidyFriday</a>",              useHTML = TRUE) %>%   hc_tooltip(headerFormat = "",             pointFormat = "<b>{point.name}</b><br>随机数据:{point.value}",             borderRadius = 5) %>%   hc_colorAxis(dataClasses = JS('      [{to: 1, color: "#ffffcc", name: "无"},       {from: 1, to: 20, color: "#d9f0a3"},       {from: 20, to: 40, color: "#addd8e"},       {from: 40, to: 60, color: "#78c679"},       {from: 60, to: 80, color: "#31a354"},       {from: 80, color: "#006837"}]')) %>%   hc_legend(align = 'right',            layout = 'vertical',            valueDecimals = 0,            floating = TRUE,            symbolRadius = 0,            x = -20, y = -20,            symbolHeight = 14,             backgroundColor = JS("(Highcharts.theme && Highcharts.theme.legendBackgroundColor) || 'rgba(255, 255, 255, 0.85)'"),            title = list(text = "随机数据")) %>%   hc_add_theme(hc_theme_google()) %>%   hc_exporting(enabled = TRUE) %>%   hc_credits(enabled = TRUE) %>%   hc_mapNavigation(enabled = TRUE)

其它省的方法几乎一模一样,唯一不同的就是这个地图数据的链接啦,格式是这样的:https://data.jianshukeji.com/jsonp?filename=geochina/{文件名}

其中中国和各个省的文件名分别为:

  • 中国:china.json
  • 安徽:anhui.json
  • 澳门:aomen.json
  • 北京:beijing.json
  • 福建:fujian.json
  • 甘肃:gansu.json
  • 广东:guangdong.json
  • 广西:guangxi.json
  • 贵州:guizhou.json
  • 海南:hainan.json
  • 河北:hebei.json
  • 河南:henan.json
  • 黑龙江:heilongjiang.json
  • 湖北:hubei.json
  • 湖南:hunan.json
  • 吉林:jilin.json
  • 江苏:jiangsu.json
  • 辽宁:liaoning.json
  • 内蒙古:neimenggu.json
  • 宁夏:ningxia.json
  • 青海:qinghai.json
  • 山东:shandong.json
  • 山西:shanxi.json
  • 陕西:shanxi2.json
  • 上海:shanghai.json
  • 四川:sichuan.json
  • 台湾:taiwan.json
  • 天津:tianjin.json
  • 西藏:xizang.json
  • 香港:xianggang.json
  • 新疆:xinjiang.json
  • 云南:yunnan.json
  • 浙江:zhejiang.json

大家就可以试试自己感兴趣的省份啦!

chinamap
需要做网站?需要网络推广?欢迎咨询客户经理 13272073477