title: Plotting Choropleth Maps from KML Data Using ggplot2
body: <p>How do I plot a choropleth or thematic map using ggplot2 from a KML data source?</p>
  
  <p>Example KML: <a href="https://dl.dropbox.com/u/1156404/nhs_pct.kml" rel="nofollow">https://dl.dropbox.com/u/1156404/nhs_pct.kml</a></p>
  
  <p>Example data: <a href="https://dl.dropbox.com/u/1156404/nhs_dent_stat_pct.csv" rel="nofollow">https://dl.dropbox.com/u/1156404/nhs_dent_stat_pct.csv</a></p>
  
  <p>Here's what I've got so far:</p>
  
  <pre><code>install.packages("rgdal")
  library(rgdal)
  library(ggplot2)
  
  fn='nhs_pct.kml'
  
  #Look up the list of layers
  ogrListLayers(fn)
  
  #The KML file was originally grabbed from Google Fusion Tables
  #There's only one layer...but we still need to identify it
  kml=readOGR(fn,layer='Fusiontables folder')
  
  #This seems to work for plotting boundaries:
  plot(kml)
  
  #And this:
  kk=fortify(kml)
  ggplot(kk, aes(x=long, y=lat,group=group))+ geom_polygon()
  
  #Add some data into the mix
  nhs &lt;- read.csv("nhs_dent_stat_pct.csv")
  
  kml@data=merge(kml@data,nhs,by.x='Name',by.y='PCT.ONS.CODE')
  
  #I think I can plot against this data using plot()?
  plot(kml,col=gray(kml@data$A.30.Sep.2012/100))
  #But is that actually doing what I think it's doing?!
  #And if so, how can experiment using other colour palettes?
  
  #But the real question is: HOW DO I DO COLOUR PLOTS USING gggplot?
  ggplot(kk, aes(x=long, y=lat,group=group)) #+ ????
  </code></pre>
  
  <p>So my question is: how do I use eg kml@data$A.30.Sep.2012 values to colour the regions?</p>
  
  <p>And as a supplementary question: how might I then experiment with different colour palettes, again in the ggplot context?</p>