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
.
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)
## region county.fips.character county.name state.name
## 1 1001 01001 autauga alabama
## 36 1003 01003 baldwin alabama
## 55 1005 01005 barbour alabama
## 15 1007 01007 bibb alabama
## 2 1009 01009 blount alabama
## 16 1011 01011 bullock alabama
## state.fips.character state.abb
## 1 01 AL
## 36 01 AL
## 55 01 AL
## 15 01 AL
## 2 01 AL
## 16 01 AL
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.
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"))
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)
choro$render()