R语言绘制沈阳地铁线路图

##使用leaflet绘制地铁线路图,要求

##(1)图中绘制地铁线路

library(dplyr)
library(leaflet)
library(data.table)
stations<-read.csv("C:\\Users\\BIGDATA\\Desktop\\文件\\BigData\\R语言\\相关作业文档\\3\\第五次实训课数据\\systation.csv");

stations <- arrange(stations,line,line_id)

lines_color <- data.frame("line"=c(1:13,16),"color"=c("#00008B","#000000","#000000","#000000","#823094","#CF047A","#F3560F","#008CC1","#91C5DB","#C7AFD3","#8C2222","#007a61","#ec91cc","#32D2CA"))

pal <- colorFactor(as.character(lines_color$color), domain = stations$line)


Shenyang <- leaflet() %>%
setView(lng=123.44,lat=41.81,zoom = 11) %>% addProviderTiles("CartoDB.Positron")


## 辅助函数绘制线路

draw_line_add <- function(l_no,line_s_id=NULL){
  line_color <- lines_color[lines_color$line==l_no,]$color
  line_data <- stations[stations$line==l_no,]
  if(is.null(line_s_id)){
  draw_lines <- Shenyang %>%
    addPolylines(lat=line_data$gps_lat,lng=line_data$gps_lon,color=line_color)
  }else{
    draw_lines <- Shenyang %>%
      addPolylines(lat=line_data$gps_lat[line_s_id],lng=line_data$gps_lon[line_s_id],color=line_color)
  }
  return(draw_lines)
}

for(l in unique(stations$line)){
  line_length <- nrow(stations[stations$line==l,])
  Shenyang<- draw_line_add(l_no=l)
}

stations_no <- nrow(stations)
for (i in 1:stations_no) {
  s <- stations$station[i]
  stations$lines[i] <- paste(stations[stations$station==s,]$line,sep="",collapse="/")
}

#添加地铁站名
Shenyang<- Shenyang%>%
  addCircleMarkers(stations$gps_lon, stations$gps_lat, popup =paste(stations$station,stations$lines,sep=","),color = pal(stations$line), radius=1.5) %>%
  addLegend(pal=pal,values = stations$line)
Shenyang 



  

##(2)各站点展示进站流量(08:00:00-08:05:00间的数据),流量的大小用标记的大小表示,并且提示线路、站点、流量的具体数值。


library(lubridate)
library(data.table)
library(dplyr)
library(sqldf)
library(leaflet)

stations<-read.csv("C:\\Users\\BIGDATA\\Desktop\\文件\\BigData\\R语言\\相关作业文档\\3\\第五次实训课数据\\systation.csv");
stadata<-read.csv("C:\\Users\\BIGDATA\\Desktop\\文件\\BigData\\R语言\\相关作业文档\\3\\第五次实训课数据\\SY-20150401.csv");
stadata$se<-period_to_seconds(hms(stadata$V3))
stadata<-as.data.table(stadata)
stadata<-filter(stadata,V6==0 & se>=28800 & se<=29100)


getR <- function(quakes) {
  sapply(quakes$count, function(count) {
    count/40
 })
}


stad<-substring(stadata['V4'][,],4)
stad<-data.frame(stad);
names(stad)[1]<-'station'

stations<-stations[order(stations$station),]

count<-sqldf("select station, count(*) from stad group by station")
s<-merge(count,stations,by="station")

names(s)[2]<-c('count')



Shenyang %>% addCircleMarkers(s$gps_lon, s$gps_lat, popup =paste(s$station,s$line,sep=","),color = pal(s$line), radius=getR(s),label=as.character(s$count)) %>% addTiles()

  

 

##使用plotly绘制(17:00:00-17:05:00)出站流量最多的五个站点的出站流量。

library(lubridate)
library(sqldf)
library(plotly)

stations<-read.csv("C:\\Users\\BIGDATA\\Desktop\\文件\\BigData\\R语言\\相关作业文档\\3\\第五次实训课数据\\systation.csv");
stadata<-read.csv("C:\\Users\\BIGDATA\\Desktop\\文件\\BigData\\R语言\\相关作业文档\\3\\第五次实训课数据\\SY-20150401.csv");

stadata$se<-period_to_seconds(hms(stadata$V3))
stadata<-filter(stadata,V6!=0 & se>=61200 & se<=61500)

stad<-substring(stadata['V4'][,],4)
stad<-data.frame(stad);
names(stad)[1]<-'station'


stations<-stations[order(stations$station),]

count<-sqldf("select station, count(*) from stad group by station")
s<-merge(count,stations,by="station")
names(s)[2]<-c('count')
s[17,2]<-s[17,2]*2;
st<-s[-18,];
st<-st[order(-s$count),];
st<-st[1:5,1:2];
plot_ly(st, x=~station,y=~count)

  

转载自:https://blog.csdn.net/yanjiangdi/article/details/74784015

You may also like...