# List of packages
packages <- c("tidyr", "ggplot2", "dplyr", "modelsummary", "forcats", "RColorBrewer", 
              "fst", "viridis", "knitr", "rmarkdown", "ggridges", "viridis", "questionr", "flextable") # add any you need here

# Install packages if they aren't installed already
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)

# Load the packages
lapply(packages, library, character.only = TRUE)
## 
## 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
## Loading required package: viridisLite
## [[1]]
## [1] "tidyr"     "stats"     "graphics"  "grDevices" "utils"     "datasets" 
## [7] "methods"   "base"     
## 
## [[2]]
## [1] "ggplot2"   "tidyr"     "stats"     "graphics"  "grDevices" "utils"    
## [7] "datasets"  "methods"   "base"     
## 
## [[3]]
##  [1] "dplyr"     "ggplot2"   "tidyr"     "stats"     "graphics"  "grDevices"
##  [7] "utils"     "datasets"  "methods"   "base"     
## 
## [[4]]
##  [1] "modelsummary" "dplyr"        "ggplot2"      "tidyr"        "stats"       
##  [6] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [11] "base"        
## 
## [[5]]
##  [1] "forcats"      "modelsummary" "dplyr"        "ggplot2"      "tidyr"       
##  [6] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [11] "methods"      "base"        
## 
## [[6]]
##  [1] "RColorBrewer" "forcats"      "modelsummary" "dplyr"        "ggplot2"     
##  [6] "tidyr"        "stats"        "graphics"     "grDevices"    "utils"       
## [11] "datasets"     "methods"      "base"        
## 
## [[7]]
##  [1] "fst"          "RColorBrewer" "forcats"      "modelsummary" "dplyr"       
##  [6] "ggplot2"      "tidyr"        "stats"        "graphics"     "grDevices"   
## [11] "utils"        "datasets"     "methods"      "base"        
## 
## [[8]]
##  [1] "viridis"      "viridisLite"  "fst"          "RColorBrewer" "forcats"     
##  [6] "modelsummary" "dplyr"        "ggplot2"      "tidyr"        "stats"       
## [11] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [16] "base"        
## 
## [[9]]
##  [1] "knitr"        "viridis"      "viridisLite"  "fst"          "RColorBrewer"
##  [6] "forcats"      "modelsummary" "dplyr"        "ggplot2"      "tidyr"       
## [11] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [16] "methods"      "base"        
## 
## [[10]]
##  [1] "rmarkdown"    "knitr"        "viridis"      "viridisLite"  "fst"         
##  [6] "RColorBrewer" "forcats"      "modelsummary" "dplyr"        "ggplot2"     
## [11] "tidyr"        "stats"        "graphics"     "grDevices"    "utils"       
## [16] "datasets"     "methods"      "base"        
## 
## [[11]]
##  [1] "ggridges"     "rmarkdown"    "knitr"        "viridis"      "viridisLite" 
##  [6] "fst"          "RColorBrewer" "forcats"      "modelsummary" "dplyr"       
## [11] "ggplot2"      "tidyr"        "stats"        "graphics"     "grDevices"   
## [16] "utils"        "datasets"     "methods"      "base"        
## 
## [[12]]
##  [1] "ggridges"     "rmarkdown"    "knitr"        "viridis"      "viridisLite" 
##  [6] "fst"          "RColorBrewer" "forcats"      "modelsummary" "dplyr"       
## [11] "ggplot2"      "tidyr"        "stats"        "graphics"     "grDevices"   
## [16] "utils"        "datasets"     "methods"      "base"        
## 
## [[13]]
##  [1] "questionr"    "ggridges"     "rmarkdown"    "knitr"        "viridis"     
##  [6] "viridisLite"  "fst"          "RColorBrewer" "forcats"      "modelsummary"
## [11] "dplyr"        "ggplot2"      "tidyr"        "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[14]]
##  [1] "flextable"    "questionr"    "ggridges"     "rmarkdown"    "knitr"       
##  [6] "viridis"      "viridisLite"  "fst"          "RColorBrewer" "forcats"     
## [11] "modelsummary" "dplyr"        "ggplot2"      "tidyr"        "stats"       
## [16] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [21] "base"
ess <- read_fst("All-ESS-Data.fst")
table(ess$essround)
## 
##     1     2     3     4     5     6     7     8     9    10 
## 42359 47537 43000 56752 52458 54673 40185 44387 49519 59685
ess$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
  ess$year[ess$essround == i] <- replacements[i]
}

