Skip to content

Instantly share code, notes, and snippets.

@jlowin
Created November 13, 2009 03:04
Show Gist options
  • Save jlowin/233556 to your computer and use it in GitHub Desktop.
Save jlowin/233556 to your computer and use it in GitHub Desktop.
choropleth ggplot code
library(ggplot2)
#extract reference data
mapcounties <- map_data("county")
mapstates <- map_data("state")
#merge data with ggplot county coordinates
mapcounties$county <- with(mapcounties , paste(region, subregion, sep = ","))
mergedata <- merge(mapcounties, unemp_data, by.x = "county", by.y = "counties")
mergedata <- mergedata[order(mergedata$order),]
#draw map
map <- ggplot(mergedata, aes(long,lat,group=group)) + geom_polygon(aes(fill=colorBuckets))
map <- map + scale_fill_brewer(palette="PuRd") + coord_map(project="globular") + opts(legend.position = "none")
#add state borders
map <- map + geom_path(data = mapstates, colour = "white", size = .75)
#add county borders
map <- map + geom_path(data = mapcounties, colour = "white", size = .5, alpha = .1)
map
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment