利用R语言绘制世界航班路线图

作者简介Introduction

一、 简介

本文基于NASA的夜间地图(https://www.nasa.gov/specials/blackmarble/2016/globalmaps/BlackMarble_2016_01deg.jpg)的基础上进行世界航班路线可视化,参考多篇博客以及可视化案例。

1. 包加载

本博客使用的包较多,利用pacman包里的p_load()函数进行加载

library(pacman)

p_load(tidyverse, data.table, geosphere, grid, jpeg, plyr)

2. 数据准备

使用的数据来自于OpenFlights.org(https://openflights.org/data.html)。

3.数据下载

download.file("https://raw.githubusercontent.com/jpatokal/openflights/master/data/airlines.dat",destfile = "airlines.dat", mode = "wb")

download.file("https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat",destfile = "airports.dat", mode = "wb")

download.file("https://raw.githubusercontent.com/jpatokal/openflights/master/data/routes.dat",destfile = "routes.dat", mode = "wb")

4.数据导入

airlines

airports

routes

5. 数据整理

#添加列名

colnames(airlines)

colnames(airports)

colnames(routes)

#类型转换

routes$airline_id

# airlines与routes数据融合

flights

# flights与airports数据融合

airports_orig

colnames(airports_orig)

airports_dest

colnames(airports_dest)

flights

flights

#剔除缺失值

flights

#最后数据如下

head(flights[,c(1:5)])

6. 下面就是准备地理信息数据

本文主要是可视化地理信息上的点与点之间的连接,这可以通过geosphere包里的函数gcIntermediate()很轻松实现。具体使用方法可以参考这里(http://flowingdata.com/2011/05/11/how-to-map-connections-with-great-circles/)。

# 按航空公司拆分数据集

flights_split

# Calculate intermediate points between each two locations

flights_all

# 转换为数据框

flights_fortified

# Unsplit lists

flights_fortified

# Add and clean column with airline names

flights_fortified$name

flights_fortified$name

# Extract first and last observations for plotting source and destination points (i.e., airports)

flights_points %

group_by(group) %>%

filter(row_number() == 1 | row_number() == n())

二、 可视化

接下来就是进行可视化了,前面讲了我们只是在NASA提供的夜间地球图上面进行数据映射,所以第一我们需要获取该背景地图。

1. 图片获取并渲染

#下载图片

download.file("https://www.nasa.gov/specials/blackmarble/2016/globalmaps/BlackMarble_2016_01deg.jpg",destfile = "BlackMarble_2016_01deg.jpg", mode = "wb")

#加载并渲染图片

earth

earth

2. 数据映射

由于航空公司十分多,就挑选几个有名的航空公司进行可视化。

(1) Lufthansa(德国汉莎航空公司)

ggplot() +

annotation_custom(earth, xmin = -180, xmax = 180, ymin = -90, ymax = 90) +

geom_path(aes(long, lat, group = id, color = name), alpha = 0.0, size = 0.0, data = flights_fortified) +

geom_path(aes(long, lat, group = id, color = name), alpha = 0.2, size = 0.3, color = "#f9ba00", data = flights_fortified[flights_fortified$name == "Lufthansa", ]) +

geom_point(data = flights_points[flights_points$name == "Lufthansa", ], aes(long, lat), alpha = 0.8, size = 0.1, colour = "white") +

theme(panel.background = element_rect(fill = "#05050f", colour = "#05050f"),panel.grid.major = element_blank(),panel.grid.minor = element_blank(),axis.title = element_blank(),axis.text = element_blank(),axis.ticks.length = unit(0, "cm"),legend.position = "none") +annotate("text", x = -150, y = -18, hjust = 0, size = 14,label = paste("Lufthansa"), color = "#f9ba00", family = "Helvetica Black") +

annotate("text", x = -150, y = -26, hjust = 0, size = 8,

label = paste("Flight routes"), color = "white") +

annotate("text", x = -150, y = -30, hjust = 0, size = 7,

coord_equal()

(2) Emirates(阿联酋航空公司)

ggplot() +

annotation_custom(earth, xmin = -180, xmax = 180, ymin = -90, ymax = 90) +

geom_path(aes(long, lat, group = id, color = name), alpha = 0.0, size = 0.0, data = flights_fortified) +

geom_path(aes(long, lat, group = id, color = name), alpha = 0.2, size = 0.3, color = "#ff0000", data = flights_fortified[flights_fortified$name == "Emirates", ]) +

geom_point(data = flights_points[flights_points$name == "Emirates", ], aes(long, lat), alpha = 0.8, size = 0.1, colour = "white") +

theme(panel.background = element_rect(fill = "#05050f", colour = "#05050f"),

axis.title = element_blank(),

axis.text = element_blank(),

legend.position = "none") +

annotate("text", x = -150, y = -18, hjust = 0, size = 14,

label = paste("Emirates"), color = "#ff0000", family = "Fontin") +

annotate("text", x = -150, y = -26, hjust = 0, size = 8,

label = paste("Flight routes"), color = "white") +

annotate("text", x = -150, y = -30, hjust = 0, size = 7,

coord_equal()

(3) British Airways(英国航空公司)

ggplot() +

annotation_custom(earth, xmin = -180, xmax = 180, ymin = -90, ymax = 90) +

geom_path(aes(long, lat, group = id, color = name), alpha = 0.0, size = 0.0, data = flights_fortified) +

geom_path(aes(long, lat, group = id, color = name), alpha = 0.2, size = 0.3, color = "#075aaa", data = flights_fortified[flights_fortified$name == "British Airways", ]) +

geom_point(data = flights_points[flights_points$name == "British Airways", ], aes(long, lat), alpha = 0.8, size = 0.1, colour = "white") +

theme(panel.background = element_rect(fill = "#05050f", colour = "#05050f"),

axis.title = element_blank(),

axis.text = element_blank(),

legend.position = "none") +

annotate("text", x = -150, y = -18, hjust = 0, size = 14,

label = paste("BRITISH AIRWAYS"), color = "#075aaa", family = "Baker Signet Std") +

annotate("text", x = -150, y = -26, hjust = 0, size = 8,

label = paste("Flight routes"), color = "white") +

annotate("text", x = -150, y = -30, hjust = 0, size = 7,

coord_equal()

(4) Air China(中国国航)

ggplot() +

annotation_custom(earth, xmin = -180, xmax = 180, ymin = -90, ymax = 90) +

geom_path(aes(long, lat, group = id, color = name), alpha = 0.0, size = 0.0, data = flights_fortified) +

geom_path(aes(long, lat, group = id, color = name), alpha = 0.2, size = 0.3, color = "#F70C15", data = flights_fortified[flights_fortified$name == "Air China", ]) +

geom_point(data = flights_points[flights_points$name == "Air China", ], aes(long, lat), alpha = 0.8, size = 0.1, colour = "white") +

theme(panel.background = element_rect(fill = "#05050f", colour = "#05050f"),

axis.title = element_blank(),

axis.text = element_blank(),

legend.position = "none") +

annotate("text", x = -150, y = -18, hjust = 0, size = 14,

label = paste("Air China"), color = "#F70C15", family = "Times New Roman") +

annotate("text", x = -150, y = -26, hjust = 0, size = 8,

label = paste("Flight routes"), color = "white") +

annotate("text", x = -150, y = -30, hjust = 0, size = 7,

coord_equal()

(5) China Southern Airlines(中国南航)

ggplot() +

annotation_custom(earth, xmin = -180, xmax = 180, ymin = -90, ymax = 90) +

geom_path(aes(long, lat, group = id, color = name), alpha = 0.0, size = 0.0, data = flights_fortified) +

geom_path(aes(long, lat, group = id, color = name), alpha = 0.2, size = 0.3, color = "#004D9D", data = flights_fortified[flights_fortified$name == "China Southern Airlines", ]) +

geom_point(data = flights_points[flights_points$name == "China Southern Airlines", ], aes(long, lat), alpha = 0.8, size = 0.1, colour = "white") +

theme(panel.background = element_rect(fill = "#05050f", colour = "#05050f"),

axis.title = element_blank(),

axis.text = element_blank(),

legend.position = "none") +

annotate("text", x = -150, y = -18, hjust = 0, size = 14,

label = paste("China Southern Airlines"), color = "#004D9D", family = "Times New Roman") +

annotate("text", x = -150, y = -26, hjust = 0, size = 8,

label = paste("Flight routes"), color = "white") +

annotate("text", x = -150, y = -30, hjust = 0, size = 7,

coord_equal()

(6) 一次性映射多家航空公司航行路线

#抽取数据集

flights_subset

flights_subset

flights_subset_points %

group_by(group)%>%

filter(row_number()==1|row_number()==n())

#可视化

ggplot() +

annotation_custom(earth, xmin = -180, xmax = 180, ymin = -90, ymax = 90) +

geom_path(aes(long, lat, group = id, color = name), alpha = 0.2, size = 0.3, data = flights_subset) +

geom_point(data = flights_subset_points, aes(long, lat), alpha = 0.8, size = 0.1, colour = "white") +

scale_color_manual(values = c("#f9ba00", "#ff0000", "#075aaa")) +

theme(panel.background = element_rect(fill = "#05050f", colour = "#05050f"),

axis.title = element_blank(),

axis.text = element_blank(),

legend.position = "none") +

annotate("text", x = -150, y = -4, hjust = 0, size = 14,

label = paste("Lufthansa"), color = "#f9ba00", family = "Helvetica Black") +

annotate("text", x = -150, y = -11, hjust = 0, size = 14,

label = paste("Emirates"), color = "#ff0000", family = "Fontin") +

annotate("text", x = -150, y = -18, hjust = 0, size = 14,

label = paste("BRITISH AIRWAYS"), color = "#075aaa", family = "Baker Signet Std") +

annotate("text", x = -150, y = -30, hjust = 0, size = 8,

label = paste("Flight routes"), color = "white") +

annotate("text", x = -150, y = -34, hjust = 0, size = 7,

coord_equal()

三、 SessionInfo

sessionInfo()

R version 3.4.3 (2017-11-30)

Platform: x86_64-w64-mingw32/x64 (64-bit)

Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:

[1] LC_COLLATE=Chinese (Simplified)_China.936 LC_CTYPE=Chinese (Simplified)_China.936

[3] LC_MONETARY=Chinese (Simplified)_China.936 LC_NUMERIC=C

[5] LC_TIME=Chinese (Simplified)_China.936

attached base packages:

[1] grid stats graphics grDevices utils datasets methods base

other attached packages:

[1] plyr_1.8.4 jpeg_0.1-8 geosphere_1.5-7 data.table_1.10.4-3

[5] forcats_0.2.0 stringr_1.2.0 dplyr_0.7.4 purrr_0.2.4

[9] readr_1.1.1 tidyr_0.8.0 tibble_1.4.2 ggplot2_2.2.1.9000

[13] tidyverse_1.2.1 pacman_0.4.6

loaded via a namespace (and not attached):

[1] Rcpp_0.12.15 cellranger_1.1.0 pillar_1.1.0 compiler_3.4.3 bindr_0.1

[6] tools_3.4.3 lubridate_1.7.1 jsonlite_1.5 nlme_3.1-131 gtable_0.2.0

[11] lattice_0.20-35 pkgconfig_2.0.1 rlang_0.1.6 psych_1.7.8 cli_1.0.0

[16] rstudioapi_0.7 yaml_2.1.16 parallel_3.4.3 haven_1.1.1 bindrcpp_0.2

[21] xml2_1.2.0 httr_1.3.1 knitr_1.19 hms_0.4.1 glue_1.2.0

[26] R6_2.2.2 readxl_1.0.0 foreign_0.8-69 sp_1.2-7 modelr_0.1.1

[31] reshape2_1.4.3 magrittr_1.5 scales_0.5.0.9000 rvest_0.3.2assertthat_0.2.0

[36] mnormt_1.5-5 colorspace_1.3-2 stringi_1.1.6 lazyeval_0.2.1 munsell_0.4.3

[41] broom_0.4.3 crayon_1.3.4

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180306G0HLHP00?refer=cp_1026
  • 腾讯「云+社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 yunjia_community@tencent.com 删除。

扫码关注云+社区

领取腾讯云代金券