knitr::opts_chunk$set(echo = F,
fig.align = "center",
message = F,
warning = F,
fig.width = 10,
fig.height = 8)
## Load the libraries we will be using
pacman::p_load(gapminder, socviz, tidyverse, grid, ggthemes,
usmap, maps, statebins, viridis, leaflet)
# Changing the default theme
theme_set(theme_map())
theme_update(
plot.title = element_text(hjust = 0.5,
size = 20,
face = "bold")
)
# Creating a vector for dem/rep colors
party_colors <- c("Democratic" = "#2E74C0",
"Republican" = "#CB454A")
# Saving the US states data as us_states below
us_states <- map_data(map = "state")
# Will display the election data set in the global environment
data(election)
Let’s create a map that shows which candidate each state voted for in the 2016 election.
To do that, we need to add who won each state to the us_states data set. But how do we do that?
We need a column in both data sets we can use to ID which row of the election data goes with which rows in the us_states data.
Fortunately, both data sets have a column named state!
Unfortunately, in election, the state names are capitalized
“Alabama” and in the us_states data they are all lower case “alabama”.
So we need to fix that first using the tolower()
(to lower)
function!
Since the state column in us_states is named region, let’s name the new column in election region as well!
To make joining the data set together easier, call the new data set election2 that only has the region, st, winner, party, and pct_trump columns
Now that we have the two data sets with the same column name and have matching cases, join the data sets together and name the results us_states2!
Using the code from Creating Maps with GGPlot Part 1, update
the map to show how each state voted in the 2016 presidential election.
Make sure to use the party colors from the party_colors
vector.
Next, use theme_map()
from ggthemes
and
coord_map()
with an alber’s projection
Instead of displaying the binary option of republican or democrat, have the map display the margin percentage (pct_margin).
To have the colors appear using dem blue and rep red, use
scale_fill_gradient2()
with
low = "#2E74C0"
mid = scales::muted("purple")
high = "#CB454A"
midpoint = 50
The colors are more red and purple than we’d expect. Why?
Remove the rows corresponding to Washington DC (margin of 86%)
In region it is “district of colombia”, in st it is just DC, so let’s use st to help remove all the rows with DC
Alternatively, instead of creating a whole new graph, we can replace
the data set in gg_elect_map
with a data set that doesn’t
include DC. How? By using the %+%
operator!
Now we’ll switch back to the opiates data set for our next example