Pages Menu
Categories Menu

Posted by on Sep 27, 2010 in R Spatial | 7 comments

Maps with ggplot2

The ggplot2 package offers powerful tools to plot data in R. The plots are designed to comply with the “grammar of graphics” philosophy and can be produced to a publishable level relatively easily. For users wishing to create a good map without too much thought I would recommend this worksheet. For those without their own shapefiles who rely on the “maps” package they may wish to consult Hadley Wickham‘s ggplot2 book.

Data Requirements:

London Sport Participation Shapefile. Download (requires unzipping)

poly_coords function. Download

Install the following packages (if you haven’t already done so):

maptools, RColorBrewer, ggplot2

Click here to view the tutorial code.



  1. Interesting.

    I’ve been using fortify.SpatialPolygonDataFrame within the ggplot package to get boundaries out of shapefiles – this creates a dataframe that has an id for each boundary (think local authority, etc) along with coords and most importantly, the order of the coords.

    From this, it’s easy to merge on datasets (assuming a common set of names for LAs for example) and use all the plotting capabalities of ggplot (think geom_polygon)

    see for example,

    • Hi Mark,

      I too have been using it in that way, but as I said in the comments, have been having problems on my mac and with the 64bit installation on Windows…Nice plots would you mind sharing the code?


  2. Hi James

    My bad – the code wasn’t wrapped in my browser, so hadn’t noticed it when scanning through – i’ve used fortify on both mac and 32bit win without issue.

    will email you the code separately – most is fully “web based” as it uses csvs from



  3. These tutorials are just what I needed to get into GIS with R. This site is definitely going into the GIS folder of my rss reader. Great work!


    To get the picture on your this site, which is different from what is generated in the code, we could, for example, update the code you wrote to include ‘guide=”legend”,breaks=c(0,5,10,15,20,25,30)’ within the function scale_fill_gradientn and 7 instead of 5 in the new_fill function.



  4. It seems the shape file link is not working. Could you please update the shape file download link ? Thanks.


  5. +1 on the shapefile link not working – could you please update it?

    Great tutorials!

  6. Just wanted to say thanks for the Poly_Coords function provided here. I too fell foul of the gpclib restrictions, and I just could not get this to install on 64bit windows: your solution is a great alternative.

    I did wonder whether you had worked on an extension to this which accommodates ‘holes’. I have access to some high quality shapefiles through licenced software, but they include lakes, tarns, lochs, etc. which results in the regions not rendering in R: this means much of Scotland disappears! Ideally, I’d just have these holes block filled – i.e. ignored.

    Any help (or pointers) would be much appreciated. (My level in R is “novice”).


  1. Spatial analysis, un blog (anglophone) sur l’analyse spatiale | QUANTI / sciences sociales - [...] Créer des cartes avec l’extension ggplot2 [...]
  2. Schoenere Karten mit R und ggplot2 | stk - [...] einmal mit gpclibPermit() die Erlaubnis geben muessen, diese Teilfunktion auch zu benutzen. (Danke an diese Tutorials und Beispiele, die…
  3. Map Attribute Data in R using GGPLOT2 | Getting Under the Water - [...] In this post I map some attribute data using the R software package GGPLOT2. The code primarily comes from…
  4. Mapping in R with ShinyApps | thedataanalyst - […] Given the volume of online references to fortify, you would be forgiven for assuming everyone working online is operating…

Post a Reply

Your email address will not be published. Required fields are marked *