#REmap包简介
#包安装
#remapH函数
#remapH案例
#get_theme函数
#remapC函数
#markLineControl函数
#markPointControl函数
#remapC案例
- REmap包简介
REmap包是R与echart的对接,在R里调用echar的api直接作图,函数特征更加简单,绘图过程更加方便快捷,如果要个性化绘制可以修改REmap包源码, 需要有一定的javascript/css/html
remapH函数:该函数主要用于绘制热力效果图,如在地图上绘制不同密度的热力效果
remapC函数:该函数主要用于绘制分块区域热力,如在地图上不同省份热力效果
remapB函数:该函数主要基于百度地图背景,做迁徙,通勤效果图
markLineControl,markPointControl,get_theme这三个函数主要用于控制在map图上画线, 打点以及主题的各种样式,如颜色,效果,粗细等。
- 包安装
a、https://cran.r-project.org/ 上下载对应系统及版本的R软件,下文环境为win64的R
b、此次用到的包为REmap,其挂靠在https://github.com/ 上,只需运行R,依次输入:
(1)Install.packages(“devtools”)
此时会谈出一个界面,选择china即可,下载完成后,再输入:
(2)Library(devtools)
(3)Install_github(“Lchiffon/REmap”),稍等片刻下载完成后,再输入:
(4)library(REmap)
配置完成
注意:1.如果安装devtools包过程提示没有digest包error,则再输入install.packages(“digest”),再重复上述3过程
- remapH函数
remapH(data, maptype = 'china', theme = get_theme("Dark"), blurSize = 30, color = c('blue'), minAlpha = 0.05, opacity = 1, )
data:要传入的数据,数据为三列,第一列为经度lon,第二列为维度lat,第三列为密度/概率prob
maptype: 要绘制的地址类型,如中国地图maptype=‘china’,世界地图maptype=‘world’
theme: 主题,设置该参数后无需设置get_theme里其他参数,下文介绍
blurSize: 为热力效果的泛化范围,可调整热力点中心的扩散程度
color: 热力的渐变颜色
minAlpha:热力点的展示阈值,对应data中的prob列,作图时各点密度会对比minAlpha,以凸显不同密度所展示的不同热力分布
opacity:透明度,调整热力图的透明度
remapH案例
> library(REmap)> options(remap.js.web=T)> data<- read.csv(file="C:\\Users\\Xu\\Desktop\\cityvaluetop10.csv",header=T)> theme1 <- get_theme(theme = "none",+ lineColor = "white",+ backgroundColor = "#CCCCCC", + titleColor = "#fff",+ borderColor = "white", + regionColor = "Silver",+ labelShow = T, + pointShow = F, + pointColor = "gold"+ ) > remapH(data, #效果如下图+ maptype = 'china',+ theme = theme1,+ blurSize = 70,+ color = "red",+ minAlpha = 10,+ opacity = 1+ )Save img as: ID_20171104005314_62482.html
- get_theme函数
get_theme(theme = "Dark", lineColor = "Random", backgroundColor = "#1b1b1b", titleColor = "#fff", borderColor = "rgba(100,149,237,1)", regionColor = "#1b1b1b", labelShow = T, pointShow = F, pointColor = "gold" )
theme: 主题,设置该参数后无需设置get_theme里其他参数,可选“Dark”,“Sky”,“blue”,“none”四中,当设置get_theme其他参数时,需要将该参数设置为“none”
lineColoe:线条颜色,对应为map图里面迁徙线条的颜色
backgroundColor:图片的背景色,支持16进制颜色输入,也支持rgb()函数和rgba()
titleColor:标题的颜色,设置同上
borderColor:地图中各省、市边界颜色
regionColor:地图中各区域颜色,各省份和市
labelShow:否展示各省、市名字,设置为True时展示
pointShow:是否展示各省会,城市所在点,设置为True时展示
pointColor:设置上述点的颜色
- remapC函数
该函数主要用于绘制分块区域热力,如在地图上显示不同省份热力效果
remapC(data, maptape = 'china' markLineData = NULL, markPointData = NULL, color = c('#le90ff','#f0ffff'), them = get_theme('Bright'), title = "", subtitle = "", markLineTheme = markLineControl(), markPointTheme = markPointControl(), geoData = NA, mindata = NA, maxdata = NA))
data: 表示要传入的数据,该数据有2列,第一列为city(各城市,省份名字),第二列为value(第一列城市对对应的数值)
maptype:要绘制的地址类型,如中国地图maptype=‘china’,世界地图maptype=‘world’
markLineData:要绘制的线条数据,该数据前两列为必须,第一列为origin(出发地),第二列为destination(目的地),此外建议在第三列指定线条颜色,因为在markLineControl里设置线条颜色容易出错
markPointData:要绘制的点的数据,该数据为一列,该列为要绘制点的名字,此外建议在第二列指定点的颜色,因为在markLineControl里设置点颜色容易出错
color:热力颜色的渐变,第一个为起始颜色,第二个为终止颜色
theme:主题,设置该参数后无需设置get_theme里其他参数,可选“Dark”,“Sky”,“blue”,“none”四中,当设置get_theme其他参数时,需要将该参数设置为“none”
title:标题
subtitle:副标题
markLineTheme:控制线条的样式参数,该参数由markLineControl函数传入(后文介绍)
markPointTheme:控制点的样式参数,该参数由markPointControl函数传入(后文介绍)
geoData:为上述点,线涉及到的城市,省份的经纬度,该数据为三列,第一列lon(经度),第二列lat(维度),第三列city(城市或省份名字)
mindata,maxdata:热力图的阈值,低于或高于该值将不会被展示出来
markLineControl函数
markLineControl(symbol = c('none', 'arrow'), symbolSize = c(2,4), smooth = T, smoothness = 0.2, effect = T, lineWidth = 1, lineType = 'solid', color = "Random" )
symbol:控制要绘制线条两端形状,第一个为起点,第二个为终点,可选样式有:’pin’, ‘circle’,’rectangle’,’triangle’,’diamond’,’emptyCircle’,’emptyRectangle’,’ emptyTriangle’,’emptyDiamond’,’heart’,’droplet’,’arrow’,’tar’
symbleSize:控制上述参数形状大小,同样有两个值
smooth: 控制线条是否平滑,为T时,smoothness参数才有效
smoothness:控制线条的平滑度,为0时是直线
effect:控制线条是否呈现动态效果,为T时显示动态效果,需要注意的是当线条过多时effect参数无效,不会显示动效
linewidth:控制线条的粗细
lineType:控制线条的形状,可选样式有:’solid’, ‘dotted’,’dashed’
color:控制线条的颜色,不建议在此处设置颜色
markPointControl函数
markPointControl(symbol = 'emptyCircle', symbolSize = "Random", effect = T, effectType = 'scale', color = "Random" )
symbol:控制点的形状,可选样式有:’pin‘,’circle’,’rectangle’,’triangle’,’diamond’,’emptyCircle’, ’emptyRectangle’,’emptyTriangle’,’emptyDiamond’,’heart’,’droplet’,’arrow’,’tar’
symbolSize:控制上述参数形状大小
effect:控制点是否呈现动态效果,为T时显示动态效果,需要注意的是当点数目过多时effect参数无效,不会显示动效
effectType:控制点的动效呈现形式,可选样式有:’scale’ ,’bounce’
color:控制点的颜色,不建议在此处设置颜色
remapC案例
> library(REmap)> options(remap.js.web=T)> data<- read.csv(file="C:\\Users\\Xu\\Desktop\\cityvaluetop10.csv",header=T)> theme1 <- get_theme(theme = "none",+ lineColor = "white",+ backgroundColor = "#CCCCCC", + titleColor = "#fff",+ borderColor = "white", + regionColor = "Silver",+ labelShow = T, + pointShow = F, + pointColor = "gold"+ )> remapH(data,+ maptype = 'china',+ theme = theme1,+ blurSize = 70,+ color = "red",+ minAlpha = 10,+ opacity = 1+ )Save img as: ID_20171104005427_69796.html> library(REmap)> options(remap.js.web=T)> cityvalue <- read.csv(file="C:\\Users\\Xu\\Desktop\\cityvalue.csv",header=T,encoding="gb2312")> theme1 <- get_theme(theme = "none", + lineColor = "red",+ backgroundColor = "white", + titleColor = "#fff",+ borderColor = "white", + regionColor = "grey",+ labelShow = F, + pointShow = F, + pointColor = "gold"+ )> markline_data <- data.frame(origin=rep("广州",10),+ destination=c("广西","湖南","湖北","贵州",+ "河南","江西","福建","北京",+ "浙江","四川"),+ color=rep("red",10)+ )> markpoint_data <- data.frame(point=c("广西","湖南","湖北","贵州","河南",+ "江西","福建","北京","浙江","四川"),color=rep("white",10))> markline_control <- markLineControl(symbolSize=c(0,0),+ smooth=F,+ smoothness=0,+ effect=F,+ lineWidth=1,+ lineType="solid",+ )> markpoint_control <- markPointControl(symbol = 'Circle',+ symbolSize = 10,+ effect = T,+ effectType = 'scale',+ )> remapC(cityvalue,+ maptype = 'china',+ markLineData = markline_data,+ markPointData = markpoint_data,+ color = c('#888888','#CDCDCD'),+ theme = theme1,+ title = "",+ subtitle = "",+ markLineTheme = markline_control,+ markPointTheme = markpoint_control,+ #geoData = NA, geoData参数,此处可以省略该参数的原因是因为作图涉及到城市以及省份数据,remapC函数都会调用内置get_geo_position函数获取对应经纬度,而这只有在数据是城市或省份时才有效+ #mindata = NA,+ #maxdata = NA+ )Save img as: ID_20171104010458_132853.htmlThere were 11 warnings (use warnings() to see them)
知识整理来自
[1]R语言中文社区
[2]知乎