這裡提供兩種方法,方法一是通過 hchinamap 包,這個包的使用比較簡單,但是自定義程度比較低,另外一個是使用 highcharter 包,代碼雖然複雜了點,但是自定義程度很高。
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 = "\\((.*)\\)") -> text
load("https://mdniceczx.oss-cn-beijing.aliyuncs.com/chinadf.rda", verbose = TRUE)
china <- chinadf %>%
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 = "數據來源:隨機數據 | 繪製:<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) -> 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 = "數據來源:隨機數據 | 繪製:<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/{文件名}
其中中國和各個省的文件名分別為:
大家就可以試試自己感興趣的省份啦!