County data mapped on a national scale can sometimes be difficult to
see. The get_bls_county()
and bls_map_county()
functions both have arguments that will accommodate data and maps for
specific states or groups of states.
The example below shows the most recent unemployment rates for Florida.
library(blscrapeR)
<- get_bls_county(stateName = "Florida")
df
map_bls(map_data=df, fill_rate = "unemployed_rate",
stateName = "Florida")
Also, the states can be called together to form larger districts. Below is an example of the most recent unemployment rates for the southeastern states of Florida, Georgia and Alabama.
# Map the unemployment rate for the Southeastern United States.
<- get_bls_county(stateName = c("Florida", "Georgia", "Alabama"))
df
map_bls(map_data=df, fill_rate = "unemployed_rate", projection = "lambert",
stateName = c("Florida", "Georgia", "Alabama"))
If you don’t like any of the pre-fabricated maps, that’s alright
because blscrapeR
provides the fortified map data, which
includes longitude
, latitude
and
FIPS
codes. This data set is suitable for any kind of
ggplot2
map you can think of.
First, call the internal map data and have a look:
library(blscrapeR)
<- county_map_data
us_map head(us_map)
## long lat order hole piece group id
## 1 1225889 -1275020 1 FALSE 1 01001.1 01001
## 2 1244873 -1272331 2 FALSE 1 01001.1 01001
## 3 1244129 -1267515 3 FALSE 1 01001.1 01001
## 4 1272010 -1262889 4 FALSE 1 01001.1 01001
## 5 1276797 -1295514 5 FALSE 1 01001.1 01001
## 6 1272367 -1296730 6 FALSE 1 01001.1 01001
Notice the id
column looks a lot like one of the FIPS
codes returned by the get_bls_county()
function? This is
actually a concatenation of the state + county FIPS codes. The first two
numbers are the state FIPS and the last four are the county FIPS. These
boundaries currently represent 20015/2016 and will be updated
accordingly so they always represent the current year.
Next, produce your custom map:
library(blscrapeR)
library(ggplot2)
# Get the most recent unemployment rate for each county on a national level.
<- get_bls_county()
df # Get map data
<- county_map_data
us_map
# Insert larger breaks in unemployment rates
$rate_d <- cut(df$unemployed_rate, breaks = c(seq(0, 10, by = 2), 35))
df# Plot
ggplot() +
geom_map(data=us_map, map=us_map,
aes(x=long, y=lat, map_id=id, group=group),
fill="#ffffff", color="#0e0e0e", size=0.15) +
geom_map(data=df, map=us_map, aes_string(map_id="fips", fill=df$rate_d),
color="#0e0e0e", size=0.15) +
scale_fill_brewer()+
coord_equal() +
theme(axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
legend.title=element_blank())
What’s R mapping without some interactivity? Below we’re using two
additional packages, leaflet
, which is popular for creating
interactive maps and tigris
, which allows us to download
the exact shape files we need for these data and includes a few other
nice tools.
# Leaflet map
library(blscrapeR)
library(tigris)
library(leaflet)
<- counties(cb = TRUE, year = 2015)
map.shape <- get_bls_county()
df
# Slice the df down to only the variables we need and rename "fips" colunm
# so I can get a cleaner merge later.
<- subset(df, select = c("unemployed_rate", "fips"))
df colnames(df) <- c("unemployed_rate", "GEOID")
# Merge df with spatial object.
<- geo_join(map.shape, df, by="GEOID")
leafmap
# Format popup data for leaflet map.
<- paste0("<strong>County: </strong>",
popup_dat $NAME,
leafmap"<br><strong>Value: </strong>",
$unemployed_rate)
leafmap
<- colorQuantile("YlOrRd", NULL, n = 20)
pal # Render final map in leaflet.
leaflet(data = leafmap) %>% addTiles() %>%
addPolygons(fillColor = ~pal(unemployed_rate),
fillOpacity = 0.8,
color = "#BDBDC3",
weight = 1,
popup = popup_dat)
Note: This is just a static image since the full map would not be as portable for the purpose of documentation.