US ZIP Maps

You can create a visualization of US ZIP codes with the function zip_map:

library(choroplethr)

?df_pop_zip
data(df_pop_zip)

?zip_map
zip_map(df_pop_zip)

As demonstrated above, the only required parameter to zip_map is a data.frame. You can see the optional parameters by typing ?zip_map.

Note that this visualization is a scatterplot where the position of each dot represents the longitude and latitude of the zip code and the value is expressed as a color. Also, boundaries of the states are drawn. But because boundaries of the zip codes are not drawn, this visualization is not technically a choropleth.

Data Requirements

The data.frame that you provide to zip_map must have one column named "region" and one column named "value". Your entries for "region" must exactly match how zip codes are named in the zipcode package, which is how choroplethr gets the longitude and latitude of the zip codes:

library(zipcode)

?zipcode
data(zipcode)
head(zipcode)

That is, you must use a five digit character representation of the ZIP code.

Exploring Data

The zip_map function provides two parameters to facilitate exploring data: buckets and zoom. buckets defaults to 7, which means that there are 7 colors, and an equal number of counties in each color. Valid values for buckets are integers betwen 1 and 7. If buckets is 1 then a continuous scale will be used. zoom defaults to NULL, which means that all counties are shown. You can set it to be a vector of states. You must name your states as defined in the "region" column of ?state.regions.

As an example, here is how you can use choroplethr to show the population of US ZIP codes on the West Coast.

zip_map(df_pop_zip,
        title   = "2012 Population Estimates",
        legend  = "Population",
        buckets = 1,
        zoom    = c("california", "washington", "oregon"))

Advanced Options

Any customization outside the optional parameters presented above will require you to create a ZipMap object. choroplethr uses R6 to take advantage of object-oriented programming. Here is an example of using the ggplot2_scale on the base Choropleth object to customize the palette used.

library(ggplot2)

choro = ZipMap$new(df_pop_zip)
choro$title = "2012 Population Estimates"
choro$ggplot_scale = scale_color_brewer(name="Population", palette=2, drop=FALSE)
choro$render()

Note: Care must be taken when manually setting the scale on ZipMap objects. In particular, choroplethr uses ggplot2 custom annotations to render Alaska and Hawaii as insets. This means that the scales of the insets and the main map will only be the same if you do the following

  1. for discrete scales, pass drop=FALSE to the scale (as above).
  2. for continuous scales, pass limits which encompass the minimum and maximum values for the entire dataset.


trulia/choroplethr documentation built on June 1, 2019, 1:52 a.m.