#Title: DWV Assign 7 - Bartels.R
#Author: Jack Reilly
#Purpose: Replicate Figures 1 and 2 from Bartels.  Manage other project files
#Requires: Cumulative ANES; 2024 ANES
#Output: Figures as PDFs (ggplot only)

library(tidyverse)
library(haven)

setwd("~/desktop/working/dwv/bartels2026")

#For this one, we're going to approach things a little differently than we did in the big file
#We're going to create a big dataset
#And create a graphing file
#And then filter down to just the data that we want and run the graphing "command" file

#Step 1. Manage Cumulative NES
anes_full<-read_dta("anes_timeseries_cdf_stata_20220916.dta")
source("DWV Assign 7 - Bartels (Cumulative Management).R")

#Step 2. Manage 2024 NES
source("DWV Assign 7 - Bartels (2024 Management).R")

#Step 3. Merge the two

#Meticulously check things
figure1_cumul
figure1_24
figure1_master <- rbind(figure1_cumul, figure1_24)
head(figure1_master)
tail(figure1_master)

#Meticulously check things
figure2_cumul
figure2_24
figure2_master <- rbind(figure2_cumul, figure2_24)
head(figure2_master)
tail(figure2_master)

#Ed. Note: this took a long time, making everything fit just right!
#Looks clean in code . . . not clean in process

#Step 4. Draw Figure 1
figure1_data <- figure1_master |>  filter(presyear == 1, year<1997)
endyear<-1996
caption_text<-"Presidential Election Years Only, Bartels Years"

source("DWV Assign 7 - Bartels (Figure 1A).r")
bartels1A

source("DWV Assign 7 - Bartels (Figure 1B).r")
bartels1B

# "Patch" the two together
library(patchwork)
bartels1A / bartels1B

#We can do better.
source("DWV Assign 7 - Bartels (Figure 1 Combine).r")
bartels1

ggsave("figures/bartels1_1original.pdf", bartels1, 
       width = 7, height = 10, dpi = 300)

#Step 5. Draw Figure 1, with Expanded Years
figure1_data <- figure1_master |>  filter(presyear == 1)
endyear<-2024
caption_text<-"Presidential Election Years Only, All Years"

source("DWV Assign 7 - Bartels (Figure 1 Combine).r")
bartels1

ggsave("figures/bartels1_2expanded.pdf", bartels1, 
       width = 7, height = 10, dpi = 300)


#Step 6. Draw Figure 1, with Midterms
figure1_data <- figure1_master |>  filter(year<1997)
endyear<-1997
caption_text<-"Midterms Included, Bartels Years"

source("DWV Assign 7 - Bartels (Figure 1 Combine).r")
bartels1

ggsave("figures/bartels1_3midterms.pdf", bartels1, 
       width = 7, height = 10, dpi = 300)

#Step 7. Draw Figure 1, with Midterms and Expanded Years
figure1_data <- figure1_master
endyear<-2024
caption_text<-"Midterms Included, All Years"

source("DWV Assign 7 - Bartels (Figure 1 Combine).r")
bartels1

ggsave("figures/bartels1_4midterms_expanded.pdf", bartels1, 
       width = 7, height = 10, dpi = 300)

#Step 8. Draw Figure 2 - Bartels, Presidential

figure2_data<-figure2_master |>  filter(presyear == 1, year<1997)
endyear<-1997
caption_text<-"Presidential Election Years Only, Bartels Years"

source("DWV Assign 7 - Bartels (Figure 2).r")
bartels2

ggsave("figures/bartels2_1original.pdf", bartels2, 
       width = 7, height = 10, dpi = 300)

#Step 9. Draw Figure 2 - All Years, Presidential
figure2_data<-figure2_master |>  filter(presyear == 1)
endyear<-2024
caption_text<-"Midterms Included, All Years"

source("DWV Assign 7 - Bartels (Figure 2).r")
bartels2

ggsave("figures/bartels2_2expanded.pdf", bartels2, 
       width = 7, height = 10, dpi = 300)

#Step 10. Draw Figure 2 - Bartels Years, Midterms Included
figure2_data<-figure2_master |>  filter(year<1997)
endyear<-1997
caption_text<-"Midterms Included, Bartels Years"

source("DWV Assign 7 - Bartels (Figure 2).r")
bartels2

ggsave("figures/bartels2_3midterms.pdf", bartels2, 
       width = 7, height = 10, dpi = 300)

#Step 10. Draw Figure 2 - All Years, Midterms Included
figure2_data<-figure2_master
endyear<-2024
caption_text<-"Midterms Included, Bartels Years"

source("DWV Assign 7 - Bartels (Figure 2).r")
bartels2

ggsave("figures/bartels2_4midterms_expanded.pdf", bartels2, 
       width = 7, height = 10, dpi = 300)

