12.4 Application: 2021 NJ Election Results
We sometimes get data from an outside source. We then need to figure out how to connect it to the mapping data.
<- map_data("county", region="New Jersey")
njcounties
## 2021 county election results
<- data.frame(county = unique(njcounties$subregion),
murphyvote murphy = c(43.92, 52.52, 53.28, 61.69, 36.69, 43.64,
73.96, 44.63, 73.56, 40.19, 65.09,
55.74, 40.31, 44.06, 31.79, 51.47,
35.01, 51.54, 31.93, 61.56, 34.56))
We can use merge()
to do so by indicating a shared unique identifier that the dataframes have in common. Note that subregion
is the name of the county variable in the first dataframe (the x) and county
is the name of the county variable in the second dataframe (the y). For this to work, we had to first make sure the county names are formatted exactly the same way in both dataframes. For example, R won’t known “camden” and “Camden” are the same. They have to exactly match for R to be able to properly join the data together. With messier data, you might have to rename some variable values prior to joining data in a merge, such as by changing the case of letters or adjusting punctuation, etc.
- For more information on merging, see QSS chapter 4.2.5 or this explainer.
<- merge(njcounties, murphyvote,
njcounties by.x="subregion", by.y = "county",
all.x=TRUE, all.y=F)
Now that the data are merged, we can add Murphy’s vote share as a color.
ggplot()+
## create an nj county-level plot
geom_polygon(data=njcounties, aes(x=long, y=lat,
group=group,
fill=murphy),
colour="white")+
## Shade the map according to the vote share
scale_fill_gradient(name="Murphy's Vote Share %", low="red", high="blue")+
## remove background
theme_void()+
ggtitle("2021 NJ Governor Results by County")+
coord_quickmap()