Making a Zip Code Choropleth in R Using Ggplot2 and Ggmap

Choropleth zip code

I organized your directory structure a bit differently. Despite older code snippets on the internets, you don't need to bind the data to the data frame. But, you do need to fortify polygons to use with ggplot. Also, read.csv makes a data.frame, so you don't need to re-make one from that call.

library(ggplot2)
library(maptools)
library(rgdal)
library(ggthemes)

setwd("~/Development/nova_choro")

zips <- readOGR(dsn="zip_codes/NOVA.shp", layer="NOVA")
company <- read.csv("data.csv")

# this makes the polygons usable to ggplot2
# and by using ZCTA5CE10 as the region id,
# you can then use that equivalent id field
# from the actual company data frame for the
# choropleth colors

zips_map <- fortify(zips, region="ZCTA5CE10")

gg <- ggplot()
# draw the base map polygons
gg <- gg + geom_map(data=zips_map, map=zips_map,
aes(x=long, y=lat, map_id=id),
color="#7f7f7f", fill="white", size=0.25)
# fill in the polygons
gg <- gg + geom_map(data=company, map=zips_map,
aes(fill=Growth, map_id=zip_code_area),
color="#7f7f7f", size=0.25)
# better color scheme
gg <- gg + scale_fill_distiller(palette="Greens")
# no "slashes" in the legend boxes
gg <- gg + guides(fill=guide_legend(override.aes=list(colour=NA)))
# use an actual projection (there may be a better one for NOVA
gg <- gg + coord_map()
# get rid of map chart junk
gg <- gg + theme_map()
gg

Sample Image

I did some checking and VA uses a modified Lambert Conic Conformal projection, so you can substitute the default Mercator coord_map with gg <- gg + coord_map("lambert", lat0=38.34427, lat1=39.14084) if you like and that should be close enough to what the official agencies use.

How to get zip code dataset by ggmap package in R

use revgeocode()

revgeocode(location = c(125.35, 43.8634))
Information from URL : http://maps.googleapis.com/maps/api/geocode/json?latlng=43.8634,125.35&sensor=false
[1] "Jun Zhuan Hu Tong, Nanguan Qu, Changchun Shi, Jilin Sheng, China, 130022"

And if you want more detail, specify output = 'all'

res <- revgeocode(location = c(125.35, 43.8634), output = 'all')

str(res)
# List of 2
# $ results:List of 6
# ..$ :List of 5
# .. ..$ address_components:List of 6
# .. .. ..$ :List of 3
# .. .. .. ..$ long_name : chr "Jun Zhuan Hu Tong"
# .. .. .. ..$ short_name: chr "Jun Zhuan Hu Tong"
# .. .. .. ..$ types : chr "route"
# .. .. ..$ :List of 3
# .. .. .. ..$ long_name : chr "Nanguan Qu"
# .. .. .. ..$ short_name: chr "Nanguan Qu"
# .. .. .. ..$ types : chr [1:3] "political" "sublocality" "sublocality_level_1"
# .. .. ..$ :List of 3
# .. .. .. ..$ long_name : chr "Changchun Shi"
# .. .. .. ..$ short_name: chr "Changchun Shi"
# .. .. .. ..$ types : chr [1:2] "locality" "political"
# .. .. ..$ :List of 3
# .. .. .. ..$ long_name : chr "Jilin Sheng"
# .. .. .. ..$ short_name: chr "Jilin Sheng"
# .. .. .. ..$ types : chr [1:2] "administrative_area_level_1" "political"
# ... etc


Related Topics



Leave a reply



Submit