使用 hchinamap / highcharter 绘制交互式中国地图及中国各省地图 您所在的位置:网站首页 中国地图云南德宏 使用 hchinamap / highcharter 绘制交互式中国地图及中国各省地图

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

2023-03-22 18:07| 来源: 网络整理| 查看: 265

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

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

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

中国省级地图

中国市级地图

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

install.packages('hchinamap')

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

dir # A tibble: 527 x 3 #> region name value #> #> 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 % dplyr::filter(region == "China") hchinamap(name = china$name, value = china$value, width = "100%", height = "400px", title = "中国地图", region = "China")

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

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

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

dir % str_match(string = ., pattern = "\\((.*)\\)") -> text load("https://mdniceczx.oss-cn-beijing.aliyuncs.com/chinadf.rda", verbose = TRUE) china % dplyr::filter(region == "China") %>% select(-region) china fromJSON(text[1, 2], simplifyVector = FALSE) -> cn highchart(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 = "数据来源:随机数据 | 绘制:TidyFriday", useHTML = TRUE) %>% hc_tooltip(headerFormat = "", pointFormat = "{point.name}随机数据:{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) -> gd gd readLines("https://data.jianshukeji.com/jsonp?filename=geochina/guangdong.json", warn = F) %>% str_match(string = ., pattern = "\\((.*)\\)") -> text fromJSON(text[1, 2], simplifyVector = FALSE) -> gdmap highchart(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 = "数据来源:随机数据 | 绘制:TidyFriday", useHTML = TRUE) %>% hc_tooltip(headerFormat = "", pointFormat = "{point.name}随机数据:{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

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



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有