Pages Menu
TwitterRss
Categories Menu

Posted by on Sep 27, 2010 in R Spatial | 6 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.

 

Share on Facebook
Bookmark this on Google Bookmarks
Share on reddit
Bookmark this on Digg
Share on StumbleUpon
Share on LinkedIn

6 Comments

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

    • 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. 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. It seems the shape file link is not working. Could you please update the shape file download link ? Thanks.

    Jd

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

    Great tutorials!

Trackbacks/Pingbacks

  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 …

Post a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>