knitr::opts_chunk$set(echo = F,
fig.align = "center",
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())
## starting httpd help server ... done
## # A tibble: 51 × 22
## state st fips total_vote vote_margin winner party pct_margin r_points
## <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr> <dbl> <dbl>
## 1 Alabama AL 1 2123372 588708 Trump Repu… 0.277 27.7
## 2 Alaska AK 2 318608 46933 Trump Repu… 0.147 14.7
## 3 Arizona AZ 4 2604657 91234 Trump Repu… 0.035 3.5
## 4 Arkansas AR 5 1130635 304378 Trump Repu… 0.269 26.9
## 5 Californ… CA 6 14237893 4269978 Clint… Demo… 0.300 -30.0
## 6 Colorado CO 8 2780247 136386 Clint… Demo… 0.0491 -4.91
## 7 Connecti… CT 9 1644920 224357 Clint… Demo… 0.136 -13.6
## 8 Delaware DE 10 443814 50476 Clint… Demo… 0.114 -11.4
## 9 District… DC 11 311268 270107 Clint… Demo… 0.868 -86.8
## 10 Florida FL 12 9502747 112911 Trump Repu… 0.0119 1.19
## # ℹ 41 more rows
## # ℹ 13 more variables: d_points <dbl>, pct_clinton <dbl>, pct_trump <dbl>,
## # pct_johnson <dbl>, pct_other <dbl>, clinton_vote <dbl>, trump_vote <dbl>,
## # johnson_vote <dbl>, other_vote <dbl>, ev_dem <dbl>, ev_rep <dbl>,
## # ev_oth <dbl>, census <chr>
## # A tibble: 5 × 6
## state total_vote r_points pct_trump party census
## <chr> <dbl> <dbl> <dbl> <chr> <chr>
## 1 Kentucky 1924150 29.8 62.5 Republican South
## 2 Ohio 5536547 8.07 51.3 Republican Midwest
## 3 New York 7721795 -22.5 36.5 Democratic Northeast
## 4 Missouri 2828266 18.5 56.4 Republican Midwest
## 5 Montana 501822 20.2 55.6 Republican West
This state level mapping data is stored in ggplot2
. We
can use the map_data()
function along with the
map = ...
argument to get the data set that has the
outlines for certain maps:
map = "state"
will be a data set for each state in the
USmap = "county"
will be a data set for each county in
the USmap = "world"
or map = "world2"
returns a
data set for each countrymap = "usa"
returns a map of just the United States
border (no state borders)For other countries, you can give map =
“italy”,
“france”, “nz”, etc…
We’ll keep it simple and just look at the US states data for now.
Let’s see what it looks like…
## long lat group order region subregion
## 1 -87.46201 30.38968 1 1 alabama <NA>
## 2 -87.48493 30.37249 1 2 alabama <NA>
## 3 -87.52503 30.37249 1 3 alabama <NA>
## 4 -87.53076 30.33239 1 4 alabama <NA>
## 5 -87.57087 30.32665 1 5 alabama <NA>
## 6 -87.58806 30.32665 1 6 alabama <NA>
## [1] 15537 6
The order column is important because when ggplot()
goes
to draw the states, it will draw them in row order.
We don’t need to re-order the rows since they are already arranged
from 1 - max in the order column, but if they weren’t arranged in the
proper order, which dplyr
verb could we use?
To create a map with ggplot()
, we use 4 main
aesthetics:
x =
the longitude of each line (long)y =
the latitude of each line (lat)group =
the column with the states’ group numbers
(group)fill =
the column you want to shade each state for. We
don’t have that in our data set yet!The geom_
you want to use is
geom_polygon()
, which will draw lines between the
x, y
coordinates in order of the rows presented for each
group
.
If you want a blank map, make sure to include
fill = 'white'
and color = "black"
inside
geom_polygon()
.
Finally, add theme_map()
from the ggthemes
package to use a more suitable theme for the graph
Try creating the map outline below!
The graph isn’t what we’d see on a globe, and some of theme look a little off. How can we fix it?
By using the Alber’s projection! To use the Alber’s projection, you
use the coord_map()
function seen in the code chunk
below!