In the introductory post of this series I showed how to plot empty maps in R.
Today I'll begin to show how to add data to R maps. The topic of this post is the visualization of data points on a map.

We will use a couple of datasets from the OpenFlight website for our examples.
After loading the airports.dat file let's visualize the first few lines.

Latitude and longitude are reported for every airport in the dataset.
Let's draw the map of Europe with the help of rworldmap package, as was shown in the previous post on maps:

Then we can easily lay the airports over the map:

Map of European Airports

Adding dimensions

In the introductory post I mentioned that ggmap actually builds on the ggplot graphics engine, thus all the strengths of ggplot are available when mapping data with ggmap.
Here I will show a couple of examples on how to take advantage of this.

Let's load another dataset from OpenFlights in R.

Starting from the routes dataset, let's count the both number of routes departing from and arriving to a particular airport. I'm using another very useful package by Hadley Wickham for this task.

Then, let's add the info on departing and arriving flights to the airports dataset (which contains the coordinates data.)

The goal is now to plot the airports on the map of Europe as circles whose area is proportional to the number of departing flights.

The first step is to get the map from GoogleMaps (or one of the other available services), like was shown last time.

The following lines already get us quite close to producing the desired chart.

The ggmap command prepares the drawing of the map. The geom_point function adds the layer of data points, as would be normally done in a ggplot. A thorough explanation of ggplot is well beyond the scope of this post, but here are quick details on what is passed to geom_point:
- aes indicates how aesthetics (points in this case) are to be generated; the lon variable is associated to the x axis, lat to y, and the size of the points is proportional to the value of the variable flights (actually to its square root;)
- data indicates the dataset where the variable passed to aes are to be found;
- the alpha parameter controls the transparency of the plotted points (some degree of transparency will make the overlapping circles distinguishable.)

And here's what appears on the R plotting window when one types mapPoints in the console.

European Airports and departing routes

A few tweaks to the legend (so that it does report the actual number of departures rather than the square root,) and the chart is ready for publication.

European Airports by departing routes

Once more, the map is a ggplot (type class(mapPoints) in your console to check) thus a nearly unlimited set of operations can be performed to improve it. For example, the number of departing flights could be portrayed by the color of the circles rather than their dimension.

As a final example for this post, I'll show the code to perform faceting. In other words we will have a couple of panels, one reporting the departing flights, the other the incoming ones.

European Airports by departing and incoming routes

What's next

Next time we will deal with geographically aggregated data and how to display them in choropleth maps.

View (and download) the full code:

Print Friendly, PDF & Email

Related Post