基于R的飞机航线数据可视化(行政区划)
加载库
## 加载库
library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
## 加载库
library(tidyr)
library(RColorBrewer)
library(maps)
library(geosphere)
library(igraph)
##
## Attaching package: 'igraph'
## The following object is masked from 'package:tidyr':
##
## crossing
## The following objects are masked from 'package:dplyr':
##
## as_data_frame, groups, union
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
## The following object is masked from 'package:base':
##
## union
## 加载库
library(networkD3)
library(stringr)
读取数据
## 读取数据,数据融合
routes <- read.csv("routes.csv",header=TRUE)
airports <- read.csv("airports.csv",header=TRUE)
可视化机场位置
map(database = "world",col="skyblue", fill=TRUE, bg="black", lwd=0.1)
# 添加起点的位置
points(x=airports$Longitude, y=airports$Latitude, pch=19, cex=0.2,col="orange")
可视化每个国家有多少机场
### 可视化每个国家有多少机场
plotdata <- airports%>%group_by(Country)%>%
summarise(number = n(),
LatitudeMean = mean(Latitude),
LongitudeMean = mean(Longitude))
## 在地图上可视化每个国家的机场数量
map(database = "world",col="skyblue", fill=TRUE, bg="black", lwd=0.1)
# 添加起点的位置
points(x=plotdata$LongitudeMean, y=plotdata$LatitudeMean,
pch=19, cex=log10(plotdata$number),col="orange")
可视化航线
## 数据组合
plotdata <- routes[c("source.airport","destination.apirport")]
## 剔除缺失值
plotdata <- plotdata[!is.na(plotdata[,1])&!is.na(plotdata[,2]),]
plotdata2 <- airports[c("IATA","Latitude","Longitude")]
plotdata3 <- merge(plotdata,plotdata2,by.x = "source.airport", by.y = "IATA")
plotdata4 <- merge(plotdata3,plotdata2,by.x = "destination.apirport", by.y = "IATA")
## 可视化航线
map(database = "world",col="skyblue", fill=TRUE, bg="black", lwd=0.1)
# 添加起点的位置
points(x=airports$Longitude, y=airports$Latitude, pch=19, cex=0.2,col="orange red")
col.1 <- adjustcolor("orange", alpha=0.4)
for(i in 1:nrow(plotdata4)) {
node1 <- plotdata4[i,c("Latitude.x","Longitude.x")]
node2 <- plotdata4[i,c("Latitude.y","Longitude.y")]
arc <- gcIntermediate( c(node1$Longitude.x, node1$Latitude.x),
c(node2$Longitude.y, node2$Latitude.y),
n=100, addStartEnd=TRUE )
lines(arc, col=col.1, lwd=0.1)
}
更多信息参考 https://rpubs.com/DaiTu/414172