I am trying to create a choropleth map of US counties with two datasets connected by FIPS codes. I use maps package county and county.fips combined into one data table (for example, not the most elegant way to integrate 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) county <- na.omit(county) setkey(county, order) county[region=="washington" & subregion=="san juan", FIPS := 53055] county[region=="washington" & subregion=="pierce", FIPS := 53053] county[region=="florida" & subregion=="okaloosa", FIPS := 12091] county[region=="louisiana" & subregion=="st martin", FIPS := 22099] county[region=="north carolina" & subregion=="currituck", FIPS := 37053] county[region=="texas" & subregion=="galveston", FIPS := 48167] county[region=="virginia" & subregion=="accomack", FIPS := 51001]
I want to use the county dataset here to create a map and use another dataset with the corresponding FIPS column to populate the respective counties. The problem occurs when using geom_map and, in particular, the map_id argument.
The following code returns an Error in unit(x, default.units) : 'x' and 'units' must have length > 0 error Error in unit(x, default.units) : 'x' and 'units' must have length > 0 when I run it with map_id=FIPS
ggplot() + geom_map(data=county, map=county, aes(x=long, y=lat, map_id=FIPS))
However, starting with map_id=region returns a normal map and starting it with map_id=subregion returns a map with two of the three states missing. The closest answer I found was this , assuming map_id needs to be set to region or id , but changing the FIPS column name did not help.
Can anyone explain what is going on here? I understand that map_id is only required as a key to another df$column ; Am I wrong about this? Ideally, I would like to link my second dataset using the FIPS column as follows:
ggplot() + geom_map(data=county, map=county, aes(x=long, y=lat, map_id=FIPS)) + geom_map(data=DT2, map=county, aes(fill=Revenue, map_id=FIPS))