For this exercise I have created various visualizations that will provide more understanding about the 2018 US congressional elections, which occured on November 6, 2018. The first step will be to create a bar chart of the number of seats the two major parties won. Going into the elections the Democratic party had 194 seats, and the republicans had 241.

library(plyr)
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following objects are masked from 'package:plyr':
## 
##     arrange, mutate, rename, summarise
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(ggthemes)
housevote=read.csv("C:/Users/bcole/Documents/Housevotetracker.csv")
test<-ggplot(housevote, aes(WinningParty, color=WinningParty))+ geom_bar(fill=c('blue', 'red'))+ggtitle("2018 Congressional Vote by Party")+theme(plot.title = element_text(hjust=0.5))+ylab("Number of Representatives")+xlab("Political Party")+theme(legend.title=element_blank())+scale_color_manual(values =c("blue","red"))
ggplotly(test)

This first chart uses the tool Plotly, which enables us to interact with the data. This chart shows us that the Democrats gained the house, and received 235 out of 435 seats, though one election in North Carolina hasn’t been certified yet.

This next chart will demonstrate the same data, but we will be able to analyze it on a state by state level, in order to determine which states sent more Republicans, or more Democrats, to congress.

Housevotebystate<-ggplot(housevote, aes(WinningParty, color=State, fill=State))+ geom_bar()+ggtitle("2018 Congressional Vote by Party")+theme(plot.title = element_text(hjust=0.5))+ylab("Number of Representatives")+xlab("Political Party")
ggplotly(Housevotebystate)

The next chart is a histogram that shows the Margin of victory that Democratic candidates had over their opponets.

histogram<-ggplot(housevote,aes(DemMargin))+geom_histogram(aes(fill=..count.., binwidth=.05))+ ggtitle("2018 Congressional Margin Histogram")+theme(plot.title = element_text(hjust=0.5))+ylab("Number of Representatives")+xlab("Democratic Margin (Dem Votes - All Votes)/Total Votes)")+
                                        scale_fill_gradient("Blues")+scale_y_continuous(name="Count")+ scale_fill_gradient("Blues")+guides(fill=guide_legend(title=("Counts")                                                                                                                             ))
## Warning: Ignoring unknown aesthetics: binwidth
## Scale for 'fill' is already present. Adding another scale for 'fill',
## which will replace the existing scale.
ggplotly(histogram)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

From this chart it would seem like Republicans actually won a lot of contested seats. This is true, but also of note is the higher number of seats that were basically uncontested on the Democratic side. This allowed Democrats to wrack up cheap victories, while Republicans were forced to spread their money to defend a large number of seats that they would ultimately lose.

housevote$Swing3<-as.numeric(as.character(housevote$Swing3))
## Warning: NAs introduced by coercion
Swinghistogram<- ggplot(housevote,aes(Swing3))+geom_histogram(aes(fill=..count.., binwidth=.05))+ ggtitle("2018 Congressional Vote Swing Histogram")+theme(plot.title = element_text(hjust=0.5))+ylab("Count")+xlab("Swing Towards Democrats since 2016)")+guides(fill=guide_legend(title=("Counts")                                                                                                                             ))
## Warning: Ignoring unknown aesthetics: binwidth
ggplotly(Swinghistogram)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 41 rows containing non-finite values (stat_bin).

The average congressional district went significantly more Democratic when compared with the 2016 Presidential election results. Some districts (such as those in West Virgina, swung nearly 40% back towards the Democratic side)

The next chart will examine how household income influenced voting decisions in the election. People in the upper middle-class suburbs have noted their distaste for Trump, so it will be interesting to see what our results are.

incomeandvoting<-ggplot(housevote,aes(x=DemMargin, y=medianearnings))+geom_point(aes(text=paste("District:", CD,"State:", State)))+ ggtitle("Margin vs. Median Income")+theme(plot.title = element_text(hjust=0.5))+ylab("Median Income")+xlab("Democrat Margin (Dem Votes - All Votes)/ Total Vote")+geom_smooth()+theme_solarized()
## Warning: Ignoring unknown aesthetics: text
ggplotly(incomeandvoting)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

