Code UPDATED in Fall 2024 Due to change in package

Motivating Example:

In this demo we will be looking voter turn out data from IPUMS from 2016 and 2018.

## LOAD IN VOTER TURN-OUT DATA
vote<-read.csv("https://raw.githubusercontent.com/kitadasmalley/DATA502/main/FALL2021/Data/voterTurnOut1618.csv",
              header=TRUE, 
              stringsAsFactors = FALSE)

## CHECK STRUCTURE
str(vote)
## 'data.frame':    102 obs. of  9 variables:
##  $ YEAR        : int  2016 2016 2016 2016 2016 2016 2016 2016 2016 2016 ...
##  $ State       : chr  "Alabama" "Alaska" "Arizona" "Arkansas" ...
##  $ Voted       : chr  "Voted" "Voted" "Voted" "Voted" ...
##  $ nVote       : int  1208 736 881 995 4229 769 565 669 1213 2463 ...
##  $ nWgtVote    : num  2095097 307662 2769011 1241271 14416087 ...
##  $ n           : int  1691 967 1249 1401 5885 916 715 893 1417 3148 ...
##  $ nWgt        : num  2954157 412200 3940561 1754463 20343859 ...
##  $ sampPropVote: num  0.714 0.761 0.705 0.71 0.719 ...
##  $ wgtPropVote : num  0.709 0.746 0.703 0.707 0.709 ...
## CHANGE STATE NAMES TO LOWER CASE
## THIS WILL HELP WITH MATCHING LATER
vote$State<-tolower(vote$State)

## CHECK THE OUTPUT
str(vote)
## 'data.frame':    102 obs. of  9 variables:
##  $ YEAR        : int  2016 2016 2016 2016 2016 2016 2016 2016 2016 2016 ...
##  $ State       : chr  "alabama" "alaska" "arizona" "arkansas" ...
##  $ Voted       : chr  "Voted" "Voted" "Voted" "Voted" ...
##  $ nVote       : int  1208 736 881 995 4229 769 565 669 1213 2463 ...
##  $ nWgtVote    : num  2095097 307662 2769011 1241271 14416087 ...
##  $ n           : int  1691 967 1249 1401 5885 916 715 893 1417 3148 ...
##  $ nWgt        : num  2954157 412200 3940561 1754463 20343859 ...
##  $ sampPropVote: num  0.714 0.761 0.705 0.71 0.719 ...
##  $ wgtPropVote : num  0.709 0.746 0.703 0.707 0.709 ...

Step 1: State Polygons

Import the shapes using the usmaps package:

## MAPS PACKAGE
#install.packages("maps")
library(maps)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ✖ purrr::map()    masks maps::map()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## CALL THE IN THE STATE COORDINATES
states <- map_data("state")

## WHAT DOES THE OUTPUT LOOK LIKE?
head(states)
##        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>

Step 2: Joining

library(tidyverse)

## JOIN THE MAP DATA WITH THE DATA FRAME
mapPropVote<-states%>%
  rename(State=region)%>%
  left_join(vote)

head(mapPropVote)
##        long      lat group order   State subregion YEAR Voted nVote nWgtVote
## 1 -87.46201 30.38968     1     1 alabama      <NA> 2016 Voted  1208  2095097
## 2 -87.46201 30.38968     1     1 alabama      <NA> 2018 Voted   976  1830305
## 3 -87.48493 30.37249     1     2 alabama      <NA> 2016 Voted  1208  2095097
## 4 -87.48493 30.37249     1     2 alabama      <NA> 2018 Voted   976  1830305
## 5 -87.52503 30.37249     1     3 alabama      <NA> 2016 Voted  1208  2095097
## 6 -87.52503 30.37249     1     3 alabama      <NA> 2018 Voted   976  1830305
##      n    nWgt sampPropVote wgtPropVote
## 1 1691 2954157    0.7143702   0.7092028
## 2 1511 2856889    0.6459298   0.6406635
## 3 1691 2954157    0.7143702   0.7092028
## 4 1511 2856889    0.6459298   0.6406635
## 5 1691 2954157    0.7143702   0.7092028
## 6 1511 2856889    0.6459298   0.6406635

Step 3: Plot with ggplot

A) Default coloring

## LETS LOOK AT THE ELECTION YEAR
this.year=2016

## YOU CAN PIPE INTO A GGPLOT
## FILTER
## NOTE THAT GROUP IS NEEDED TO HAVE THE PROPER CONNECTIONS 
mapPropVote%>%
  filter(YEAR==this.year)%>%
  ggplot(aes(x=long, y=lat)) +
  geom_polygon(aes(fill = wgtPropVote, group=group),color="black")+
  theme_bw()+
  coord_map()

B) Color accessible

#install.packages("viridis")
library(viridis)

## SCALE FOR VIRIDIS PACKAGE
mapPropVote%>%
  filter(YEAR==this.year)%>%
  ggplot(aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill = wgtPropVote),color="black")+
  theme_bw()+
  coord_map()+
  scale_fill_viridis(option="magma", direction = -1)

C) Basic Interaction with plotly

#install.packages("plotly")
library(plotly)

## SAVE THE GGPLOT OBJECT
p<-mapPropVote%>%
  filter(YEAR==this.year)%>%
 ggplot(aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill = wgtPropVote, text=State),color="black")+
  theme_bw()+
  scale_fill_viridis(option="magma", direction = -1)

#p

## GGPLOT OBJECT IS AN INPUT TO PLOTLY
p2<-ggplotly(p, width=700, height=400)


p2