Skip to content

Commit

Permalink
Add analysis script with comments. Update app script.
Browse files Browse the repository at this point in the history
  • Loading branch information
pagepiccinini committed Nov 4, 2016
1 parent 3aa642e commit 2adf84a
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 27 deletions.
Binary file modified .DS_Store
Binary file not shown.
68 changes: 68 additions & 0 deletions analysis.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
## LOAD PACKAGES ####
library(tidyverse) # Need for dplyr and ggplot2
library(maps) # Need for 48 states map
library(mapproj) # Need for update pro
library(albersusa) # Need for 50 states map <-- devtools::install_github("hrbrmstr/albersusa")
library(maptools) # Need for foritifying 50 states map


## READ IN DATA AND ORGANIZE ####
# Data for analysis
data_elections = read.table("data/data_us_presidential_elections.txt", header=T, sep="\t")

data_electoral = read.table("data/data_electoral_votes.txt", header=T, sep="\t")

# Get map - continental 48 states version
# states = map_data("state") %>%
# # Change name of column so it matches other data
# rename(state = region)

# Get map - all 50 states version
states_map = usa_composite()

states = fortify(states_map, region="name") %>%
# Change name of column so it matches other data
rename(state = id) %>%
# Make state names lower case to match other data
mutate(state = tolower(state))

# Organize final data for plot, combine specific data frames
data_plot = inner_join(data_elections, states)

data_result = inner_join(data_elections, data_electoral)


## MAKE MAP ####
ggplot(subset(data_plot, year == 2012),
aes(x = long, y = lat, group = group, fill = party_winner)) +
# Draw states as polygons with white borders between states
geom_polygon(color = "white") +
# Set manual colors so D and R are blue and red
scale_fill_manual(values = c("blue", "red", "yellow", "green")) +
# Update map projection to match most maps
coord_map(projection = "polyconic") +
# Remove axes and background
theme_void() +
# Move legend position and increase text size
theme(legend.position = "top", text = element_text(size = 40))


## MAKE TABLE SUMMARY OF ELECTION RESULT ####
data_sum = data_result %>%
# Focus on one specific year
filter(year == 2012) %>%
# Aggregate by each party in that election year
group_by(party_winner) %>%
# Compute total number of votes for each party
summarise(total_votes = sum(num_electoral_votes, na.rm = T)) %>%
# Ungroup for any future analyses
ungroup() %>%
# Drop any NAs for party_winner
filter(!is.na(party_winner))


## MAKE SENTENCE SUMMARY OF ELECTION RESULT ####
paste("The winner of the election was the",
filter(data_sum, total_votes == max(total_votes))$party_winner,
"party with", filter(data_sum, total_votes == max(total_votes))$total_votes,
"electoral college votes.")
67 changes: 40 additions & 27 deletions app.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
## LOAD PACKAGES ####
library(shiny)
library(tidyverse)
library(maptools)
library(maps)
library(mapproj)
library(albersusa) # devtools::install_github("hrbrmstr/albersusa")
library(albersusa)
library(maptools)


## READ IN DATA AND ORGANIZE ####
data_elections = read.table("data/data_us_presidential_elections.txt", header=T, sep="\t") %>%
mutate(year = factor(year))
# Data for analysis
data_elections = read.table("data/data_us_presidential_elections.txt", header=T, sep="\t")

data_electoral = read.table("data/data_electoral_votes.txt", header=T, sep="\t")

data_electoral = read.table("data/data_electoral_votes.txt", header=T, sep="\t") %>%
mutate(year = factor(year))
# Get map - continental 48 states version
# states = map_data("state") %>%
# rename(state = region)

states = usa_composite()
states_map = fortify(states, region="name") %>%
# Get map - all 50 states version
states_map = usa_composite()

states = fortify(states_map, region="name") %>%
rename(state = id) %>%
mutate(state = tolower(state))

data_plot = inner_join(data_elections, states_map)
# Organize final data for plot, combine specific data frames
data_plot = inner_join(data_elections, states)

data_result = inner_join(data_elections, data_electoral)

Expand All @@ -38,13 +45,15 @@ ui <- fluidPage(

# Set-up layout of main part of page
sidebarLayout(

# Add a sidebar panel
sidebarPanel(

# Add space for election year input
selectInput(inputId = "year", label = "Election Year",
choices = c(levels(data_result$year)))
choices = c(levels(factor(data_result$year))))
),

# Add main panel
mainPanel(

Expand All @@ -55,7 +64,8 @@ ui <- fluidPage(
textOutput("result_sent"),

# Add space for map
plotOutput("map")
plotOutput("result_map")

)
)
)
Expand All @@ -64,18 +74,18 @@ ui <- fluidPage(
## MAKE SERVER OUTPUTS
server <- function(input, output) {

output$map = renderPlot({
data_plot %>%
filter(year == input$year) %>%
ggplot() +
geom_polygon(aes(x=long, y=lat, group = group, fill = party_winner),
colour="white") +
scale_fill_manual(values = c("blue", "red", "yellow", "green", "purple")) +
# Make map
output$result_map = renderPlot({
ggplot(subset(data_plot, year == input$year),
aes(x = long, y = lat, group = group, fill = party_winner)) +
geom_polygon(color = "white") +
scale_fill_manual(values = c("blue", "red", "yellow", "green")) +
coord_map(projection = "polyconic") +
theme_void() +
theme(legend.position = "top", text = element_text(size = 40))
theme(legend.position = "top", text = element_text(size = 40))
})

# Save summary information as a reactive variable
data_sum = reactive({
data_result %>%
filter(year == input$year) %>%
Expand All @@ -85,20 +95,23 @@ server <- function(input, output) {
filter(!is.na(party_winner))
})

# Make table
output$result_tab = renderTable({
data_sum() %>%
# Clean up column header names to be prettier
rename(Party = party_winner) %>%
rename("Electoral College Votes" = total_votes)
},
#include.rownames=FALSE
)

# Make sentence
output$result_sent = renderText({
paste("The winner of the election was the",
filter(data_sum(), total_votes == max(total_votes))$party_winner,
"party with", filter(data_sum(), total_votes == max(total_votes))$total_votes,
"electoral college votes.")
})

output$result_tab = renderTable({
data_sum() %>%
rename(Party = party_winner) %>%
rename("Electoral College Votes" = total_votes)
},
include.rownames=FALSE
)

}

Expand Down
Binary file removed data/.DS_Store
Binary file not shown.

0 comments on commit 2adf84a

Please sign in to comment.