There is a slight positive trend in this data, but there is so much noise, so conclusions are hard to make.

In the next exercise, I will explore how poverty influenced voting decisions in the 2018 midterm elections.

povertyandvoting<-ggplot(housevote,aes(x=povertylast12, y=DemMargin))+geom_jitter(aes(text=paste("District:", CD,"State:", State)),size=1, width=.5)+ ggtitle("Poverty vs. Election  Margin")+theme(plot.title = element_text(hjust=0.5))+ylab("Democrat Margin")+xlab("Percentage of People in Poverty in Past Year")+geom_smooth()+theme_solarized_2()
## Warning: Ignoring unknown aesthetics: text
ggplotly(povertyandvoting)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

In general, high levels are seen in a few of the districts that vote most for Democrats, though this is mainly the result of a few outlier districts.

library(ggExtra)
texas<-filter(housevote, State=="Texas")
povertexas<-ggplot(texas,aes(x=povertylast12, y=DemMargin))+geom_point(aes(text=paste("District:", CD,"State:", State)))+ ggtitle("Poverty vs. Election  Margin in Texas")+theme(plot.title = element_text(hjust=0.5))+ylab("Democrat Margin")+xlab("Percentage of People in Poverty in Past Year")+geom_smooth()+theme_classic()
## Warning: Ignoring unknown aesthetics: text
ggplotly(povertexas)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

This chart relates the number of people in Texas who live in poverty vs. the Margin of Victory for Democratic candidates. This chart demonstrates the Democratic party’s higher margins with poor voters and the upper-middle class, as well as worse numbers with those in the lower middle class.

The main goal of the assignment is to create Great visualizations of electoral data, so I will use ggmap and tmap to create maps of electoral change in the United States. A map of the current congress was downloaded while using ArcMap, and a shapefile was downloaded.

library(raster)
## Loading required package: sp
## 
## Attaching package: 'raster'
## The following object is masked from 'package:plotly':
## 
##     select
library(tigris)
## To enable 
## caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
## 
## Attaching package: 'tigris'
## The following object is masked from 'package:graphics':
## 
##     plot
library(rgdal)
## rgdal: version: 1.3-6, (SVN revision 773)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
##  Path to GDAL shared files: C:/Users/bcole/Documents/R/win-library/3.5/sf/gdal
##  GDAL binary built with GEOS: TRUE 
##  Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
##  Path to PROJ.4 shared files: C:/Users/bcole/Documents/R/win-library/3.5/sf/proj
##  Linking to sp version: 1.3-1
library(sp
    ) 
library(tmap)
library(ggmap)
## Google Maps API Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it: see citation("ggmap") for details.
## 
## Attaching package: 'ggmap'
## The following object is masked from 'package:plotly':
## 
##     wind
Test1<-readOGR(dsn="C:/congress2",layer="Export_Output")
## OGR data source with driver: ESRI Shapefile 
## Source: "C:\congress2", layer: "Export_Output"
## with 435 features
## It has 11 fields
## Integer64 fields read as strings:  OBJECTID
## Warning in readOGR(dsn = "C:/congress2", layer = "Export_Output"): Z-
## dimension discarded
Test2<-read.csv("C:/Users/bcole/Documents/Testfile.csv")
  Test3<- merge(Test1,Test2, by="OBJECTID")
library(sp)
library(tmap)
tm_shape(Test3)+
  tm_fill("DemMargin", title = "Dem Margin",palette = "RdBu",breaks=c(-1,-.75,-.5, -0.25,0, 0.25,0.5,0.75,1))
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
## Variable "DemMargin" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.

tmap_mode("view")
## tmap mode set to interactive viewing

This has created a dataset that now contains the spatial layer downloaded, and it is now matched with the voting data.