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.

 

11 Comments

  1. Mark Bulling

    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, http://www.flickr.com/photos/everheardofaspacebar/4259299763/

    1. James Author

      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?

      James

  2. Mark Bulling

    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 data.london.gov.uk

    Cheers

    Mark

  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!

    Notes:

    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.

    Best,

    Will

  4. Jon

    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”).

Comments are closed.