library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(readr)
library(ggplot2)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
VR <- read_csv('Voter_Registration_Data_(oregon_2024).csv')
## Rows: 7920 Columns: 10
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (9): COUNTY, SR Code, SR Desc, SS Code, SS Desc, Cong Code, Cong Desc, P...
## dbl (1): SUM(PARTYCOUNT)
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
VR <- VR %>% select(-'SR Code', -'SS Code', -'SS Desc')

max_date <- max(VR$Date)

VR <- VR %>% filter(Date == max_date)



oregon_totals <-VR %>% group_by(PARTY) %>% summarise(Total_Registered = sum(`SUM(PARTYCOUNT)`, na.rm = TRUE)) %>% mutate(Percent_Registered = round((Total_Registered/sum(Total_Registered)) * 100,2) )


oregon_county_totals <-VR %>% group_by(COUNTY,PARTY) %>% summarise(Total_Registered = sum(`SUM(PARTYCOUNT)`, na.rm = TRUE)) %>% mutate(Percent_Registered = round((Total_Registered/sum(Total_Registered)) * 100,2) )
## `summarise()` has grouped output by 'COUNTY'. You can override using the
## `.groups` argument.
most_repub_counties <- oregon_county_totals %>% select(COUNTY, PARTY, Percent_Registered) %>% filter(PARTY == 'Republican') 
most_repub_counties <- most_repub_counties %>% arrange(desc(Percent_Registered)) %>% head(10)


most_demo_counties <- oregon_county_totals %>% select(COUNTY, PARTY, Percent_Registered) %>% filter(PARTY == 'Democrat') 
most_demo_counties <- most_demo_counties %>% arrange(desc(Percent_Registered)) %>% head(10)


most_indy_counties <- oregon_county_totals %>% select(COUNTY, PARTY, Percent_Registered) %>% filter(PARTY == 'Independent') 
most_indy_counties <- most_indy_counties %>% arrange(desc(Percent_Registered)) %>% head(10)


oregon_totals_pie <- ggplot(data = oregon_totals, aes(x="", y=Percent_Registered, fill=PARTY)) +
  geom_bar(stat="identity", width=1) +
  coord_polar("y", start=0) + theme_void()

oregon_totals_bar <- ggplot(data = oregon_totals, aes( x = PARTY, y = Percent_Registered)) + geom_bar(stat = 'identity', aes(fill=PARTY)) + labs( y = "Percent Registered Voters (%)")+ theme(axis.text.x = element_text(angle = 45, hjust = 1)) 


#vector of smaller percentage parties. These will be grouped together into a single group 'other'
other_parties <- c('Constitution', 'Libertarian', 'No Labels', 'Other', 'Pacific Green', 'Progressive', 'Working Families Party of Oregon')

#A copy of the Oregon county totals DF where smaller parties are aggregated into a single category  
OCT_other_agg <- oregon_county_totals
OCT_other_agg <- OCT_other_agg %>% mutate(PARTY = case_when(
    PARTY %in% other_parties ~ "Other",
    TRUE ~ PARTY
))

OCT_other_agg <- OCT_other_agg %>% group_by(COUNTY,PARTY) %>% summarise(Total_Registered = sum(`Total_Registered`, na.rm = TRUE)) %>% mutate(Percent_Registered = round((Total_Registered/sum(Total_Registered)) * 100,2) )
## `summarise()` has grouped output by 'COUNTY'. You can override using the
## `.groups` argument.
OT_other_agg <- oregon_totals

OT_other_agg <- OT_other_agg %>% mutate(PARTY = case_when(
    PARTY %in% other_parties ~ "Other",
    TRUE ~ PARTY
))

OT_other_agg <- OT_other_agg %>% group_by(PARTY) %>% summarise(Total_Registered = sum(`Total_Registered`, na.rm = TRUE)) %>% mutate(Percent_Registered = round((Total_Registered/sum(Total_Registered)) * 100,2) )




oregon_totals_agg_pie <- ggplot(data = OCT_other_agg, aes(x="", y=Percent_Registered, fill=PARTY)) +
  geom_bar(stat="identity", width=1) +
  coord_polar("y", start=0) + theme_void()



oregon_totals_agg_bar <- ggplot(data = OCT_other_agg, aes( x = PARTY, y = Percent_Registered)) + geom_bar(stat = 'identity', aes(fill=PARTY)) + labs( y = "Percent Registered Voters (%)")




other_parties_df <- oregon_totals %>% filter(PARTY %in% other_parties)
main_parties_df <- OCT_other_agg %>% filter(!PARTY %in% other_parties)


other_parties_bar <- ggplot(data = other_parties_df, aes( x = PARTY, y = Percent_Registered)) + geom_bar(stat = 'identity', width = 0.8, aes(fill=PARTY)) + labs( y = "Percent Registered Voters (%)") + theme(axis.text.x = element_text(angle = 45, hjust = 1)) 








