Creates a Data Frame of US Zip Codes in a Given Radius from a Given US Zip Code

Generates a data frame of US zip codes and their distance to the given zip code when given a starting zip code and a radius in miles. Also includes functions for use with 'choroplethrZip' which are detailed in the vignette.


Stephen Ewing (EAVWing)

August 9, 2018

This package has been submitted to CRAN but is also available on my Github page:

library(devtools)
install_github('EAVWing/ZipRadius')

If you give it a zip code and a radius in miles it returns a data frame containing all of the zip codes that have a center point within the distance specified by the radius.

For example these are the 8 zipcodes within 3.5 miles of zip code 30316:

test <- zipRadius("30316", 3.5)
test
##     zip    city state latitude longitude Distance
## 1 30303 Atlanta    GA 33.75286 -84.39013     3.26
## 2 30307 Atlanta    GA 33.76821 -84.33786     2.68
## 3 30312 Atlanta    GA 33.74574 -84.37640     2.33
## 4 30315 Atlanta    GA 33.70960 -84.38365     2.82
## 5 30316 Atlanta    GA 33.72951 -84.34087     0.00
## 6 30317 Atlanta    GA 33.75001 -84.31854     1.91
## 7 30335 Atlanta    GA 33.75241 -84.38968     3.22
## 8 31120 Atlanta    GA 33.74000 -84.38000     2.36

It also includes 3 functions which can be used with Ari Lamstein's choroplethrZip package. choroplethrZip is too big to be supported by CRAN so if you want to use it you should install it with:

library(devtools)
install_github('arilamstein/[email protected]')

The get states function gives a character vetctor of the states in a zip radius. Here are the states within 200 miles of 30316:

getStates("30316", 200)
## [1] "alabama"        "florida"        "georgia"        "north carolina"
## [5] "south carolina" "tennessee"

Sometimes you'll need a character vector of the zip codes instead of a data frame:

getZips("30316", 3.5)
## [1] "30303" "30307" "30312" "30315" "30316" "30317" "30335" "31120"

You also could want a data frame that includes just the zip codes as region and the population for those zip codes. This uses an innerjoin with the population data frame from choroplethrZip so any zip codes that don't appear in both our zip list used for distance measurements and their zip list with populations are dropped. You'll see this now returns 6 zip codes vs. the 8 returned with the zipRadius function.

getZipPop("30316", 3.5)
## # A tibble: 6 x 2
##   region value
##   <chr>  <dbl>
## 1 30303   4255
## 2 30307  17344
## 3 30312  18060
## 4 30315  33761
## 5 30316  31282
## 6 30317  11520

Finally, here's an example of making a choroplethr of the zip codes in the radius. I haven't included this function in the package because I can't import choroplethrZip into the package due to its size.

library(ZipRadius)
library(choroplethrZip)
library(ggplot2)
 
makeZipMap <- function(zipcode, radius){
  choro = choroplethrZip::ZipChoropleth$new(getZipPop(zipcode, radius))
  suppressMessages(suppressWarnings(choro$prepare_map()))
  choro$legend = "Population"
  ec_zips = getZips(zipcode, radius)
  ec_df   = choro$choropleth.df[choro$choropleth.df$region %in% ec_zips,]
  ec_plot = choro$render_helper(ec_df, "", choro$theme_clean())
 
  ec_plot + ggplot2::coord_map()
}
makeZipMap("30316", 500)

News

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("ZipRadius")

1.0.1 by Stephen Ewing, a year ago


Browse source code at https://github.com/cran/ZipRadius


Authors: Stephen Ewing <[email protected]>


Documentation:   PDF Manual  


GPL (>= 2) license


Imports dplyr, geosphere, magrittr, testthat, rlang, ggplot2

Suggests knitr, rmarkdown


See at CRAN