作者简介Introduction

taoyan:伪码农,R语言爱好者,爱开源。

个人博客: https://ytlogos.github.io/

公众号:生信大讲堂

往期回顾

R语言可视化学习笔记之相关矩阵可视化包ggcorrplot

R语言学习笔记之相关性矩阵分析及其可视化

ggplot2学习笔记系列之利用ggplot2绘制误差棒及显著性标记

ggplot2学习笔记系列之主题(theme)设置

用circlize包绘制circos-plot


一、 简介

本文基于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 <- fread("airlines.dat", sep = ",", skip = 1)

airports <- fread("airports.dat", sep = ",")

routes <- fread("routes.dat", sep = ",")

5. 数据整理

#添加列名

colnames(airlines) <- c("airline_id", "name", "alias", "iata", "icao", "callisign", "country", "active")

colnames(airports) <- c("airport_id", "name", "city", "country","iata", "icao", "latitude", "longitude","altitude", "timezone","dst","tz_database_time_zone","type", "source")

colnames(routes) <- c("airline", "airline_id", "source_airport", "source_airport_id","destination_airport","destination_airport_id","codeshare", "stops","equipment")

#类型转换

routes$airline_id <- as.numeric(routes$airline_id)

# airlines与routes数据融合

flights <- left_join(routes, airlines, by="airline_id")

# flights与airports数据融合

airports_orig <- airports[,c(5,7,8)]

colnames(airports_orig) <- c("source_airport","source_airport_lat", "source_airport_long")

airports_dest <- airports[, c(5, 7, 8)]

colnames(airports_dest) <- c("destination_airport", "destination_airport_lat", "destination_airport_long")

flights <- left_join(flights, airports_orig, by = "source_airport")

flights <- left_join(flights, airports_dest, by = "destination_airport")

#剔除缺失值

flights <- na.omit(flights, cols = c("source_airport_long", "source_airport_lat", "destination_airport_long", "destination_airport_lat"))

#最后数据如下

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

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

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

# 按航空公司拆分数据集

flights_split <- split(flights, flights$name)

# Calculate intermediate points between each two locations

flights_all <- lapply(flights_split, function(x) gcIntermediate(x[, c("source_airport_long", "source_airport_lat")], x[, c("destination_airport_long", "destination_airport_lat")], n=100, breakAtDateLine = FALSE, addStartEnd = TRUE, sp = TRUE))

# 转换为数据框

flights_fortified <- lapply(flights_all, function(x) ldply(x@lines, fortify))

# Unsplit lists

flights_fortified <- do.call("rbind", flights_fortified)

# Add and clean column with airline names

flights_fortified$name <- rownames(flights_fortified)

flights_fortified$name <- gsub("\\..*", "", flights_fortified$name)

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

flights_points <- flights_fortified %>%

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 <- readJPEG("BlackMarble_2016_01deg.jpg", native = TRUE)

earth <- rasterGrob(earth, interpolate = TRUE)

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,

label = paste("ytlogos.github.io || NASA.gov || OpenFlights.org"), color = "white", alpha = 0.5) +

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"),

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("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,

label = paste("ytlogos.github.io || NASA.gov || OpenFlights.org"), color = "white", alpha = 0.5) +

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"),

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("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,

label = paste("ytlogos.github.io || NASA.gov || OpenFlights.org"), color = "white", alpha = 0.5) +

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"),

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("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,

label = paste("ytlogos.github.io || NASA.gov || OpenFlights.org"), color = "white", alpha = 0.5) +

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"),

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("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,

label = paste("ytlogos.github.io || NASA.gov || OpenFlights.org"), color = "white", alpha = 0.5) +

coord_equal()

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

#抽取数据集

flights_subset <- c("Lufthansa", "Emirates", "British Airways")

flights_subset <- flights_fortified[flights_fortified$name %in% flights_subset, ]

flights_subset_points <- flights_subset%>%

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"),

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 = -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,

label = paste("ytlogos.github.io || NASA.gov || OpenFlights.org"), color = "white", alpha = 0.5) +

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.2     assertthat_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

 往期精彩内容整理合集 

2017年R语言发展报告(国内)

R语言中文社区历史文章整理(作者篇)

R语言中文社区历史文章整理(类型篇)

公众号后台回复关键字即可学习

回复 R                  R语言快速入门及数据挖掘 
回复 Kaggle案例  Kaggle十大案例精讲(连载中)
回复 文本挖掘      手把手教你做文本挖掘
回复 可视化          R语言可视化在商务场景中的应用 
回复 大数据         大数据系列免费视频教程 
回复 量化投资      张丹教你如何用R语言量化投资 
回复 用户画像      京东大数据,揭秘用户画像
回复 数据挖掘     常用数据挖掘算法原理解释与应用
回复 机器学习     人工智能系列之机器学习与实践
回复 爬虫            R语言爬虫实战案例分享

