R User Group of Milano (Italy)

Maps in R: Plotting data points on a map

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:


  1. Robert /

    I frequently use the argument "scale = free_y" when faceting regular plots. Does such an option exist when plotting maps to allow different panels to sort of zoom in on particular areas?

  2. Hi Max,
    Nice post and great data source, thanks !

    Here's something else you could do with rworldmap using mapBubbles(). I just did this very quickly there are other potential refinements I may look at.

    #getting started with default options
    mapBubbles( airportA, nameX='lon', nameY='lat', nameZSize='flights', nameZColour='flights', catMethod='quantiles' )

    mapBubbles( airportA, nameX='lon', nameY='lat', nameZSize='flights', nameZColour='flights', colourPalette='topo', catMethod='pretty', numCats=6, mapRegion='Europe', colourLegendPos='bottomright', addLegend=FALSE )

    #shameless plug !
    mtext("map made using rworldmap by Andy South", line=-1, side=3, adj=1, cex=0.6)

    If users want other hints on rworldmap they can look at my paper : http://journal.r-project.org/archive/2011-1/RJournal_2011-1_South.pdf.

    New developments coming soon.
    All the best,

  3. kayhan /


    Thanks you for your post. I think to learn how to draw a heat map on the map, I should wait for your next post :(

    Thanks again,

  4. Max Marchi /

    Robert, for what I know, you can use all your regular ggplot stuff on the maps.

    Andy, thank you for your code.

    Kayhan, next time I'll show how to place colored polygons on maps. I'll explicitly deal with choropleth maps, but the process should be useful for overlaying heatmaps as well.
    Meanwhile you can look at the following links if you find something useful for you:

    • kayhan /

      Thanks Max,

      Let me tell you what I want to do:

      I have crime count (of 17 types) of Cincinnati over regular grid of size 0.02 x 0.02 (lat x lon). I would like to get the map (eg from Google) and overlay crime count over it while I can still see the underlying map. The crime count in each cell is an integer number, and the result should look like a heat map over the map of Cincinnati.

      Thanks for the link, I will look into them.

      • Max Marchi /

        You can use ggmap to get the map (as illustrated in this post) and add layers that draw a level plot on top of it. Then play with the alpha parameter to find the desired transparency level.

        If the coordinates system and projection in your data is the same of GoogleMaps, that's it.

        This link (http://www.r-bloggers.com/displaying-data-using-level-plots/) has some code for producing level plots with ggplot2.

        Time permitting, I'll try to have a specific post on this, but I can't make promises on when it's happening.

        I'm taking suggestions for open data to use for the example (given it's MilanoR's blog, preferably Italian or European data.)

        • Hi Max,

          Very nice blog, thank you.

          I cannot plot the data on the Google map. In particular, this command does not do it for me:

          > mapPoints <- ggmap(map) +
          + geom_point(aes(x = lon, y = lat, size = sqrt(flights)), data = airportD, alpha = .5)

          Could you help?

          • Max Marchi /

            Hi CA,

            can you give us details on what goes wrong? Any error message?

            Also, type sessionInfo() in your console after the non-working command and copy/paste the output here, so we can see if it's a problem of missing packages.

  5. Thanks for this useful tutorial. It would be great if you could demonstrate this over an animated time series too!

    • Max Marchi /

      Hi Jam

      I'm not sure I understand what you mean.
      Something like points that move in time over the map?
      Also if you have a particular case you're thinking of, let me know.

  6. lakshmi /

    I need to display a shape file's polygon shape on online google map.which package do i need to use to display a shape file(.shp) file on google map?.when i tried with readshapepoly() & plot() method,it is opening a seperate R window where it is opening the map which is not the actual google map,it is like a image i couldn't zoom in or out. I need to display my shapefile's polygon shape on google dynamic map(with zoom in/out).

  7. mike_roach /

    This is great and has been a big help - thanks

  8. maycca /

    Hey, thank you for promising blog ! however, I have a problem to open the data with


    apparently it doesnt read it correctly, because I have error <Error in `colnames

    Please, what is a separator for the .dat format to read it correctly? Thank you !

  9. maycca /

    Hey, thank you for promising blog ! however, I have a problem to open the data with

    airports <- read.csv("http://openflights.svn.sourceforge.net/viewvc/openflights/openflights/data/airports.dat&quot;, header = FALSE)

    apparently it doesnt read it correctly, because I have error

    Error in colnames<-(*tmp*, value = c("ID", "name", "city", "country", :
    'names' attribute [11] must be the same length as the vector [1]

    Please, what is a separator for the .dat format to read it correctly? Thank you !

Leave a Reply