Posted by on Jun 7, 2012 in Featured Maps, R Spatial, Slideshow, Visualisation | 17 comments

# Mapping the World’s Biggest Airlines

The map above shows the routes flown by the top 7 airlines (by international passenger distance flown). The base map shows large urban areas and I have attempted to make it look a bit like the beautiful “Earth at Night” composite image produced by NASA. You can clearly see a relationship between where people live and where the big carriers fly to across Europe and the US but India and much of China have relatively few routes. I expect much of the slack is picked up by smaller airlines in these countries but they must represent key growth areas the world economy becomes increasingly driven by the east. This map isn’t meant to be comprehensive- I just wanted to make another example of a visualisation with ggplot2.

How I did it

Plotting great circles has become an increasingly popular thing to do with R (because they look cool) and the excellent flight path data freely available from the OpenFlights website provides a neat data source to play around with. There are several tutorials out there but none of them (to my knowledge) apply apply colour value to the arcs based on a relevant variable in the datafile or make much of the underlying base map. For the Tufte fans out there this is means an important opportunity for maximising data ink has been missed.

The first step was to calculate the great circles with the flight data. The Anthrospace blog has a good tutorial on this so I won’t replicate the code here. I would warn you that it is a little tricky to sort out. For the R pros out there if you have a refinement on the code please comment below.

The next step was to source the world urban area boundaries. These can be found on the Natural Earth Data website. Direct link. In the code below I have simplified these and coloured them in the plot to reflect the colours in the NASA image. I have also coloured the background and continents accordingly. Without the great circle arcs your basemap should look a bit like this (I’m really pleased with how it came out).

With this layer in place you can now draw whatever you want on top of it. In this case it is the flight arcs. I then added a few annotations and moved the key etc in Inkscape for the final image.

```library(ggplot2) library(maps) library(rgeos) library(maptools) gpclibPermit() ```

Load in your great circles (see above for link on how to do this). You need a file that has long, lat, airline and group. The group variable is produced as part of the Anthrospace tutorial.
`gcircles`

Get a world map
`worldmap `

`urbanareasin`

Simplify these using the gsimplify function from the rgeos package
`simp`

Fortify them for use with ggplot2
`urbanareas<-fortify(simp)`

This step removes the axes labels etc when called in the plot.
```xquiet yquiet<-scale_y_continuous("", breaks=NA, lim=c(70,-70)) quiet<-list(xquiet, yquiet)```

Create a base plot
`base`

Then build up the layers
`wrld<-c(geom_polygon(aes(long,lat,group=group), size = 0.1, colour= "#090D2A", fill="#090D2A", alpha=1, data=worldmap))`
`urb`

Bring it all together with the great circles
```base+wrld+urb+geom_path(data=gcircles, aes(long,lat, group=group, colour=airline),alpha=0.1, lineend="round",lwd=0.1)+coord_equal()+quiet+opts(panel.background = theme_rect(fill='#00001C',colour='#00001C')) ```

1. Nice work James…

2. I had a problem on the last step:

Error in grid.Call.graphics(L_setviewport, pvp, TRUE) :
Non-finite location and/or size for viewport

3. should import maptools for readShapePoly

library(maptools)

• Sorry I missed that- thanks

4. still does not work

5. NASA is working really great the picture taken by NASA is awesome.

6. Hi there,

How can i get color for “airline” variable? and the flight_gcircles.csv?

Thanks

7. Hi,

runs fine for me, except for the last command gives me

breaks = NA is deprecated. Please use breaks = NULL to remove breaks in the scale. (Deprecated; last used in version 0.8.9)

Is that because I’m using ggplot2 0.9 ? Any workaround?

8. Very interesting visualization. Do you have the steps documented anywhere explaining how you created the base map from the natural earth files?

9. hello sir you have done a great job, I have confusion about what is urb stands for in the code and how to make layer file of urban areas

10. Great tutorial in deed. I used your code to project my data. I haven’t manage however to change SpatialPoints into SpatialPolygons(DataFrame)… I’ve tried several versions of your:
urbanareas<-fortify(simp)
like:
urbanareas<-ggplot2:::fortify.SpatialPolygonsDataFrame(simp)
urbanareas<-ggplot2:::fortify(simp)
urbanareas<-fortify.SpatialPolygonsDataFrame(simp)