利用R语言绘制世界航班路线图相关推荐

  1. 利用R语言绘制时间序列图

    数据 GDP.csv文件,存储1879~2019年河南省GDP数据 绘图 # 读取数据, 首先将excel 格式的转化为 csv 格式 再读取 h <- read.table(file = &q ...

  2. 使用R语言分析世界幸福指数

    使用R语言分析世界幸福指数 本文中的数据为2019年的世界幸福指数报告,数据来源于kaggle. 数据详情 数据包含9个字段: Rank: 排名 Country or Region: 国家或地区 Sc ...

  3. R语言绘制棒棒糖图(火柴杆图)

    本博客介绍几种利用R语言绘制棒棒糖图(火柴杆图)的方法. 2. 使用原生ggplot方法 最容易也是最简单想到的方法是直接使用ggplot2包进行更新,这里需要使用ggplot本身的特性,通过图层叠加 ...

  4. 利用R语言对泰坦尼克号沉没事件幸存者的数据分析与预测

    题外话:在文章正式开始之前,我还是想先写一点题外话,一是为了引出写作这篇博客的目的,二则是希望能够记录下现在的所思所想为以后留个纪念.首先介绍一下自己,毕业3年多的小硕一枚,大学期间学的专业是高分子材 ...

  5. R语言绘制图例(legend)的各种问题_详细综合解析

    多掌握几个图例Legend,更好地利用R语言绘图. legend(x, y = NULL, legend, fill = NULL,col =, bty = "o",xjust = ...

  6. 使用R语言绘制地图,图审号地图:2019年中国地图-审图号GS(2019)1822号为基础制作的矢量shp 地图数据

    下面介绍用R语言如何绘制: 1 加载数据 · 以民政部网站数据为例,利用R语言如何下载数据和绘制地图.民政部数据的API为http://xzqh.mca.gov.cn/data/,全国边界矢量为qua ...

  7. R语言绘制热图实践(一)pheatmap包

    目录 前言 pheatmap包 pheatmap简介 常用参数介绍 使用 安装 绘制样本间相关系数图(简单使用) 差异表达基因热图(进阶使用) 如何查看pheatmap的聚类结果 pheatmap总结 ...

  8. R语言绘制不一样的条形图

    绘制条形图的方法有很多,这里介绍如果用R语言绘制一个不一样的条形图 准备数据,这里为了方便,我们使用已经存在于gcookbook包中的一个数据集 首先需要调用该包,如果该包不存在,可以使用下面的方式安 ...

  9. R语言绘制生存曲线图

    R语言绘制生存曲线图 KMunicate是支持按照Morris等人的KMunicate研究推荐的方式生成Kaplan-Meier图. 1958年,Edward L. Kaplan 和Paul Meie ...

  10. R语言绘制Bump Chart

    R语言绘制Bump Chart的小示例 # install.packages("tidyverse") # install.packages("ggbump") ...

最新文章

  1. 在Salesforce中处理Email的发送
  2. 小技巧:不用任何媒体处理软件进行视频压缩
  3. 实现进程守护 脚本命令
  4. ie6 z-index不起作用的解决方法
  5. modelsim不停出现loading……无法仿真
  6. ios 视图切换翻页效果
  7. 统计学习方法读书笔记12-逻辑斯蒂回归与最大熵
  8. Alpine linux硬盘安装
  9. 企业私有云存储选NAS还是私有云盘?3分钟带你了解企业NAS和企业云盘的区别
  10. 前端_http协议_html语言headbody基本介绍和用法
  11. 电话号码自动分隔 html,手机版网站中电话、手机号码添加链接
  12. 网络扫描技术揭秘读书笔记3--TCP SYN扫描
  13. mysql答辩会问什么_计算机科学与技术专业,毕设答辩会问什么问题?
  14. 苹果和小虫编程c语言,【OJ题库C/C++】Day12-苹果和虫子2
  15. JVM_01 内存结构(深入理解+图解)
  16. 【web前端】引入CSS(选择器)
  17. 电网视频监控系统国网B接口中注册的接口流程GB281181国标流媒体服务平台
  18. SparkCore案例练习:统计广告ID
  19. 精益生产(Lean manufacturing)
  20. 从办理社保卡的过程,说说办理流程的潜在风险

热门文章

  1. 卧槽!迅雷的代码竟然被扒了精光!
  2. MySQL 8.0 的 5 个新特性,太实用了!
  3. 两年经验,尽然斩获多家巨头offer,如:蚂蚁、头条、PingCAP~
  4. 牛逼!在IDEA里搞Spring Boot Mybatis反向工程,太爽咯~
  5. 2020年4月编程语言排行榜:少儿编程真有这么火吗?
  6. 小米造富神话虚实:平均年薪16万 470亿元期权咋分
  7. 开源巨献:Google最热门60款开源项目
  8. 刚毕业就年薪百万,谁说知识不是财富
  9. python答疑的作用_python课程答疑
  10. python中str如何使用_python中str函数的使用方法