oregon_totals
## # A tibble: 11 × 3
##    PARTY                            Total_Registered Percent_Registered
##    <chr>                                       <dbl>              <dbl>
##  1 Constitution                                 3868               0.13
##  2 Democrat                                   998171              32.8 
##  3 Independent                                144005               4.74
##  4 Libertarian                                 20322               0.67
##  5 No Labels                                    2325               0.08
##  6 Nonaffiliated                             1111398              36.6 
##  7 Other                                       14444               0.48
##  8 Pacific Green                                7872               0.26
##  9 Progressive                                  3767               0.12
## 10 Republican                                 725407              23.9 
## 11 Working Families Party of Oregon             8347               0.27
oregon_county_totals
## # A tibble: 396 × 4
## # Groups:   COUNTY [36]
##    COUNTY PARTY         Total_Registered Percent_Registered
##    <chr>  <chr>                    <dbl>              <dbl>
##  1 BAKER  Constitution                26               0.2 
##  2 BAKER  Democrat                  1844              14.1 
##  3 BAKER  Independent                745               5.71
##  4 BAKER  Libertarian                 77               0.59
##  5 BAKER  No Labels                    8               0.06
##  6 BAKER  Nonaffiliated             4445              34.1 
##  7 BAKER  Other                       30               0.23
##  8 BAKER  Pacific Green               20               0.15
##  9 BAKER  Progressive                  4               0.03
## 10 BAKER  Republican                5820              44.6 
## # ℹ 386 more rows
most_repub_counties
## # A tibble: 10 × 3
## # Groups:   COUNTY [10]
##    COUNTY  PARTY      Percent_Registered
##    <chr>   <chr>                   <dbl>
##  1 GRANT   Republican               49.5
##  2 LAKE    Republican               49.4
##  3 HARNEY  Republican               48.7
##  4 WHEELER Republican               48.4
##  5 SHERMAN Republican               47.7
##  6 WALLOWA Republican               46.2
##  7 BAKER   Republican               44.6
##  8 CROOK   Republican               43.7
##  9 GILLIAM Republican               42.2
## 10 UNION   Republican               41.9
most_demo_counties
## # A tibble: 10 × 3
## # Groups:   COUNTY [10]
##    COUNTY     PARTY    Percent_Registered
##    <chr>      <chr>                 <dbl>
##  1 MULTNOMAH  Democrat               49.7
##  2 BENTON     Democrat               41.6
##  3 HOOD RIVER Democrat               38.3
##  4 LANE       Democrat               36.3
##  5 WASHINGTON Democrat               36.0
##  6 LINCOLN    Democrat               33.0
##  7 CLACKAMAS  Democrat               32.4
##  8 CLATSOP    Democrat               31.6
##  9 DESCHUTES  Democrat               29.8
## 10 TILLAMOOK  Democrat               28.6
most_indy_counties
## # A tibble: 10 × 3
## # Groups:   COUNTY [10]
##    COUNTY    PARTY       Percent_Registered
##    <chr>     <chr>                    <dbl>
##  1 CURRY     Independent               6.14
##  2 DESCHUTES Independent               5.93
##  3 BAKER     Independent               5.71
##  4 LINCOLN   Independent               5.65
##  5 GRANT     Independent               5.53
##  6 CROOK     Independent               5.44
##  7 CLATSOP   Independent               5.39
##  8 COOS      Independent               5.37
##  9 DOUGLAS   Independent               5.25
## 10 KLAMATH   Independent               5.25
oregon_totals_agg_pie

oregon_totals_agg_bar

other_parties_bar

oregon_totals_bar

OT_other_agg
## # A tibble: 5 × 3
##   PARTY         Total_Registered Percent_Registered
##   <chr>                    <dbl>              <dbl>
## 1 Democrat                998171              32.8 
## 2 Independent             144005               4.74
## 3 Nonaffiliated          1111398              36.6 
## 4 Other                    60945               2   
## 5 Republican              725407              23.9
OCT_other_agg
## # A tibble: 180 × 4
## # Groups:   COUNTY [36]
##    COUNTY PARTY         Total_Registered Percent_Registered
##    <chr>  <chr>                    <dbl>              <dbl>
##  1 BAKER  Democrat                  1844              14.1 
##  2 BAKER  Independent                745               5.71
##  3 BAKER  Nonaffiliated             4445              34.1 
##  4 BAKER  Other                      194               1.49
##  5 BAKER  Republican                5820              44.6 
##  6 BENTON Democrat                 25436              41.6 
##  7 BENTON Independent               2760               4.52
##  8 BENTON Nonaffiliated            19506              31.9 
##  9 BENTON Other                     1160               1.9 
## 10 BENTON Republican               12207              20.0 
## # ℹ 170 more rows
#plots, tables and graphs

party_colors <- c("Republican" = "red", "Democrat" = "blue", "Nonaffiliated" = "green", "Other" = "yellow", "Independent" = "brown", "Constitution" = "violet", "Libertarian" = "black", "No Labels" = "darkgreen", "Pacific Green" = "tan", "Progressive" = "lightblue", "Working Families Party of Oregon" = "darkred")

OT_other_agg <- OT_other_agg %>%
  arrange(desc(PARTY)) %>%
  mutate(cumulative = cumsum(Percent_Registered),
         midpoint = cumulative - Percent_Registered / 2,
         label = paste0(Percent_Registered, "%"))


