Question 1

# object creation for identifying the first representative alphabetically 

q1 <- candidates %>%
  select("cand_name", "cand_pty_affiliation", "cand_office_st", "cand_office") %>%
  filter(cand_pty_affiliation %in% c("DEM","REP")) %>%
  arrange(cand_name) %>%
  slice(1)

q1
## # A tibble: 1 x 4
##   cand_name       cand_pty_affiliation cand_office_st cand_office
##   <chr>           <chr>                <chr>          <chr>      
## 1 ABATECOLA, BILL REP                  AZ             H

Question 2

# object creation for 4 selected states 

q2 <- candidates %>%
  filter(cand_office_st %in% c("CA", "FL", "TX", "NY")) %>%
  group_by(cand_office_st, cand_office, .add = TRUE) %>%
  summarize(cand_count = n(), .groups = "drop_last") %>%
  rename(State = cand_office_st) 

# graph creation for candidates across 4 states

ggplot(q2, aes(x = cand_office, y = cand_count, fill = State)) +
  geom_col(position = "dodge") +
  labs(title = "Number of 2016 Candidates \n Per Office in 4 States", x = "Office", y = "# of Candidates") +
  theme(legend.position = "top") +
  scale_x_discrete(labels=c("House", "Senate")) +
  theme_economist_white() 

Question 3

# tibble creation for candidates by party in AL

q3 <- candidates %>%
  filter(cand_office_st == "AL") %>%
  inner_join(results_house, by="cand_id") %>%
  select("cand_id", "cand_name", "party", "incumbent", "won", "general_percent") %>%
  mutate(cand_id = str_sub(cand_id, -5)) %>%
  summarize(cand_id, cand_name, party, incumbent, won, general_percent)
  
q3
## # A tibble: 16 x 6
##    cand_id cand_name                       party incumbent won   general_percent
##    <chr>   <chr>                           <chr> <lgl>     <lgl>           <dbl>
##  1 02087   "ROBY, MARTHA"                  R     TRUE      TRUE            0.488
##  2 05163   "BROOKS, MO"                    R     TRUE      TRUE            0.667
##  3 07086   "SEWELL, TERRYCINA ANDREA"      D     TRUE      TRUE            0.984
##  4 03032   "ROGERS, MICHAEL DENNIS"        R     TRUE      TRUE            0.669
##  5 01123   "BYRNE, BRADLEY ROBERTS"        R     TRUE      TRUE            0.964
##  6 03061   "SMITH, JESSE TREMAIN"          D     FALSE     FALSE           0.329
##  7 06098   "PALMER, GARY"                  R     TRUE      TRUE            0.745
##  8 01060   "YOUNG JR, LARRY DEAN"          R     FALSE     FALSE          NA    
##  9 02142   "GERRITSON, REBECCA (BECKY)"    R     FALSE     FALSE          NA    
## 10 02159   "ROGERS, ROBERT L"              R     FALSE     FALSE          NA    
## 11 02167   "MATHIS, NATHAN"                D     FALSE     FALSE           0.405
## 12 03157   "DICHIARA, LARRY"               R     FALSE     FALSE          NA    
## 13 04098   "ADERHOLT, ROBERT BROWN"        R     TRUE      TRUE            0.985
## 14 04148   "NORRIS, PHILLIP"               R     FALSE     FALSE          NA    
## 15 05202   "BOYD, WILLIE \"WILL\" DR. JR." D     FALSE     FALSE           0.332
## 16 06127   "PUTMAN, DAVID"                 D     FALSE     FALSE           0.254

Question 4

## collaborated with Elliott Detjen and Rom during Monday evening study hall 

new_president <- results_president %>%
  filter(party == "DEM", cand_id == "P00003392")

new_house <- results_house %>%
  group_by(state) %>%
  filter(party == "D", !is.na(general_votes)) %>%
  summarize(house_dem_pct = mean(general_percent, na.rm = TRUE), .groups = "drop")
  
q4 <- inner_join(new_president, new_house, by="state")

# graph creation

ggplot(q4, aes(x = general_percent, y = house_dem_pct, fill = won)) +
  geom_text(aes(label=state, color=won)) +
  xlim(0,1) +
  ylim(0,1) +
  labs(title = "2016 Democractic Presidential vs. \n Average Dem. House Results", x = "Democractic Presidential Results", y = "Average Democractic House Result", color = "Result") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme_bw() +
  theme_clean() +
  scale_color_manual(breaks =c("TRUE", "FALSE"), values= c( "dodgerblue", "lightcoral"), labels = c("Democratic","Republican"))

Question 5

# collaborated with Elliott Detjen and Rom during Monday evening study hall 

house <- results_house %>%
  select(cand_id, general_percent, incumbent) %>%
  mutate(office = "House")

senate <- results_senate %>%
  select(cand_id, general_percent, incumbent) %>%
  mutate(office = "Senate")

# table joining 

results <- rbind(house, senate)

q5 <- inner_join(campaigns, results, by="cand_id") %>%
  filter(!is.na(general_percent)) 

# graph creation

ggplot(q5, aes(x = ttl_receipts, y = general_percent, color = incumbent)) +
  geom_point(alpha = 0.5) +
  scale_x_continuous(limits = c(0, 25e6), labels = c("0", "5 mil.", "10 mil.", "15 mil.", "20 mil.", "25 mil.")) +
  labs(title = "2016 Campaign Receipts vs. Results", x = "Total Receipts", y = "General Election %", color = "Incumbent") +
  scale_color_manual(values= c( "royalblue", "salmon"), labels = c("Challenger","Incumbent")) +
  facet_wrap(~office) +
  theme(panel.spacing.x = unit(100, "mm")) +
  theme_economist()