Geom_map "map_id" reference problem

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)) 
+3
source share
1 answer

A couple of things happen here. First, in the example above, I noticed that it cuts off the leading zeros of some FIPS codes. All FIPS must be five digits. You can add leading zeros by adding this line to the end of the data preparation.

 county$FIPS <- formatC(county$FIPS, width = 5, format = "d", flag = "0") 

As for ggplot, you are missing group=group in your aes (). It is hard to reproduce because I'm not sure what you are using to fill choropleth, but the following should work:

 ggplot(county, aes(long, lat, group = group)) + geom_polygon(aes(fill = YOUR_FILL_DATA), colour = alpha("white", 1/2), size = 0.2) 

EDIT: I created a random number column to use as fill speed:

 county$new.row <- sample(100, size = nrow(county), replace = TRUE) 

and executed the same ggplot code on top.

enter image description here

0
source

All Articles