OT_pie <- ggplot(data = OT_other_agg, aes(x="", y=Percent_Registered, fill=PARTY)) +
  geom_bar(stat="identity", width=1) +
  coord_polar("y", start=0) + theme_void() + scale_fill_manual(values = party_colors) + geom_text(aes(label = label, y = midpoint), color = "black", fontface = "bold") + ggtitle("          Registered Voters in Orgeon by Party (2024)") 



OT_bar <-  ggplot(data = OT_other_agg, aes( x = PARTY, y = Percent_Registered)) + geom_bar(stat = 'identity', aes(fill=PARTY)) + labs( y = "Percent Registered Voters (%)", x = "Part Affiliation") + scale_fill_manual(values = party_colors) + ggtitle("              Registered Voters in Orgeon by Party (2024)")



OCT_scatter <- ggplot(data = OCT_other_agg, aes(x = COUNTY, y = Percent_Registered,)) + geom_histogram(stat = 'identity', aes(fill = PARTY)) + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + scale_fill_manual(values = party_colors) + labs(y = "Registered Voters (%)", x = "County") + ggtitle("          Party Affiliation of Oregon Voters by County (2024)")
## Warning in geom_histogram(stat = "identity", aes(fill = PARTY)): Ignoring
## unknown parameters: `binwidth`, `bins`, and `pad`
oregon_totals_bar <- ggplot(data = oregon_totals, aes( x = PARTY, y = Percent_Registered)) + geom_bar(stat = 'identity', aes(fill=PARTY)) + labs( y = "Percent Registered Voters (%)", x = "Party Affiliation")+ theme(axis.text.x = element_text(angle = 45, hjust = 1)) + scale_fill_manual(values = party_colors) + ggtitle("          Registered Voters in Orgeon by Party (2024)")


other_parties_df <- oregon_totals %>% filter(PARTY %in% other_parties)

other_parties_bar <- ggplot(data = other_parties_df, aes( x = PARTY, y = Percent_Registered)) + geom_bar(stat = 'identity', width = 0.8, aes(fill=PARTY)) + labs( y = "Percent Registered Voters (%)", x = "Party Affiliation") + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + scale_fill_manual(values = party_colors) + ggtitle("   Oregon Voter Affiliation for Minor Political Parties (2024)")


most_repub_counties <- oregon_county_totals %>% select(COUNTY, PARTY, Percent_Registered) %>% filter(PARTY == 'Republican') 
most_repub_counties <- most_repub_counties %>% arrange(desc(Percent_Registered)) %>% head(10)


most_demo_counties <- oregon_county_totals %>% select(COUNTY, PARTY, Percent_Registered) %>% filter(PARTY == 'Democrat') 
most_demo_counties <- most_demo_counties %>% arrange(desc(Percent_Registered)) %>% head(10)


party_change <- data.frame(
  
  Party = c("Democrat", "Republican", "Independent", "Other", "Nonaffiliated"), 
  Percent_change = c(-2.89, -1.97, 0.17, -0.1, 4.8)
  
)

percent_change_bar <-  ggplot(data = party_change, aes( x = Party, y = Percent_change)) + geom_bar(stat = 'identity', aes(fill=Party)) + labs( y = "Chane in Party Membership (%)", x = "Party") + scale_fill_manual(values = party_colors) + ggtitle("        Percent Change in Party Membership from 2020 to 2024")









OT_pie

OT_bar

OCT_scatter

oregon_totals_bar

other_parties_bar

most_repub_counties
## # A tibble: 10 × 3
## # Groups:   COUNTY [10]
##    COUNTY  PARTY      Percent_Registered
##    <chr>   <chr>                   <dbl>
##  1 GRANT   Republican               49.5
##  2 LAKE    Republican               49.4
##  3 HARNEY  Republican               48.7
##  4 WHEELER Republican               48.4
##  5 SHERMAN Republican               47.7
##  6 WALLOWA Republican               46.2
##  7 BAKER   Republican               44.6
##  8 CROOK   Republican               43.7
##  9 GILLIAM Republican               42.2
## 10 UNION   Republican               41.9
most_demo_counties
## # A tibble: 10 × 3
## # Groups:   COUNTY [10]
##    COUNTY     PARTY    Percent_Registered
##    <chr>      <chr>                 <dbl>
##  1 MULTNOMAH  Democrat               49.7
##  2 BENTON     Democrat               41.6
##  3 HOOD RIVER Democrat               38.3
##  4 LANE       Democrat               36.3
##  5 WASHINGTON Democrat               36.0
##  6 LINCOLN    Democrat               33.0
##  7 CLACKAMAS  Democrat               32.4
##  8 CLATSOP    Democrat               31.6
##  9 DESCHUTES  Democrat               29.8
## 10 TILLAMOOK  Democrat               28.6
party_change
##           Party Percent_change
## 1      Democrat          -2.89
## 2    Republican          -1.97
## 3   Independent           0.17
## 4         Other          -0.10
## 5 Nonaffiliated           4.80
percent_change_bar