Skip to content

Instantly share code, notes, and snippets.

@reubano
Created October 12, 2011 11:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reubano/1281016 to your computer and use it in GitHub Desktop.
Save reubano/1281016 to your computer and use it in GitHub Desktop.
Choropleths in R
#load data
stateAbbr rownames(stateAbbr)unemp_data
#get county names in correct format
countyNames counties statesstates
#concatenate states and counties
unemp_data$counties
#parse out county titles & specifics
unemp_data$counties unemp_data$counties unemp_data$countiesunemp_data$counties
#define color buckets
colors = c("#F1EEF6", "#D4B9DA", "#C994C7", "#DF65B0", "#DD1C77", "#980043")
unemp_data$colorBuckets
library(maps)
#align data with map definitions
mapnamescolorsmatched
#draw map
map("county",col = colors[unemp_data$colorBuckets[match(mapnames ,unemp_data$counties)]],
fill = TRUE,resolution = 0,lty = 0,projection = "polyconic")
map("state",col = "white",fill=FALSE,add=TRUE,lty=1,lwd=1,projection="polyconic")
@reubano
Copy link
Author

reubano commented Oct 12, 2011

site: http://www.thisisthegreenroom.com/2009/choropleths-in-r/
data: http://datasets.flowingdata.com/unemployment09.csv

“state abbr.csv” is a simple file I created to provide a mapping between state names and state abbreviations — as I recall, the data format and the necessary format in R were not the same. The file looked like this: New Jersey | NJ || New Mexico | NM || New York | NY ||

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment