Skip to content

Instantly share code, notes, and snippets.

@Choens
Created November 5, 2010 17:04
Show Gist options
  • Save Choens/664456 to your computer and use it in GitHub Desktop.
Save Choens/664456 to your computer and use it in GitHub Desktop.
An example choropleth map using map() in R.
# Draw a basic choropleth map, using R.
# This example creates a map of the United States, based on completely
# fictional information. Since we just survived an election, I used red and
# blue as my color choices.
library(maps)
library(mapdata)
# If either library() call fails, try these.
install.packages("maps", dep=TRUE)
install.packages("mapdata", dep=TRUE)
# Create some fake data
lower.48 <- c('Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Idaho',
'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine',
'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi',
'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey',
'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma',
'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota',
'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington',
'West Virginia', 'Wisconsin', 'Wyoming')
colors <- c(rep(c('red','blue'),25), 'red')
# Draw a simple choropleth map.
map('state', region=lower.48, col=colors, fill=TRUE)
map("state",col = "white",fill=FALSE,add=TRUE,lty=1,lwd=1, bg = 'slategray')
# You only need this if you want to save your image.
#savePlot(filename="Map.png", type="png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment