US County Chropleths

You can create a choropleth of US Counties with the function county_choropleth:

library(choroplethr)

?df_pop_county
data(df_pop_county)

?county_choropleth
county_choropleth(df_pop_county)

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

Data Requirements

The data.frame that you provide to county_choropleth must have one column named "region" and one column named "value". Your entries for "region" must exactly match how regions are named in the map which choroplethr uses. These names are defined in the object county.regions:

library(choroplethrMaps)

?county.regions
data(county.regions)
head(county.regions)

In order to use choroplethr, you must use the naming convention in the "region" column of county.regions. That is, you must use the numeric version of the county FIPS code - i.e. you must drop any leading zeroes.

Exploring Data

The county_choropleth 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 Counties on the West Coast.

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

If you are interested in zooming in on individual counties, please see the function ?county_zoom_choropleth.

Advanced Options

Any customization outside the optional parameters presented above will require you to create a CountyChoropleth 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 = CountyChoropleth$new(df_pop_county)
choro$title = "2012 Population Estimates"
choro$ggplot_scale = scale_fill_brewer(name="Population", palette=2, drop=FALSE)
choro$render()

Note: Care must be taken when manually setting the scale on CountyChoropleth 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.