Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8965185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:45:37+00:00 2026-06-15T16:45:37+00:00

How do I plot a choropleth or thematic map using ggplot2 from a KML

  • 0

How do I plot a choropleth or thematic map using ggplot2 from a KML data source?

Example KML: https://dl.dropbox.com/u/1156404/nhs_pct.kml

Example data: https://dl.dropbox.com/u/1156404/nhs_dent_stat_pct.csv

Here’s what I’ve got so far:

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 <- 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)) #+ ????

So my question is: how do I use eg kml@data$A.30.Sep.2012 values to colour the regions?

And as a supplementary question: how might I then experiment with different colour palettes, again in the ggplot context?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:45:38+00:00Added an answer on June 15, 2026 at 4:45 pm

    Plotting maps in R is very often a pain. Here’s an answer which largely follows Hadley’s tutorial at https://github.com/hadley/ggplot2/wiki/plotting-polygon-shapefiles

    library(maptools)
    library(rgdal)
    library(ggplot2)
    library(plyr)
    
    fn='nhs_pct.kml'
    nhs <- read.csv("nhs_dent_stat_pct.csv")
    
    kml <- readOGR(fn, layer="Fusiontables folder")
    

    Note: I got a message about orphan holes. I included the following line after reading https://stat.ethz.ch/pipermail/r-help/2011-July/283281.html

    slot(kml, "polygons") <- lapply(slot(kml, "polygons"), checkPolygonsHoles)
    
    ## The rest more or less follows Hadley's tutorial 
    kml.points = fortify(kml, region="Name")
    kml.df = merge(kml.points, kml@data, by.x="id",by.y="Name",sort=FALSE)
    kml.df <- merge(kml.df,nhs,by.x="id",by.y="PCT.ONS.CODE",sort=FALSE,all.x=T,all.y=F)
    
    ## Order matters for geom_path!
    kml.df <- kml.df[order(kml.df$order),]
    
    nhs.plot <- ggplot(kml.df, aes(long,lat,group=group,fill=A.30.Sep.2012)) + 
      geom_polygon() +
      geom_path(color="gray") +
      coord_equal() +
      scale_fill_gradient("The outcome") + 
        scale_x_continuous("") + scale_y_continuous("") +   theme_bw()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I plot complex data using QwtPlot, so, I reimplement QwtSeriesData for converting my data
I am trying to plot some data using pylab scatter function. I'm using pylab.scatter(X,
I plot Iris data with ggplot2 . It seems ggplot2 will standardized the data
From Plot vectors of different length with ggplot2 , I've got my plot with
I want to create intervals (discretize/bin) of continuous variables to plot a choropleth map
I plot the following: library(ggplot2) carrots <- data.frame(length = rnorm(500000, 10000, 10000)) cukes <-
I want to plot multiple rowstacked histograms on the same graph using gnuplot. A
I am trying to plot a graph using gnuplot. I have six text files.
I am trying to plot this kind of behaviour of a data set. I
I would like to plot a heatmap on a table imported from MATLAB. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.