Color in counties on a map

Vedda

I have a list of counties that I would like to color in on a map, but the only way I've found to do it is with lat/long. This is not as accurate as I want to make sure all the counties are represented and not just a dot.

Is it possible to color in the county borders?

Example:

dput:

    db_loc <- structure(list(X = 1:10, fips = c(5001L, 5001L, 5001L, 5001L, 
5001L, 5001L, 5001L, 5001L, 5001L, 5001L), zip = c(72003L, 72026L, 
72038L, 72042L, 72048L, 72055L, 72073L, 72140L, 72160L, 72166L
), city = c("Almyra", "Casscoe", "Crocketts Bluff", "De Witt", 
"Ethel", "Gillett", "Humphrey", "Saint Charles", "Stuttgart", 
"Tichnor"), latitude = c(34.403216, 34.505369, 34.438327, 34.283347, 
34.28965, 34.109348, 34.396301, 34.383661, 34.479852, 34.061917
), longitude = c(-91.40953, -91.30213, -91.26907, -91.32515, 
-91.13632, -91.36875, -91.66201, -91.15428, -91.53854, -91.24828
)), .Names = c("X", "fips", "zip", "city", "latitude", "longitude"
), row.names = c(NA, 10L), class = "data.frame")

Map Data:

library(ggmap)

map <- get_map(location='arkansas', zoom=8, maptype = "terrain",
             source='google',color='color')

ggmap(map) + geom_point(
                aes(x=longitude, y=latitude, show_guide = TRUE), 
                data=db_loc, alpha=.8, na.rm = T) 

Output:

enter image description here

Serban Tanasa

There are packages in R that can help you use these out of the box. There are ample instructions and examples here:
https://github.com/arilamstein/choroplethr

You should be able to install chroplether via standard install, i think:
install.packages("choroplethr")

county maps in color

If you prefer, you could go get your own map from the US census bureau website here:

As of 10/09/2015, this link was valid:
https://www.census.gov/geo/maps-data/data/cbf/cbf_counties.html

Get the shapefiles or the kml files. If you don't know what those are, just google it. rgdal package should read that. Again, read the manuals. That will give you a structure with the outlines of all counties.


Most counties have a FIPS code, you must key your data on either the FIPS code or on the geoIDs from the rgdal file. Choroplethr should automate this to some degree. Just follow their examples here.


If that's still too hard, here's a fully worked out example. I haven't used your data, since it contains only one county (FIPS 05001, Arkansas County).

library(ggplot2)
set.seed(1)    # for reproducible example
map.county <- map_data('county')
counties   <- unique(map.county[,5:6])
akcounties <- counties[counties$region=="arkansas",]
akcounties

#This is where you'd select whatever you want to plot
# `Obesity` is the first thing to come to mind
# no offense meant to the people of the great state of Arkansas :)
# it's defined as a random uniform draw between 0-100 in the 3rd line below:
obesity_map <- data.frame(state_names=akcounties$region, 
                          county_names=akcounties$subregion, 
                          obesity= runif(nrow(akcounties), min=0, max=100))

# replace the value above with whatever variable you want to plot, # and a subset of the counties you actually care about

library(data.table)   # use data table merge - it's *much* faster
map.county <- data.table(map.county[map.county$region=="arkansas",])
setkey(map.county,region,subregion)
obesity_map <- data.table(obesity_map)
setkey(obesity_map,state_names,county_names)
map.df      <- map.county[obesity_map]

ggplot(map.df, aes(x=long, y=lat, group=group, fill=obesity)) + 
geom_polygon()+coord_map()

pic arkansas

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related