嘿伙计,
最近有人问我如何可视化与R的航班联系,所以我决定撰写一篇博客文章,其中包含有关该主题的简短教程:目的是绘制从NYC JFK机场到美国其他主要机场的所有可能的航班联系。为此,我们将使用以下软件包:
nycflights13:
2013年从纽约出发的航班的数据集。dplyr:
用于处理数据集的软件包。地图:
映射包。geosphere:
球面三角测量软件包。
Make sure to install and load these beforehand using the install.packages()
and library()
functions.
数据:
Lets get a quick overview on the 数据set first. Once you loaded the package nycflights13
you have access to the 数据set airports
:
library(nycflights13) airports Source: local 数据 frame [1,397 x 7] faa name lat lon alt tz dst 1 04G Lansdowne Airport 41.13047 -80.61958 1044 -5 A 2 06A Moton Field Municipal Airport 32.46057 -85.68003 264 -5 A 3 06C Schaumburg Regional 41.98934 -88.10124 801 -6 A 4 06N Randall Airport 41.43191 -74.39156 523 -5 A 5 09J Jekyll Island Airport 31.07447 -81.42778 11 -4 A 6 0A9 Elizabethton Municipal Airport 36.37122 -82.17342 1593 -4 A 7 0G6 Williams County Airport 41.46731 -84.50678 730 -5 A 8 0G7 Finger Lakes Regional Airport 42.88356 -76.78123 492 -5 A 9 0P2 Shoestring Aviation Airfield 39.79482 -76.64719 1000 -5 U 10 0S9 Jefferson County Intl 48.05381 -122.81064 108 -8 A .. ... ... ... ... ... .. ...
The 数据set contains useful metadata on airports. For our map however we only want airports that are located in the US. Therefore we will make a couple of 数据 filtering steps using the dplyr
package. We will select only those airports which have a latitude <48.5和经度>-130。然后,我们将从此数据集中排除肯尼迪国际机场,并创建一个仅包含肯尼迪国际机场的新数据框:
usairports <- filter(airports, lat < 48.5) usairports <- filter(usairports, lon > -130) usairports <- filter(usairports, faa!="JFK") #filter out jfk jfk <- filter(airports, faa=="JFK") #separate df for jfk
创建航班转机地图:
The 数据 is now prepared for our final flight connection map. The first step will be to create a basemap for the US using the 地图
package:
#create basemap map("world", regions=c("usa"), fill=T, col="grey8", bg="grey15", ylim=c(21.0,50.0), xlim=c(-130.0,-65.0)) #overlay airports points(usairports$lon,usairports$lat, pch=3, cex=0.1, col="chocolate1")
现在,地图应如下所示:
The next and final step is to add the flight connectins from NYC to all other airports using the gesophere
package and the gcIntermediate()
function.
为(i in(1:dim(usairports [[1]))){ inter <-gcIntermediate(c(jfk $ lon [1],jfk $ lat [1]),c(usairports $ lon [i],usairports $ lat [i]),n = 200) 行数(inter,lwd = 0.1,col ="绿松石2")Â Â Â }
The gcIntermediate()
function takes two points and creates a circular connection between them. With the lines()
function you add this circle to the plot. In this example this is done for all airports in the 数据frame with a for loop. The final map can be seen below:
享受最终结果!如果您对dplyr或gesophere软件包有任何疑问,请随时提问。
干杯
马丁
17条留言
您可以在这篇文章中发表评论。
哇那’s a pretty nice visual result with just a couple lines of code. Can you also load 栅格s using one of those modules?
库巴·孔奇克(Kuba Konczyk) 6年前
您可以使用名为的软件包来绘制光栅图像“raster”. First you have to load your 栅格 into R and then you can plot it with the
plot()
function. If you want to overlay points or lines, you have to be carful that both coordinate systems match. You can find a basic introduction into the 栅格 package here: R {raster}:简介和基本图解马丁 6年前
谢谢马丁,我’ll check it out 🙂
库巴·孔奇克(Kuba Konczyk) 6年前
感谢您对此发表如此精彩和简洁的文章!我从stackoverflow中的问题碰到了您的博客。也喜欢可视化中的颜色。
尼斯塔拉·兰达瓦(Nistara Randhawa) 5年前
感谢您的积极反馈。
马丁 5年前
嘿,马丁!
可视化方面的出色工作,看起来很棒!
您如何设法创建这样的细线,并使这些线看起来有点透明?
恩佐 在4年前
嘿,马丁!
可视化方面的出色工作,看起来很棒!
您如何设法创建这样的细线,并使这些线看起来有点透明?
恩佐 在4年前
嗨恩佐!实际上,这些线条看起来只是透明的,但是这是一种错觉,因为它们确实很细。因此,当两条线重叠并相交时,它们会形成一条较宽的线,看起来似乎更暗。
通过将lines()函数的lwd参数设置为0.1并通过选择与背景相比明亮的颜色来完成所有操作:
行数(inter,lwd = 0.1,col =”turquoise2″)
马丁 在4年前
嗨,马丁,
感谢您的帖子。关于您如何获得如此细线,我还有另一个问题。我在R studio中复制了您的代码,但我无法如您的示例所示那样细线。
而且无论我如何更改Lwd的值,它都不起作用。
你能帮我这个忙吗?
曹 1年前
亲爱的雷克斯,
这么晚才回复很抱歉。您设法使其成功了吗?
马丁 1年前
可视化效果很好,感谢您分享Martin!我想做一张有点相似的地图,但对于欧亚大陆(包括欧盟,俄罗斯和介于两者之间的国家),您是否知道您所描述的步骤是否可行?
阿纳斯塔西娅 3年前
It’绝对可能,但是您需要在欧亚大陆或至少机场坐标上的航班上具有可比性的数据集,以便可以模拟这样的数据集。然后,虚拟化步骤是相同的。
马丁 3年前
马丁
感谢您的出色可视化。我开始使用该代码来绘制美国的牲畜运动图,看起来很棒。我正在根据县到县的发货数量或每批发货的动物数量来加权自己的运动。简而言之,那些每年运送数量更多的县到县运动将在我的流程图中以较粗的线表示。您认为代码中可能吗?
谢谢!!
奥雷利奥 3年前
嘿奥雷利奥,
很开心听到!是!您需要分别更改每个货件的lwd(线宽)参数。
现在看起来像这样–固定宽度为0.1:
行数(inter,lwd = 0.1,col =”turquoise2″)
希望这可以帮助!
马丁 3年前
嗨,马丁,
感谢您的帖子。
我对此代码有疑问:
为(i in(1:dim(usairports [[1]))){
间<-gcIntermediate(c(jfk $ lon [1],jfk $ lat [1]),c(usairports $ lon [i],usairports $ lat [i]),n = 200)
行数(inter,lwd = 0.1,col ="turquoise2")
}
n = 200代表什么?
谢谢
雷克斯 1年前
嗨,马丁,
感谢您的帖子。关于您如何获得如此细线,我还有另一个问题。我在R studio中复制了您的代码,但我无法如您的示例所示那样细线。
而且无论我如何更改Lwd的值,它都不起作用。
你能帮我这个忙吗?
谢谢。
曹 1年前
HolaMartÃn,
地图上的中型和中级风格的杂货店,墨尔本和布宜诺斯艾利斯的中级房地产交易中心,在最终的地图上都无济于事。大家好,我很高兴。 Si pudieras ayudarme。
sal!
米里亚姆·科沃斯·费尔南德斯 1年前
发表回复