发布时间:2025-12-09 11:46:59 浏览次数:1
背景:我在 CRAN 上提交了几个 R 包,都是很简单的绘图包,后来觉得没什么意思便不再维护了。长期以来收到了不少使用者的反馈(issues),都是英语的,也懒得看。。。昨天给他们都回复了下(用中文回复的哈哈哈,让他们自己翻译吧哈哈)
该包绘制的地图精度较低,如果你需要学习绘制高精度的中国地图,欢迎加入我的线上培训班获取:欢迎加入 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 可以完成该包提供的所有的功能的!
虽然代码多了点,但是自定义的程度很高!
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/{文件名}
其中中国和各个省的文件名分别为:
大家就可以试试自己感兴趣的省份啦!