Search code examples
rmapsfips

Are county FIPS accurate in R maps()?


When I run the following code...

require(maps)
colors <- data.frame(county=county.fips$polyname,color=rep("#FFFFFF",nrow(county.fips)), stringsAsFactors=FALSE)
colors[colors$county=="arizona,maricopa","color"] <- "#ABCABC"
map("county", col = colors$color, fill = TRUE)

I get a highlighted value for a county that is not Maricopa... It's Mohave county.

usa counties

Am I doing something wrong, or is the data suspect?

I'm using maps_2.3-11


Solution

  • The short answer is that you're doing it wrong. The names you're accessing are not in the same order as the polygons in the county database. To do what you want you should use the following:

    map("county")
    map("county", "arizona,maricopa", col="#ABCABC", fill=T, add=T)
    

    enter image description here

    As an alternative, if you really do need to map ancillary values by state,county name you can do something like the following:

    ##  Get state,county names in the order they will be plotted:
    c <- map("county", namesonly=T)
    c1 <- rep("#FFFFFF", length(c))
    c1[which(c=="arizona,maricopa")] <- "#FF0000"
    map("county", col=c1, fill=T)
    

    enter image description here