Highcharter绘制中国地图

2

刚发现segmantfault不支持iframe,绘图效果可以在这"Highcharter绘制中国地图"查看。

Highmaps数据

Highmaps data上可以获取Highmaps的全部数据列表,其中关于中国的数据有三条。中国(只包括中国大陆)、中国(中国大陆、香港、澳门)、中国(中国大陆、香港、澳门、台湾)。如果统计中国数据,我们当然是只用第三条数据。

Highmaps数据分为json和js,都可以回来绘制Highmaps。

json数据

Chinajson_file <- "data/Highmaps-data/cn-all-sar-taiwan.geo.json"
Chinajson <- jsonlite::fromJSON(Chinajson_file, simplifyVector = FALSE)
Chinajson <- geojsonio::as.json(Chinajson)

js数据

Chinajs_file <- "data/Highmaps-data/cn-all-sar-taiwan.js"
Chinajs <- readLines(Chinajs_file, warn = FALSE, encoding = "UTF-8")
Chinajs[1] <- gsub(".* = ", "", Chinajs[1])
Chinajs <- paste(Chinajs, collapse = "\n")
Chinajs <- jsonlite::fromJSON(Chinajs, simplifyVector = FALSE)

演示数据

n <- 20
z <-  sample(1:n)
sequences <- purrr::map2(1:n, z, function(x, y){ ifelse(x == 1:n, y, 0) })
df <- data.frame(
  lat = runif(n, 20, 45),
  lon = runif(n, 80, 120),
  z = z,
  color = colorize(z),
  sequence = sequences
)

hp_df <- read.csv("data/Highmaps-data/map_example.csv", stringsAsFactors = FALSE)
names(hp_df) <- c("hc-key", "value")
knitr::kable(head(hp_df))

地图

点图

highchart(type = "map") %>%
  hc_add_series(mapData = Chinajson, showInLegend = FALSE) %>%
  hc_add_series(
    data = df, type = "mappoint",
    dataLabels = list(enabled = FALSE),
    name = "演示数据") 

<iframe src="https://jeevanyue.github.io/p...; style="border: none; width: 100%; height: 600px;"> </iframe>

气泡图

highchart(type = "map") %>%
  hc_add_series(mapData = Chinajson, showInLegend = FALSE) %>%
  hc_add_series(
    data = df, type = "mapbubble",
    minSize = 0, maxSize = 30,
    dataLabels = list(enabled = FALSE),
    name = "演示数据") 

<iframe src="https://jeevanyue.github.io/p...; style="border: none; width: 100%; height: 600px;"> </iframe>

地区分布

通过js数据绘制

## js地图数据需要跟展示的数据一起通过hc_add_series导入
highchart(type = "map") %>%
  hc_add_series(
    mapData = Chinajs, showInLegend = TRUE, name = "演示数据",
    data = hp_df, joinBy = "hc-key") %>% 
  hc_colorAxis(auxpar = NULL)

通过json数据绘制

highchart(type = "map") %>%
  hc_add_series(
    mapData = Chinajson, showInLegend = FALSE, name = "演示数据",
    data = hp_df, joinBy = "hc-key") %>% 
  hc_colorAxis(auxpar = NULL)

<iframe src="https://jeevanyue.github.io/p...; style="border: none; width: 100%; height: 600px;"> </iframe>

地图汉化

name_cn <- read.csv("data/Highmaps-data/name_cn.csv",stringsAsFactors = FALSE)

Chinajs_chinese_file <- "data/Highmaps-data/cn-all-sar-taiwan_chinese.js"
Chinajs_chinese <- readLines(Chinajs_chinese_file, warn = FALSE, encoding = "UTF-8")
Chinajs_chinese[1] <- gsub(".* = ", "", Chinajs_chinese[1])
Chinajs_chinese <- paste(Chinajs_chinese, collapse = "\n")
Chinajs_chinese <- jsonlite::fromJSON(Chinajs_chinese, simplifyVector = FALSE)

highchart(type = "map") %>%
  hc_add_series(
    mapData = Chinajs_chinese, showInLegend = TRUE, name = "演示数据",
    data = hp_df, joinBy = "hc-key") %>% 
  hc_colorAxis(auxpar = NULL)

<iframe src="https://jeevanyue.github.io/p...; style="border: none; width: 100%; height: 600px;"> </iframe>

参考

你可能感兴趣的

载入中...