Skip to content

Instantly share code, notes, and snippets.

@keberwein
Last active October 28, 2016 15:25
Show Gist options
  • Save keberwein/815cca98e7d99fc360ab27faa651e5cc to your computer and use it in GitHub Desktop.
Save keberwein/815cca98e7d99fc360ab27faa651e5cc to your computer and use it in GitHub Desktop.
An example of matching FIPS codes to shape files to map data in R.
library(sp)
library(ggplot2)
library(rgeos)
library(rgdal)
library(maptools)
library(tigris)
### Begin data prep
# Grab air/water quality data from the EPA
url = "https://data.cdc.gov/api/views/cjae-szjv/rows.csv?accessType=DOWNLOAD"
dat <- read.csv(url, stringsAsFactors = FALSE)
# Colnames tolower
names(dat) <- tolower(names(dat))
dat$countyname <- tolower(dat$countyname)
# Wide data set, subset only what we need.
county_dat <- subset(dat, measureid == "296",
select = c("reportyear", "countyfips","statename", "countyname", "value", "unitname"))
county_dat <- subset(reportyear == 2011, select = c("countyfips","statename", "countyname", "value", "unitname"))
# Rename columns to make for a clean df merge later.
colnames(county_dat) <- c("fips", "state", "county_name", "value", "unitname")
# Have to add leading zeos to any FIPS code that's less than 5 digits long to get a good match.
# I'm cheating by using C code. sprintf will work as well.
county_dat$fips <- formatC(county_dat$fips, width = 5, format = "d", flag = "0")
# Convert full state names to abbreviations for a clean df merge later.
county_dat$state <- state.abb[match(county_dat$state,state.name)]
### End data prep
# Download county shape file.
us.map <- tigris::counties(cb = TRUE, year = 2015)
# Remove Alaska(2), Hawaii(15), Puerto Rico (72), Guam (66), Virgin Islands (78), American Samoa (60)
# Mariana Islands (69), Micronesia (64), Marshall Islands (68), Palau (70), Minor Islands (74)
us.map <- us.map[!us.map$STATEFP %in% c("02", "15", "72", "66", "78", "60", "69",
"64", "68", "70", "74"),]
# Make sure other outling islands are removed.
us.map <- us.map[!us.map$STATEFP %in% c("81", "84", "86", "87", "89", "71", "76",
"95", "79"),]
# Projuce map
county_map <- fortify(us.map, region="GEOID")
ggplot() +
geom_map(data=county_map, map=county_map,
aes(x=long, y=lat, map_id=id, group=group),
fill="#ffffff", color="#0e0e0e", size=0.15) +
geom_map(data=county_dat, map=county_map, aes_string(map_id="fips", fill=county_dat$value),
color="#0e0e0e", size=0.15) +
scale_fill_gradientn(colors = c("green", "red")) +
coord_map(“polyconic”) +
labs(title="Air Quality") +
theme_bw() +
theme(axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
legend.title=element_blank())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment