I am trying to create a choropleth map of US counties with two datasets connected by FIPS codes. I am using the maps package county and county.fips data, combined into one data.table like this (probably not the most elegant way of integrating the FIPS data):
library(ggplot2)
library(maps)
library(data.table)
county <- map_data("county")
data(county.fips)
county.fips <- as.data.table(county.fips)
county.fips$polyname <- as.character(county.fips$polyname)
county.fips[, paste0("type", 1:2) := tstrsplit(polyname, ",")]
names(county.fips) <- c("FIPS","polyname","region","subregion")
county <- merge(county, county.fips, by=c("region", "subregion"), all=T)
county <- county[,1:7]
county <- as.data.table(county) …Run Code Online (Sandbox Code Playgroud)