# 中国国内航线信息的可视化

 This post was kindly contributed by 数据科学与R语言 - go there to comment and to read the full post.

• 从这个网站下载到机场数据和航线数据；
• 从中挑选出中国的机场和国内航线，并加以整理；
• 用ggmap包读取谷歌地图；
• 将机场和航线信息绘制在地图上。

`library(ggmap)data.port <- read.csv('d:\\airports.dat',F)data.line <- read.csv('d:\\routes.dat',F)library(stringr)# 找到中国的机场portinchina <- str_detect(data.port[,'V4'], "China")chinaport <- data.port[portinchina,]# 去除少数几个没有编号的机场chinaport <-chinaport[chinaport\$V5!='',                      c('V3','V5','V7','V8','V9')]names(chinaport) <- c('city','code','lan','lon','att') # 找出国内航班lineinchina <- (data.line[,'V3'] %in% chinaport\$code) & (data.line[,'V5'] %in% chinaport\$code)chinaline <- data.line[lineinchina,c('V3','V5','V9')]names(chinaline) <- c('source','destination','equipment') #构建一个函数，根据机场编码得到经纬度findposition <- function(code) {    find <- chinaport\$code==code    x <- chinaport[find,'lon']    y <- chinaport[find,'lan']    return(data.frame(x,y))} # 将机场编码转为经纬度from <- lapply(as.character(chinaline\$source),findposition)from <- do.call('rbind',from)from\$group <- 1:dim(from)[1]names(from) <- c('lon','lan','group') to <- lapply(as.character(chinaline\$destination),findposition)to <- do.call('rbind',to)to\$group <-1:dim(to)[1]names(to) <-c('lon','lan','group')data.line <- rbind(from,to)temp<- data.line[data.line\$group<100,]# 用ggmap包从google读取地图数据，并将之前的数据标注在地图上。ggmap(get_googlemap(center = 'china', zoom=4,                    maptype='roadmap'),extent='device')+    geom_point(data=chinaport,aes(x=lon,y=lan),               colour = 'red4',alpha=0.8)+    geom_line(data=data.line,aes(x=lon,y=lan,group=group),              size=0.1,alpha=0.05,color='red4')`