绘图技巧

R画上海完整地铁线路图

2020-12-16  本文已影响0人  生信交流平台

前面我们尝试用REmap画了上海市一号线地铁线路图,效果不是很好

上海地铁线路图​

后面我们又用真实的上海8号线和12号线的数据画了图,这次效果还不错

R画上海地铁8号线和12号线线路图​

那么你可能会好奇,有没有方法可以画上海市所有的地铁线路图,或者说有没有办法想画那条线就画那条线(哪里不会点哪里)。

其实你会发现画所有线路图在技术上已经没有太大问题了,毕竟我们前面已经把整个流程走通了,无非就是再多几条线了。最大的问题在于如何找到其他地铁线路图所有站点的经纬度。文末会通过百度云盘共享给大家。下面是得到的上海完整地铁线路图。

我把画图的命令写成了函数,可以实现想画那条线就画那条,不指定参数就画全部。

library(REmap)
shanghai=c(121.480237,31.236305)
subway=function(lines="all"){
station=read.csv("stations.csv",stringsAsFactors=F)
#data=station[,c("baidu_lat","baidu_lon","station")]
if(length(lines)==1){
  if(lines=="all"){
 lines=unique(station$line)
 }
}
dire=c()
data=c()
for(sta in lines){
 #sta=10
 line=station[station$line==sta,]
 line=line[order(line$line_id),]
 stop =line[,1]
​
#获取地铁站数量
len <- length(stop)
#为合并数据做准备
if(sta==4){
origin = stop[1:len]
destination = stop[c(2:len,1)]
dire=rbind(dire,cbind(origin,destination))
}else if(sta==10){
 origin = stop[1:(len-4)]
 destination = stop[2:(len-3)]
 dire=rbind(dire,cbind(origin,destination))
 origin = stop[c(24,29:30)]
 destination = stop[29:31]
 dire=rbind(dire,cbind(origin,destination))
}else if(sta==11){
  origin = stop[1:(len-8)]
 destination = stop[2:(len-7)]
 dire=rbind(dire,cbind(origin,destination))
 origin = stop[c(28,32:37)]
 destination = stop[32:38]
 dire=rbind(dire,cbind(origin,destination))
}else{
origin = stop[1:len-1]
destination = stop[2:len]
dire=rbind(dire,cbind(origin,destination))
}
data=rbind(data,line[,c("baidu_lon","baidu_lat","station")])
}
dire=as.data.frame(dire)
data=as.data.frame(data)
Encoding(data$station)="UTF-8"
origin=as.character(dire$origin)
Encoding(origin)="UTF-8"
destination=as.character(dire$destination)
Encoding(destination)="UTF-8"
dire=data.frame(origin,destination,stringsAsFactors=F)
​
remapB(center = shanghai,
zoom = 12,
title = "Remap: 上海地铁",
color = "Blue",
markLineData = dire,
markLineTheme = markLineControl(smoothness = 0,
effect = T,
symbolSize = c(0,0)),
geoData = data)
}
#画全部
subway()
#画1号线,8号线和12号线
subway(c(1,8,12))

下面是上海1号线,8号线和12号线线路图

这里有几点需要注意的地方

1)stations.csv要跟这个脚本在同一个目录下面

2)上海四号线是环线,10号线和11号线有分支,所以在处理站点的时候要注意

3)subway函数不指定任何参数就画全部地铁线路图,指定多条线路图的时候,用数组就可以了。比如:画1号线,8号线和12号线用subway(c(1,8,12))

上海市地铁线路坐标文件请阅读下面这篇文章

R画上海完整地铁线路图

上一篇下一篇

猜你喜欢

热点阅读