R(四): R開發實例-map分布圖


      前幾章對R語言的運行原理、基本語法、數據類型、環境部署等基礎知識作了簡單介紹,本節將結合具體案例進行驗證測試。 案例場景:從互聯網下載全國三甲醫院數據,以地圖作為背景,展現各醫院在地圖上的分布圖。全國三甲醫院數據來源 http://www.wxmp.cn/cms/detail-51610-23480-1.html

 目錄:

  • map包研究
  • 效果圖
  • 數據清洗
  • R開發
  • R腳本部署

map包研究:


  • CRAN上地圖的一個常用包就是maps (https://cran.r-project.org/web/packages/maps/index.html), 它里邊一個基本的函數是map( ).
  • 示例: map('world', fill = TRUE,col=colors( )) , 你在Rstudio執行語句得出的結果可看出這個包用的數據集夠老了,蘇聯還在,畫中國地圖,還需要用到 mapdata 包(http://cran.r-project.org/web/packages/mapdata/index.html) 這是maps的補充包,給了更高清晰度或更大的數據集。里邊提供了中國和日本地圖。 但是這個地圖數據更老,沒有重慶市,直接 Pass。
  • ggmap包是基於ggplot2的圖層語法構建的R包,ggplot2作為R的圖形系統,其最鮮明的特點在於其分層語法,即把圖像元素一層層疊加在基礎畫布之上。事實上,一張主題地圖就相當把地圖作為畫布,而把基於地理的信息表現在地圖上。如果作為基礎畫布的地圖和ggplot2的分層語法結合起來,那么就能更高效繪制主題地圖。這個結合的工作就是由ggmap包來實現的
  • ggmap 它結合了來自Google Maps,OpenStreet Map,Stamen Maps和CloudMade Maps的靜態地圖信息來繪制主題地圖,但遺憾的是國內無法訪問google map, 只能 pass
  • REmap 是一個基於Echarts2.0 的一個R包.主要的目的是為廣大數據玩家提供一個簡便的,可交互的地圖數據可視化工具,在此基礎上,稍作修改,基本可用,但存在一個問題就是 Echart2.0 在移動終端上無法自適應

效果圖:


數據清洗:


  • 完成本節驗證,需要用到 github 上的兩個軟件包, baidumap (清洗數據時根據醫院名稱獲取經緯度) REmap (可視化展現),安裝命令如下
  • R命令: install.packages("devtools")
  • R命令: library(devtools)
  • R命令: install_github('badbye/baidumap')
  • R命令: install_github('lchiffon/REmap')
  • 從網上抓取的數據整理后保存至一個txt文件 (數據來源 http://www.wxmp.cn/cms/detail-51610-23480-1.html
  • 加載baidumap包,獲取醫院的經緯度,再對數據進行清洗,將結果保存至本地的文件
  • window Rstudio環境,代碼如下(糾正一下,write.table 里面的參數增加 sep="\t":
    #讀取數,刪除空值
    pdata<-read.csv("d:\\data.txt",sep="\t",header=FALSE,col.names = c("city","name"))
    pdata <- na.omit(pdata)
    
    #加載baidumap 根據名稱獲取醫院經緯度
    library(baidumap)
    bhs <- getCoordinate(pdata$name,formatted = T)
    
    #組織清洗后的數據
    hsdata <- data.frame(name=rownames(bhs),lon=bhs[,1],lat=bhs[,2])
    result <- merge(pdata,hsdata,by.x="name",by.y="name")
    head(result)
    
    #刪除空值數據
    result <- na.omit(result)
    
    #保存清空后的數據
    path <- c("d:\\data1.txt")
    write.table(result,file = path,row.names = FALSE)
    View Code
  • 清理前后的數據對比圖如下:

  •  

R包開發:


  • REmap的問題,在show()的時候,直接起了一個R  Session, 調用瀏覽器show出了最終的結果,如若最終的產品要集成在我們自己的Web框架內或者一個網頁內嵌的IFrame里,這樣就玩不轉,因此在REmap的基礎上稍作修改,將生成的結果保存在 linux httpd服務對應的站目錄,文件生成后,返回client 文件名,由client發起二次請求加載網頁。
  • 示列代碼如下(發布到linux系統),保存命名為 CMap.R:
    #FastRWeb調用R腳本函數入口
    run <- function(...){
      
      #物理文件保存地址
      path = c("/var/www/html")
      file_name = paste0("C", format(Sys.time(), "%Y%m%d"))
      full_path = paste0(path, "/", file_name, ".html")
      if (file.exists(full_path)) {
        out(file_name)
        return
      }
      
      my.writeMap(full_path)
      out(file_name)
    }
    
    
    my.writeMap <- function(file_name){
      
      #獲取清洗完成的數據
      pdata<-read.csv("/var/www/html/data1.txt",sep="\t",header=FALSE,col.names = c("name","cityName","lon","lat"))
      head(pdata)
      
      #按城市統計醫院數量(分布圖用到的參數)
      cityCount <- tapply(pdata$name,pdata$cityName,length)
      citydata <- data.frame(place=row.names(cityCount),values=cityCount)
    
      #geodata
      stadata <- data.frame(lon=pdata$lon,lat=pdata$lat,cityname = pdata$name)
      
      #分布圖
      library(REmap)
      output <- remapC(citydata, 
                       title = "Demo",
                       theme = get_theme("Bright"),
                       markPointData =stadata[,3],
                       markPointTheme = markPointControl(symbol = "pin",effect = TRUE,symbolSize = 3,color = "red"),
                       geoData = stadata)  
      
      my.plot(output,file_name)
    }
    
    #寫物理文件,修改REmap 代碼
    my.plot <- function(object, file_name) {
      
      ## SVG rewrite JS path
      if (object@maptype == 'SVG') {
        content <- sub("http://echarts.baidu.com/build/dist/echarts.js","./js/echarts.js", object@content)
        content <- sub("http://echarts.baidu.com/build/dist/echarts-all.js","./js/echarts-all.js",content)
      }
      
      if (object@maptype == 'SVGH') {
        content <- sub("http://echarts.baidu.com/build/dist/echarts.js","./js/echarts.js", object@content)
        content <- sub("http://echarts.baidu.com/build/dist/echarts-all.js","./js/echarts-all.js",content)
      }
      
      ## Bmap rewrite JS path
      if (object@maptype == "Bmap") {
        content <- sub("http://echarts.baidu.com/build/dist/echarts.js","./js/echarts.js",object@content)
        content <- sub("http://echarts.baidu.com/doc/asset/js/jquery.min.js","./js/jquery.min.js",content)
        content <- sub("http://lchiffon.github.io/reveal_slidify/echarts/require", "./js",content)
      }
      writeLines(content,file_name,useBytes = T)
    }
    View Code
  •  說明: 本例代碼是發布到FastRWeb框架上通過程序調用以Web方式訪問的示例,如果僅是學習研究用的話,直接將 my.writeMap 函數里面的代碼 copy 出來,將最后一句保存物理文件的代碼改為 show(output)或 output,在你本地Window的Rstudio 命令行下執行即可看到效果, 沒那么麻煩。

R腳本部署:


  • 將清洗后的數據文件(data.txt)和R腳本文件(CMap.R) copy至linux主機的 /var/FastRWeb/web.R 目錄下
  • 通過程序(開發語言不限)訪問 http://192.168.0.10:8888/CMap,得到名為 CYYYYMMDD.html 的文件名
  • 通過程序再次發起請求:http://192.168.0.10/CYYYYMMDD.html 即可得到上所示效果圖

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM