绘图杂记20R语言绘制热力地图

Posted 卖山楂啦prss

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了绘图杂记20R语言绘制热力地图相关的知识,希望对你有一定的参考价值。

一、R语言——hchinamap包绘制地图

安装

install.packages('dplyr')
install.packages('magrittr')
install.packages('hchinamap')

调用

library(hchinamap)
library(magrittr)
library(dplyr)

绘制地图

# 加载数据
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
head(chinadf)
# 筛选数据
china <- chinadf %>% dplyr::filter(region == "China")
# 绘制地图
hchinamap(
          name = china$name, 
          value = china$value,
          width = "100%", 
          height = "500px",
          title = "China地图", 
          region = "China",
	      minColor = "#FFEFD5",
          maxColor = "#FF3030")


绘制地级市地图

# 取数据
Chongqing <- chinadf %>% dplyr::filter(region == "Chongqing")
hchinamap(
          name = Chongqing$name, 
          value = Chongqing$value,
          width = "100%", 
          height = "500px",
          title = "Map of Chongqing", 
          region = "Chongqing",
	    minColor = "#FFF5EE",
          maxColor = "#A8A8A8")


参考:
https://cran.r-project.org/web/packages/hchinamap/vignettes/hchinamap.html
https://mp.weixin.qq.com/s/QDImD7zP49bCliI5l7Lj3g

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

二、R语言——highcharter包绘制地图

引自:https://zhuanlan.zhihu.com/p/42430990

安装

install.packages('highcharter')
install.packages('jsonlite')
install.packages('tidyverse')

加载

library(highcharter)
library(jsonlite)
library(tidyverse)
library(magrittr)
# 加载数据
readLines("https://data.jianshukeji.com/jsonp?filename=geochina/chongqing.json", warn = F) %>% 
  str_match(string = ., pattern = "\\\\((.*)\\\\)") -> text
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
# 筛选数据
chongqing <- chinadf %>% dplyr::filter(region == "Chongqing")
chongqing

数据集

# A tibble: 38 x 3
   region    name     value
   <chr>     <chr>    <dbl>
 1 Chongqing 万州区      49
 2 Chongqing 涪陵区      61
 3 Chongqing 渝中区      89
 4 Chongqing 大渡口区    59
 5 Chongqing 江北区      58
 6 Chongqing 沙坪坝区    86
 7 Chongqing 九龙坡区    67
 8 Chongqing 南岸区      22
 9 Chongqing 北碚区      80
10 Chongqing 綦江区       8
# ... with 28 more rows
cq <- fromJSON(text[1, 2], simplifyVector = FALSE)
highchart(type = "map") %>% 
hc_add_series_map(map = cq ,
                    df = chongqing, 
                    joinBy = "name", 
                    value = "value",
                    borderWidth = 0.5,
                    borderColor = "gray",
                    states = list(hover = list(color = '#bada55')),
                    dataLabels = list(enabled = FALSE),
                    marginBottom = "200px")

不知道啥原因…没颜色,试了好久都没做出来,拉胯了

算了

有兴趣的参考:https://mp.weixin.qq.com/s/QDImD7zP49bCliI5l7Lj3g

以上是关于绘图杂记20R语言绘制热力地图的主要内容,如果未能解决你的问题,请参考以下文章

R语言绘图:在地图上绘制热力图

数据可视化应用绘制空间地图(附R语言代码)

[R语言] Heatmap绘图经验总结

数据可视化应用绘制“变形”地图(附R语言代码)

R语言绘图——条形图/柱状图

R语言绘图添加箭头标记绘制连接两点的线段