R语言第六次实训,leaflet绘制地铁线路图

2 篇文章 0 订阅

题目:

数据请到我的主页看我的上传资源

  1. 使用leaflet绘制地铁线路图,要求
    1) 图中绘制地铁线路
    2) 各站点展示进站流量(08:00:00-08:05:00间的数据),流量的大小用标记的大小表示,
    并且提示线路、站点、流量的具体数值。
    Shenyang <- leaflet() %>%
    setView(lng=123.44,lat=41.81,zoom = 11) %>% addProviderTiles(“CartoDB.Positron”)
    2.使用plotly绘制(17:00:00-17:05:00) 出站流量最多的五个站点的出站流量(柱状图)

解决:

第一步,读文件获取数据,编写辅助函数绘图

library(dplyr)
library(leaflet)
library(data.table)
setwd("E://table")
stations<-read.csv("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 

结果

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

##(2)各站点展示进站流量(08:00:00-08:05:00间的数据),流量的大小用标记的大小表示,并且提示线路、站点、流量的具体数值。
 
 
library(lubridate)
library(data.table)
library(dplyr)
library(sqldf)
library(leaflet)
 
stations<-read.csv("E://table//systation.csv");
stadata<-read.csv("E://table//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)出站流量最多的五个站点的出站流量

##使用plotly绘制(17:00:00-17:05:00)出站流量最多的五个站点的出站流量。
 
library(lubridate)
library(sqldf)
library(plotly)
 
stations<-read.csv("E://table//systation.csv");
stadata<-read.csv("E://table//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)

结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值