首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

R语言可视化之绘制世界航班路线图

作者简介

taoyan:伪码农,R语言爱好者,爱开源。个人博客:

https://ytlogos.github.io/

一、 简介

本文基于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") +

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()

本文转载自R语言中文社区!

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

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券