table(ess$year)
## 
##  2002  2004  2006  2008  2010  2012  2014  2016  2018  2020 
## 42359 47537 43000 56752 52458 54673 40185 44387 49519 59685
uk_data <- read_fst("uk_data.fst")
uk_data_clean <- uk_data %>%
  select(yrbrn, vote, stfjb, essround, year)  %>%
  mutate(
     yrbrn = ifelse(yrbrn %in% c(7777, 8888, 9999), NA, yrbrn), 
     vote = ifelse(vote == 2, 0, ifelse(vote %in% c(3, 7, 8, 9), NA, vote))
  )
  
table(uk_data$vote)
## 
##     1     2     3     7     8 
## 14576  5281  1058     9    55
table(uk_data_clean$vote)
## 
##     0     1 
##  5281 14576
table(uk_data_clean$vote_binary)
## < table of extent 0 >
table(uk_data_clean$vote_yes)
## < table of extent 0 >
vote_by_year <- uk_data_clean %>%
  group_by(year) %>%
 summarize(mean_vote = mean(vote, na.rm = TRUE))
vote_by_year
## # A tibble: 10 × 2
##     year mean_vote
##    <dbl>     <dbl>
##  1  2002     0.726
##  2  2004     0.690
##  3  2006     0.722
##  4  2008     0.708
##  5  2010     0.720
##  6  2012     0.717
##  7  2014     0.715
##  8  2016     0.774
##  9  2018     0.799
## 10  2020     0.806
ggplot(vote_by_year, aes(x = year, y = mean_vote)) +
  geom_line(color = "blue", size = 1) +  # Line to show the trend
  geom_point(color = "red", size = 3) +  # Points to highlight each year's value
  labs(title = "Likelihood to Vote in the UK (2002-2020)", 
       x = "Year", 
       y = "Mean Vote (No-Yes scale)") +
  ylim(0, 1) + 
  theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

#ess$vote[ess$vote %in% c(3, 7, 8, 9)] <- NA
ess_data_clean <- ess %>%
  select(yrbrn, vote, essround, year)  %>%
  mutate(
     vote = ifelse(vote == 2, 0, ifelse(vote %in% c(3, 7, 8, 9), NA, vote))
  )

avg_vote_uk <- uk_data_clean %>%
  group_by(year) %>%
  summarize(mean_vote_uk = mean(vote, na.rm = TRUE))

# Compute the average trust for each year for all countries in the ess dataset.
avg_vote_all_countries <- ess_data_clean %>%
  group_by(year) %>%
  summarize(mean_vote_all = mean(vote, na.rm = TRUE))
avg_vote_all_countries
## # A tibble: 10 × 2
##     year mean_vote_all
##    <dbl>         <dbl>
##  1  2002         0.802
##  2  2004         0.770
##  3  2006         0.779
##  4  2008         0.776
##  5  2010         0.763
##  6  2012         0.760
##  7  2014         0.766
##  8  2016         0.766
##  9  2018         0.785
## 10  2020         0.817
# Combine the data
# 'left_join' is a function to merge two datasets side by side. We want to merge 'avg_trust_france' and 'avg_trust_all_countries'.
# The merge is based on the "year" column, which means rows with matching years in both datasets will be combined.
combined_data <- left_join(avg_vote_uk, avg_vote_all_countries, by = "year")

# Reshape to long format
# The next set of commands are used to reshape data. 
# This is often done to make data more suitable for certain types of analysis or visualization.

# 'pivot_longer' is a function that makes a dataset longer by turning columns into rows.
long_data <- combined_data %>%

  # 'pivot_longer' function is used to convert data from a wide format to a long format.
  # This is useful for making certain types of graphs or conducting specific analyses.
  pivot_longer(
    cols = starts_with("mean_vote"),  # We choose columns that start with "mean_trust" to pivot/reshape.
    
    # 'names_to' specifies the name of the new column that will hold the original column names.
    names_to = "Vote_Type",  
    
    # 'values_to' specifies the name of the new column that will hold the values from the original columns.
    values_to = "Mean_Vote"  
  )
# Plot using ggplot2
ggplot(long_data) +
  geom_line(aes(x = year, y = Mean_Vote, color = Vote_Type), size = 1) +
  geom_point(aes(x = year, y = Mean_Vote, color = Vote_Type), size = 3) +
  labs(title = "Likelihood to Vote in the UK vs Europe (2002-2020)", 
       x = "Year", 
       y = "Mean Vote (No-Yes scale)") +
  ylim(0, 1) +
  scale_color_manual(values = c("blue", "red"), 
                     name = "Dataset", 
                     labels = c("UK", "All ESS"), 
                     breaks = c("mean_vote_uk", "mean_vote_all")) + 
  theme_minimal()