Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

geom_map "map_id" reference issue

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)
    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 make the map and use a different dataset with a corresponding FIPS column to fill out the respective counties. The issue comes up when using geom_map and specifically the map_id argument.

The following code returns the 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, running it with map_id=region returns a normal map and running it with map_id=subregion returns a map with about 2 out of 3 states missing. The closest answer I found was this, suggesting that map_id needs to be set to region or id, but changing the FIPS column name didn't help.

Can anyone explain what is going on here? My understanding is that map_id is only needed as a key to another df$column; am I incorrect in that? Ideally I would like to be able to tie in my second dataset, through the FIPS column, like this:

    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))
like image 301
moman822 Avatar asked Oct 30 '22 01:10

moman822


1 Answers

A couple things going on here. First, I noticed in the example above, it's cutting off the leading zeros on some of the FIPS codes. All FIPS need to be five digits. You can add the leading zeros by adding this line to the end of the data prep.

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

As for ggplot, you're missing group=group in your aes(). It's hard to reproduce because I'm not sure what you're using for the choropleth fill, 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 generated a column of random numbers to use as a fill rate:

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

and ran the same ggplot code from above.

enter image description here

like image 173
keberwein Avatar answered Nov 15 '22 06:11

keberwein