Skip to content

Instantly share code, notes, and snippets.

@keberwein
Created June 15, 2016 18:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save keberwein/a61651d1de90476d5fb28dc08bbbc617 to your computer and use it in GitHub Desktop.
Save keberwein/a61651d1de90476d5fb28dc08bbbc617 to your computer and use it in GitHub Desktop.
Create a map of US counties by string matching county and state name.
# 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("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)]
library(maps)
library(ggplot2)
county_df <- map_data("county")
names(county_df) <- c("long", "lat", "group", "order", "state_name", "county_name")
county_df$state <- state.abb[match(county_df$state_name, tolower(state.name))]
county_df$state_name <- NULL
state_df <- map_data("state")
# Combine together
choropleth <- merge(county_df, county_dat, by = c("state", "county_name"))
choropleth <- choropleth[order(choropleth$order), ]
# Discretise rate to use with Brewer colour scheme
choropleth$rate_d <- cut_number(choropleth$value, 5)
ggplot(choropleth, aes(long, lat, group = group)) +
geom_polygon(aes(fill = rate_d), colour = alpha("white", 1/2), size = 0.2) +
geom_polygon(data = state_df, colour = "white", fill = NA)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment