REmap入门示例

REmap是一个基于Echarts2.0 http://echarts.baidu.com 的一个R包。主要的目的是为广大数据玩家提供一个简便的,可交互的地图数据可视化工具。目前托管在github,https://github.com/lchiffon/REmap

使用如下步骤安装:

library(devtools)
install_github('lchiffon/REmap')

REmap目前更新到V0.3,提供百度迁徙,分级统计,百度地图,热力图等功能的实现。

提示:请使用Chrome或者Firefox来作为默认浏览器

最后要声明的一点:这个包的目的是简化使用和学习的流程,如果你是一个好学的geek,请深入的学习Echarts!

特性

  1. 使用Echarts2.0封包,地图绘制使用的是SVG图形
  2. 采用百度API来自动获取城市的经纬度数据
  3. 支持Windows!

使用向导

获取经纬度

获取经纬度的函数是基于BaiduAPI的一个获取地理位置的功能。这个函数不仅是REmap下的一个功能,实际上,你也可以用它来抓取城市经纬度数据:

基本函数:

  • get_city_coord 获取一个城市的经纬度
  • get_geo_position 获取一个城市向量的经纬度
library(REmap)
city_vec = c("北京","Shanghai","广州")
get_city_coord("Shanghai")
[1] 121.47865  31.21562
get_geo_position (city_vec)
        lon      lat     city
1  116.6212 40.06107     北京
2  121.4786 31.21562 Shanghai
3  113.3094 23.39237     广州

注:windows用户会看到city一列为utf-8编码,可以使用get_geo_position (city_vec2)$city查看列向量的信息。(我能说我最好的建议是换Mac么?)

绘制迁徙地图

绘制地图使用的是主函数remap

remap(mapdata, title = "", subtitle = "", 
      theme =get_theme("Dark"))
  • mapdata 一个数据框对象,第一列为出发地点,第二列为到达地点
  • title 标题
  • subtitle 副标题
  • theme 控制生成地图的颜色,具体将会在get_theme部分说明
set.seed(125)
origin = rep("北京",10)
destination = c('上海','广州','大连','南宁','南昌',
                '拉萨','长春','包头','重庆','常州')
dat = data.frame(origin,destination)
out = remap(dat,title = "REmap实例数据",subtitle = "theme:Dark")
plot(out)

该地图会写成一个html文件,保存在电脑里面,并通过浏览器打开该文件。以下的方式都可以看到这个地图:

## Method 1
remap(dat,title = "REmap实例数据",subtitle = "theme:Dark")

## Method 2 
out = remap(dat,title = "REmap实例数据",subtitle = "theme:Dark")
out

## Method 3
plot(out)

个性化地图

正如之前所说的,为了简化学习和使用的流程,REmap并没有封装太多的参数。(真的不是我懒)如果想更个性化地调整Echarts的参数,请移步Echarts的官方文档http://echarts.baidu.com/doc/doc.html

REmap中get_theme提供了迁徙地图中常用颜色的调整:

get_theme(theme = "Dark", lineColor = "Random",
  backgroundColor = "#1b1b1b", titleColor = "#fff",
  borderColor = "rgba(100,149,237,1)", regionColor = "#1b1b1b")
  • theme 默认主题,除了三个内置主题,可以使用“none”来自定义颜色
    • a character object in (“Dark”,“Bright,”Sky“,”none“)
  • lineColor 线条颜色,默认随机,也可以使用固定颜色
    • Control the color of the line, “Random” for random color
  • backgroundColor 背景颜色
    • Control the color of the background
  • titleColor 标题颜色
    • Control the color of the title
  • borderColor 边界颜色(省与省之间的信息)
    • Control the color of the border
  • regionColor 区域颜色
    • Control the color of the region

颜色可以使用颜色名(比如’red’,’skyblue’等),RGB(“#1b1b1b”,“#fff”)或者一个rgba的形式(“rgba(100,100,100,1)”),可以在这里找到颜色对照表。

  • 默认模板:Bright
## default theme:"Bright"
set.seed(125)
out = remap(dat,title = "REmap实例数据",subtitle = "theme:Bright",
            theme = get_theme("Bright"))
plot(out)

  • 更改线条颜色
## set Line color as 'orange'
set.seed(125)
out = remap(dat,title = "REmap实例数据",subtitle = "theme:Bright",
            theme = get_theme("None",
                             lineColor = "orange"))
plot(out)

  • 更改其他颜色
## Set Region Color
out = remap(dat,title = "REmap实例数据",subtitle = "theme:Bright",
            theme = get_theme("None",
                              lineColor = "orange",
                              backgroundColor = "#FFC1C1",
                              titleColor = "#1b1b1b",
                              regionColor = '#ADD8E6'))
plot(out)

参考资料

REmap入门示例》有23个想法

  1. 请问在用remap函数的时候,出现警告:Warning message:
    In get_geo_position(city_vec) : 北京 not found.广州 not found.

    1. 我也出现这种情况了,估计是编码问题,我试着把中文转成拼音就可以了,有3个无法识别改为 nanning –> nanzningz;nanchang –> nachang 常州是怎么也识别不了,看你有什么别的方法,我试过UTF-8,GBk等格式都不行

  2. > install_github(‘lchiffon/REmap’)
    Downloading GitHub repo [email protected]
    from URL https://api.github.com/repos/lchiffon/REmap/zipball/master
    Error in curl::curl_fetch_memory(url, handle = handle) :
    Peer certificate cannot be authenticated with given CA certificates

      1. 我好像在运行了这段代码以后R无法连接internet,无法下载包了。。。请问怎么设置回去?

  3. 非常棒的工作!我最近在使用baidumap的时候遇到一个问题:分别用ggmap和baidumap获取同一位置(如中国矿业大学北京),然后将他们获取的位置标在leaflet地图上,发现差别好大呀!Why?是baidumap不准确吗?
    下面是我的代码

    library(leaflet)
    library(baidumap)
    ## getcoordinate return vector
    loc<-getCoordinate('中国矿业大学(北京)', formatted = T) # character
    library(ggmap)
    ## geocode return dataframe
    ggloc<-geocode("China University of Mining and Technology,Beijing")
    ## plot
    CUMTB %
    addTiles() %>% # Add default OpenStreetMap map tiles
    addMarkers(lng=loc[1], lat=loc[2],
    popup=”China University of Mining and Technology,Beijing”)
    CUMTB # Print
    ggCUMTB %
    addTiles() %>% # Add default OpenStreetMap map tiles
    addMarkers(lng=ggloc$lon, lat=ggloc$lat,
    popup=”China University of Mining and Technology,Beijing”)
    ggCUMTB # Print

      1. 谢谢,明白了!逛了一圈,百度不够open啊!需要ak,就得“卖身”!!

  4. > library(devtools)
    > install_github(‘lchiffon/REmap’)
    Downloading GitHub repo [email protected]
    from URL https://api.github.com/repos/lchiffon/REmap/zipball/master
    Installing REmap
    “D:/R-3.2.5/bin/x64/R” –no-site-file –no-environ –no-save –no-restore –quiet CMD INSTALL \
    “C:/Users/Administrator/AppData/Local/Temp/RtmpSwi1xZ/devtools13086ee688e/Lchiffon-REmap-a05489f” \
    –library=”D:/R-3.2.5/library” –install-tests

    * installing *source* package ‘REmap’ …
    ** R
    ** data
    *** moving datasets to lazyload DB
    ** demo
    ** inst
    ** preparing package for lazy loading
    Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) :
    there is no package called 'Rcpp'
    ERROR: lazy loading failed for package 'REmap'
    * removing 'D:/R-3.2.5/library/REmap'
    错误: Command failed (1)

    安装不了啊急

  5. install_github(‘lchiffon/REmap’)
    Downloading GitHub repo [email protected]
    from URL https://api.github.com/repos/lchiffon/REmap/zipball/master
    Installing REmap
    “f:/Program Files/R/R-3.2.2/bin/x64/R” –no-site-file –no-environ –no-save –no-restore –quiet CMD \
    INSTALL “C:/Users/liweitang/AppData/Local/Temp/RtmpigW5L3/devtools1ee4393a167a/Lchiffon-REmap-a05489f” \
    –library=”F:/Program Files/R/R-3.2.2/my_library” –install-tests

    ‘f:\Program’ 不是内部或外部命令,也不是可运行的程序
    或批处理文件。
    Error: Command failed (1)

  6. Downloading GitHub repo [email protected]
    from URL https://api.github.com/repos/lchiffon/REmap/zipball/master
    Installing REmap
    Installing 1 package: XML
    trying URL ‘https://cran.rstudio.com/bin/windows/contrib/3.3/XML_3.98-1.4.zip’
    Content type ‘application/zip’ length 4290726 bytes (4.1 MB)
    downloaded 144 KB

    Error in read.dcf(file.path(pkgname, “DESCRIPTION”), c(“Package”, “Type”)) :
    cannot open the connection
    In addition: Warning messages:
    1: In download.file(url, destfile, method, mode = “wb”, …) :
    downloaded length 147456 != reported length 4290726
    2: In unzip(zipname, exdir = dest) : error 1 in extracting from zip file
    3: In read.dcf(file.path(pkgname, “DESCRIPTION”), c(“Package”, “Type”)) :
    cannot open compressed file ‘XML/DESCRIPTION’, probable reason ‘No such file or directory’

  7. Error in curl::curl_fetch_disk(url, x$path, handle = handle) :
    Timeout was reached

  8. “`{r}
    library(REmap)
    set.seed(125)
    origin = rep(“北京”,10)
    destination = c(‘上海’,’广州’,’大连’,’南宁’,’南昌’,
    ‘拉萨’,’长春’,’包头’,’重庆’,’常州’)
    dat = data.frame(origin,destination)
    out = remap(dat,title = “REmap实例数据”,subtitle = “theme:Dark”)
    plot(out)
    “`

    老师,这段代码直接运行可以做出图,但是再rmarkdown文档里knit HTML时却会出现
    library(REmap)
    set.seed(125)
    origin = rep(“北京”,10)
    destination = c(‘上海’,’广州’,’大连’,’南宁’,’南昌’,
    ‘拉萨’,’长春’,’包头’,’重庆’,’常州’)
    dat = data.frame(origin,destination)
    out = remap(dat,title = “REmap实例数据”,subtitle = “theme:Dark”)
    ## Warning in get_geo_position(city_vec): 北京 not found.上海 not found.广州
    ## not found.南宁 not found.南昌 not found.拉萨 not found.长春 not found.包头
    ## not found.重庆 not found.常州 not found.
    plot(out)
    ## Save img as: C:\Users\YAOCHA~1\AppData\Local\Temp\RtmpEDZIei/ID_20160819145217_243.html

  9. > install_github(“lchiffon/REmap”)
    Downloading GitHub repo [email protected]
    from URL https://api.github.com/repos/lchiffon/REmap/zipball/master
    Installing REmap
    “B:/R Programming/bin/x64/R” –no-site-file –no-environ –no-save \
    –no-restore –quiet CMD INSTALL “C:/Users/Zoe \
    Cheng/AppData/Local/Temp/RtmpIpLzT0/devtools50606a5a6aa1/Lchiffon-REmap-faa8a59″ \
    –library=”B:/R Programming/library” –install-tests

    ‘B:\R’ is not recognized as an internal or external command,
    operable program or batch file.
    Error: Command failed (1)

    如何解决?

  10. 明明安装了,怎么加载不了呢?求助~
    >install.packages(“C:/Documents and Settings/Administrator/桌面/REmap-master.zip”, repos = NULL, type = “win.binary”)
    > library(`REmap-master`)
    Error in library(`REmap-master`) :
    there is no package called ‘REmap-master’

  11. 很感兴趣,我想问一下,你的这个网站是实时更新么? 还是隔几天或者几小时更新一下

发表评论

电子邮件地址不会被公开。 必填项已用*标注