I have to be missing something…
I would really appreciate all your suggestions.

11. Hi James, I recently found this link. Thank you very much for sharing your work. I needed to invest quite a bit of time, but I managed to replicate what you did. Given what others said in comments, I would like to leave some information for some others who may find it useful.

1) Urban data

Then, you need a line like this.

2) xquiet and yquiet

You can set up these variables. But you can add them like this when you draw a plot using ggplot2.

scale_x_continuous(breaks = NULL) +
scale_y_continuous(breaks = NULL)

3) urb
nayan seemed confused. urb is similar to wrld, so James did not probably leave information. I came up with this, and this works for me.

urb <- c(geom_polygon(aes(long, lat, group = group),
size = 0.3,
color = "#ffffff",
fill = "#ffffff",
alpha = 1,
data = urban areas))

4) ggplot2
Since this post was written, the ggplot2 package has been updated. I wrote the following to replicate James' work.

base + wrld + urb + geom_path(data = gcircles, aes(long, lat, group = group, color = Airline),alpha = 0.5, lineend = "round", lwd = 0.3) +
coord_equal() +
scale_x_continuous(breaks = NULL) +
scale_y_continuous(breaks = NULL) +
theme(axis.title.x = element_blank()) +
theme(axis.title.y = element_blank()) +
theme(panel.background = element_rect(fill = '#00001C', color = '#00001C')) +
theme(legend.position = "none")

12. エルメス時計スーパーコピー ビート秒針について非常にクールなことはそれがこのメカニズムが働く方法を見ることができる運動の白骨化したオーバービューが存在するということです。実際には、これは私が考えることができる最初の時間を私は死んでいる秒針が実行される（図を見た。私はディウィットの時間の2つの理由のためにこのようにして設計されたデウィット・学界は思っています。1つの特定のからくりを死者の秒針が動くのを許す見解を提供するために、第2のように微妙なのが「これは、確かにではなく、クォーツ時計です。」 http://www.okakaku.com/Super-Copy-195.html

13. 針を創立しスイスガス表1886年、120年以上の歴史を持つ、称賛には一番人気時計ブランドの一つスイス。ガス針表スイス先進技術設備を採用して、腕時計を各性能はカリスマ抜群で、その高い品質の防水機能、耐震設計と独特な夜光機能、保証ごとにすべて持って針だけガス表恒久の価値は、消費者の好感を受けて尊重。 http://www.bagkakaku.com/vuitton_bag/2/N51207.html

14. 1375年の個々のコンポーネントから成ると立っている約60センチメートル（約23 . 5インチ）背のユニークなコンスタンチンchaykinコンプトゥスの復活祭の時計は確かに属しました―これまで最も複雑なクロックのうち、実際には、最も複雑なロシアのクロックに応じて、chaykin。40才のロシアの時計屋、宗教のために、すべてのその形で、そして、彼らの異なるカレンダーと象徴性は、特に、本物の情熱です。 モンクレールスーパーコピー これは、彼の前のクロックとキリスト教の、ヘブライ語で腕時計に反映される、とイスラムのシンボルとカレンダー。 http://www.eevance.com/tokei/vuitton

15. 針を創立しスイスガス表1886年、120年以上の歴史を持つ、称賛には一番人気時計ブランドの一つスイス。ガス針表スイス先進技術設備を採用して、腕時計を各性能はカリスマ抜群で、その高い品質の防水機能、耐震設計と独特な夜光機能、保証ごとにすべて持って針だけガス表恒久の価値は、消費者の好感を受けて尊重。 http://www.gowatchs.com/brand-214.html

16. およそ1年前、私は私のマーク・カーソンkaラ・スポーツ腕時計はどうなるのだろう、ダイヤルと手を設計したが、特定の思想は当時のストラップに与えられました。私は、バーゼル2015年から帰ったとき、私は座った一つのゴールの設計は、高セキュリティストラップシステムを利用することができたのは、曲がった金属片の「私のマーク・カーソンkaラの場合にnatoストラップのより多くの保安を提供するツールと特徴的なマッチョの観察を提供する。 http://www.bagkakaku.com/vuitton_wallet/wallet/147.html