knitr::opts_chunk$set(echo = TRUE)
# List of packages
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer", 
              "fst", "viridis", "knitr", "rmarkdown", "ggridges", "viridis", "questionr") # 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 core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## `modelsummary` has built-in support to draw text-only (markdown) tables.
##   To generate tables in other formats, you must install one or more of
##   these libraries:
##   
## install.packages(c(
##     "kableExtra",
##     "gt",
##     "flextable",
##    
##   "huxtable",
##     "DT"
## ))
## 
##   Alternatively, you can set markdown as the default table format to
##   silence this alert:
##   
## config_modelsummary(factory_default = "markdown")
## 
## 
## Loading required package: viridisLite
## [[1]]
##  [1] "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
##  [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics" 
## [13] "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[2]]
##  [1] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
##  [6] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [11] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [16] "datasets"     "methods"      "base"        
## 
## [[3]]
##  [1] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
##  [6] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [11] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [16] "datasets"     "methods"      "base"        
## 
## [[4]]
##  [1] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
##  [6] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [11] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [16] "utils"        "datasets"     "methods"      "base"        
## 
## [[5]]
##  [1] "fst"          "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
##  [6] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [11] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[6]]
##  [1] "viridis"      "viridisLite"  "fst"          "RColorBrewer" "modelsummary"
##  [6] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [11] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [16] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [21] "methods"      "base"        
## 
## [[7]]
##  [1] "knitr"        "viridis"      "viridisLite"  "fst"          "RColorBrewer"
##  [6] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
## [11] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [16] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [21] "datasets"     "methods"      "base"        
## 
## [[8]]
##  [1] "rmarkdown"    "knitr"        "viridis"      "viridisLite"  "fst"         
##  [6] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
## [11] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [16] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [21] "utils"        "datasets"     "methods"      "base"        
## 
## [[9]]
##  [1] "ggridges"     "rmarkdown"    "knitr"        "viridis"      "viridisLite" 
##  [6] "fst"          "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
## [11] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [16] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [21] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[10]]
##  [1] "ggridges"     "rmarkdown"    "knitr"        "viridis"      "viridisLite" 
##  [6] "fst"          "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
## [11] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [16] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [21] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[11]]
##  [1] "questionr"    "ggridges"     "rmarkdown"    "knitr"        "viridis"     
##  [6] "viridisLite"  "fst"          "RColorBrewer" "modelsummary" "lubridate"   
## [11] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [16] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [21] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [26] "base"
ess <- read_fst("All-ESS-Data.fst")
print("hell yeah this works!") # testrun
## [1] "hell yeah this works!"
#Mission 1
france_data <- ess %>% # Start with the original 'ess' dataset
  filter(cntry == "FR") %>% # Filter the data to only include rows where country (cntry) is France ("FR")
  select(cntry, clsprty, stfdem, trstplt, imueclt, imdfetn, imptrad, ppltrst, polintr, stfeco, yrbrn,
         edulvla, edulvlb, domicil, essround, pdwrk, brncntr, rlgblg)%>% # We select only some specified variables that we will be using throughout the tutorial
  
  # The mutate function is used to modify or create new columns
  mutate(
    clsprty = ifelse(clsprty == 2, 0, ifelse(clsprty %in% c(7, 8, 9), NA, clsprty)), # If 'clsprty' is 2, set it to 0. If it's 7, 8, or 9, set it to NA.
    stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem), # For 'stfdem', set values 77, 88, and 99 to NA.
    trstplt = ifelse(trstplt %in% c(77, 88, 99), NA, trstplt), # For 'trstplt', set values 77, 88, and 99 to NA.
    imueclt = ifelse(imueclt %in% c(77, 88, 99), NA, imueclt), # For 'imueclt', set values 77, 88, and 99 to NA.
    imdfetn = case_when( # For 'imdfetn', perform multiple condition checks using 'case_when'.
      imdfetn %in% c(4, 3) ~ 0,  # Set values 4 and 3 to 0.
      imdfetn %in% c(2, 1) ~ 1,  # Set values 2 and 1 to 1.
      imdfetn %in% c(7, 8, 9) ~ NA_real_,  # Set values 7, 8, and 9 to NA.
      TRUE ~ imdfetn # If none of the above conditions apply, keep the original value.
    ),
    imptrad = ifelse(imptrad %in% c(7, 8, 9), NA, imptrad), # For 'imptrad', set values 7, 8, and 9 to NA.
    ppltrst = ifelse(ppltrst %in% c(77, 88, 99), NA, ppltrst), # For 'ppltrst', set values 77, 88, and 99 to NA.
    polintr = ifelse(polintr %in% c(7, 8, 9), NA, polintr), # For 'polintr', set values 7, 8, and 9 to NA.
    stfeco = ifelse(stfeco %in% c(77, 88, 99), NA, stfeco)  # For 'stfeco', set values 77, 88, and 99 to NA.
  )

table(france_data$yrbrn)
## 
## 1905 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 
##    1    2    1    2    5    2    8    9    5    8   12   16   41   40   67   56 
## 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 
##   64   70   90   96  115  126  144  180  163  185  169  169  206  201  195  179 
## 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 
##  199  211  238  235  243  246  307  368  341  348  351  311  358  337  352  284 
## 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 
##  355  343  309  323  323  338  339  347  317  333  337  337  340  345  346  358 
## 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 
##  321  317  291  315  287  303  311  280  311  278  298  217  245  211  229  179 
## 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 
##  222  206  140  174  153  137   99  125   84   89   97   78   79   59   38   45 
## 2004 2005 7777 8888 
##   41   26    6    1
table(france_data$stfdem)
## 
##    0    1    2    3    4    5    6    7    8    9   10 
## 1374  779 1703 2228 2357 3563 2125 2298 1669  398  267
#Mission 2
france_data <- ess %>% 
  filter(cntry == "FR") %>% 
  select(cntry, clsprty, stfdem, trstplt, imueclt, imdfetn, imptrad, ppltrst, polintr, yrbrn,
         edulvla, edulvlb, domicil, essround, pdwrk, brncntr, rlgblg, crpdwk)%>% # Additional Variable - Control paid work last 7 days
  mutate(
    clsprty = ifelse(clsprty == 2, 0, ifelse(clsprty %in% c(7, 8, 9), NA, clsprty)), # If 'clsprty' is 2, set it to 0. If it's 7, 8, or 9, set it to NA.
    stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem), # For 'stfdem', set values 77, 88, and 99 to NA.
    trstplt = ifelse(trstplt %in% c(77, 88, 99), NA, trstplt), # For 'trstplt', set values 77, 88, and 99 to NA.
    imueclt = ifelse(imueclt %in% c(77, 88, 99), NA, imueclt), # For 'imueclt', set values 77, 88, and 99 to NA.
    imdfetn = case_when( # For 'imdfetn', perform multiple condition checks using 'case_when'.
      imdfetn %in% c(4, 3) ~ 0,  # Set values 4 and 3 to 0.
      imdfetn %in% c(2, 1) ~ 1,  # Set values 2 and 1 to 1.
      imdfetn %in% c(7, 8, 9) ~ NA_real_,  # Set values 7, 8, and 9 to NA.
      TRUE ~ imdfetn # If none of the above conditions apply, keep the original value.
    ),
    imptrad = ifelse(imptrad %in% c(7, 8, 9), NA, imptrad), # For 'imptrad', set values 7, 8, and 9 to NA.
    ppltrst = ifelse(ppltrst %in% c(77, 88, 99), NA, ppltrst), # For 'ppltrst', set values 77, 88, and 99 to NA.
    polintr = ifelse(polintr %in% c(7, 8, 9), NA, polintr), # For 'polintr', set values 7, 8, and 9 to NA.
    crpdwk = ifelse(crpdwk %in% c(6, 7, 8, 9), NA, crpdwk), # For 'crpdwk', set values 6, 7, 8, and 9 to NA.
  )
table(france_data$crpdwk)
## 
##    1    2 
##  232 9225
#Mission 3
france_clean <- france_data %>%
  select(cntry, clsprty, stfdem, trstplt, imueclt, imdfetn, imptrad, ppltrst, polintr, yrbrn,
         edulvla, edulvlb, domicil, essround, pdwrk, brncntr, rlgblg, crpdwk)%>% #Includes the additional variable from Mission 2 above
  mutate(
    clsprty = ifelse(clsprty == 2, 0, ifelse(clsprty %in% c(7, 8, 9), NA, clsprty)), # If 'clsprty' is 2, set it to 0. If it's 7, 8, or 9, set it to NA.
    stfdem = ifelse(stfdem %in% c(77, 88, 99), NA, stfdem), # For 'stfdem', set values 77, 88, and 99 to NA.
    trstplt = ifelse(trstplt %in% c(77, 88, 99), NA, trstplt), # For 'trstplt', set values 77, 88, and 99 to NA.
    imueclt = ifelse(imueclt %in% c(77, 88, 99), NA, imueclt), # For 'imueclt', set values 77, 88, and 99 to NA.
    imdfetn = case_when( # For 'imdfetn', perform multiple condition checks using 'case_when'.
      imdfetn %in% c(4, 3) ~ 0,  # Set values 4 and 3 to 0.
      imdfetn %in% c(2, 1) ~ 1,  # Set values 2 and 1 to 1.
      imdfetn %in% c(7, 8, 9) ~ NA_real_,  # Set values 7, 8, and 9 to NA.
      TRUE ~ imdfetn # If none of the above conditions apply, keep the original value.
    ),
    imptrad = ifelse(imptrad %in% c(7, 8, 9), NA, imptrad), # For 'imptrad', set values 7, 8, and 9 to NA.
    ppltrst = ifelse(ppltrst %in% c(77, 88, 99), NA, ppltrst), # For 'ppltrst', set values 77, 88, and 99 to NA.
    polintr = ifelse(polintr %in% c(7, 8, 9), NA, polintr), # For 'polintr', set values 7, 8, and 9 to NA.
    crpdwk = ifelse(crpdwk %in% c(6, 7, 8, 9), NA, crpdwk), # For 'crpdwk', set values 6, 7, 8, and 9 to NA.
    yrbrn = ifelse(yrbrn %in% c(7777, 8888, 999), NA, yrbrn), #For 'yrbrn', set values 7777, 8888, 9999 to NA
  )
table(france_clean$crpdwk)
## 
##    1    2 
##  232 9225
table(france_clean$yrbrn)
## 
## 1905 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 
##    1    2    1    2    5    2    8    9    5    8   12   16   41   40   67   56 
## 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 
##   64   70   90   96  115  126  144  180  163  185  169  169  206  201  195  179 
## 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 
##  199  211  238  235  243  246  307  368  341  348  351  311  358  337  352  284 
## 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 
##  355  343  309  323  323  338  339  347  317  333  337  337  340  345  346  358 
## 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 
##  321  317  291  315  287  303  311  280  311  278  298  217  245  211  229  179 
## 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 
##  222  206  140  174  153  137   99  125   84   89   97   78   79   59   38   45 
## 2004 2005 
##   41   26
table(france_clean$stfdem)
## 
##    0    1    2    3    4    5    6    7    8    9   10 
## 1374  779 1703 2228 2357 3563 2125 2298 1669  398  267

datasummary_skim(dataset %>% select(v1, v2, v3))

#Mission 4
 datasummary_skim(france_clean %>% select(crpdwk, yrbrn, stfdem))
## Warning: The histogram argument is only supported for (a) output types "default",
##   "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
##   ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
##   compiled to PDF (via kableExtra)  or HTML (via kableExtra or gt). Use
##   `histogram=FALSE` to silence this warning.
Unique (#) Missing (%) Mean SD Min Median Max
crpdwk 3 50 2.0 0.2 1.0 2.0 2.0
yrbrn 99 0 1961.9 19.1 1905.0 1962.0 2005.0
stfdem 12 1 4.6 2.4 0.0 5.0 10.0
#Mission 5
library(ggridges)
library(ggplot2)

ggplot(france_data, aes(x = crpdwk, y = yrbrn, fill = factor(crpdwk))) +
  geom_boxplot() +
  labs(title = "Control paid work in France across Ages",
       x = "Control paid work",
       y = "Age") +
  ylim(1905, 2005)
## Warning: Removed 9581 rows containing missing values (`stat_boxplot()`).
## Warning: Removed 6 rows containing non-finite values (`stat_boxplot()`).

  theme_minimal()
## List of 97
##  $ line                      :List of 6
##   ..$ colour       : chr "black"
##   ..$ linewidth    : num 0.5
##   ..$ linetype     : num 1
##   ..$ lineend      : chr "butt"
##   ..$ arrow        : logi FALSE
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_line" "element"
##  $ rect                      :List of 5
##   ..$ fill         : chr "white"
##   ..$ colour       : chr "black"
##   ..$ linewidth    : num 0.5
##   ..$ linetype     : num 1
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_rect" "element"
##  $ text                      :List of 11
##   ..$ family       : chr ""
##   ..$ face         : chr "plain"
##   ..$ colour       : chr "black"
##   ..$ size         : num 11
##   ..$ hjust        : num 0.5
##   ..$ vjust        : num 0.5
##   ..$ angle        : num 0
##   ..$ lineheight   : num 0.9
##   ..$ margin       : 'margin' num [1:4] 0points 0points 0points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : logi FALSE
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ title                     : NULL
##  $ aspect.ratio              : NULL
##  $ axis.title                : NULL
##  $ axis.title.x              :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : NULL
##   ..$ vjust        : num 1
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 2.75points 0points 0points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.title.x.top          :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : NULL
##   ..$ vjust        : num 0
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 0points 2.75points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.title.x.bottom       : NULL
##  $ axis.title.y              :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : NULL
##   ..$ vjust        : num 1
##   ..$ angle        : num 90
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 2.75points 0points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.title.y.left         : NULL
##  $ axis.title.y.right        :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : NULL
##   ..$ vjust        : num 0
##   ..$ angle        : num -90
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 0points 0points 2.75points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.text                 :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : chr "grey30"
##   ..$ size         : 'rel' num 0.8
##   ..$ hjust        : NULL
##   ..$ vjust        : NULL
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : NULL
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.text.x               :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : NULL
##   ..$ vjust        : num 1
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 2.2points 0points 0points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.text.x.top           :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : NULL
##   ..$ vjust        : num 0
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 0points 2.2points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.text.x.bottom        : NULL
##  $ axis.text.y               :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : num 1
##   ..$ vjust        : NULL
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 2.2points 0points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.text.y.left          : NULL
##  $ axis.text.y.right         :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : num 0
##   ..$ vjust        : NULL
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 0points 0points 2.2points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.ticks                : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ axis.ticks.x              : NULL
##  $ axis.ticks.x.top          : NULL
##  $ axis.ticks.x.bottom       : NULL
##  $ axis.ticks.y              : NULL
##  $ axis.ticks.y.left         : NULL
##  $ axis.ticks.y.right        : NULL
##  $ axis.ticks.length         : 'simpleUnit' num 2.75points
##   ..- attr(*, "unit")= int 8
##  $ axis.ticks.length.x       : NULL
##  $ axis.ticks.length.x.top   : NULL
##  $ axis.ticks.length.x.bottom: NULL
##  $ axis.ticks.length.y       : NULL
##  $ axis.ticks.length.y.left  : NULL
##  $ axis.ticks.length.y.right : NULL
##  $ axis.line                 : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ axis.line.x               : NULL
##  $ axis.line.x.top           : NULL
##  $ axis.line.x.bottom        : NULL
##  $ axis.line.y               : NULL
##  $ axis.line.y.left          : NULL
##  $ axis.line.y.right         : NULL
##  $ legend.background         : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ legend.margin             : 'margin' num [1:4] 5.5points 5.5points 5.5points 5.5points
##   ..- attr(*, "unit")= int 8
##  $ legend.spacing            : 'simpleUnit' num 11points
##   ..- attr(*, "unit")= int 8
##  $ legend.spacing.x          : NULL
##  $ legend.spacing.y          : NULL
##  $ legend.key                : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ legend.key.size           : 'simpleUnit' num 1.2lines
##   ..- attr(*, "unit")= int 3
##  $ legend.key.height         : NULL
##  $ legend.key.width          : NULL
##  $ legend.text               :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : 'rel' num 0.8
##   ..$ hjust        : NULL
##   ..$ vjust        : NULL
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : NULL
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ legend.text.align         : NULL
##  $ legend.title              :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : num 0
##   ..$ vjust        : NULL
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : NULL
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ legend.title.align        : NULL
##  $ legend.position           : chr "right"
##  $ legend.direction          : NULL
##  $ legend.justification      : chr "center"
##  $ legend.box                : NULL
##  $ legend.box.just           : NULL
##  $ legend.box.margin         : 'margin' num [1:4] 0cm 0cm 0cm 0cm
##   ..- attr(*, "unit")= int 1
##  $ legend.box.background     : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ legend.box.spacing        : 'simpleUnit' num 11points
##   ..- attr(*, "unit")= int 8
##  $ panel.background          : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ panel.border              : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ panel.spacing             : 'simpleUnit' num 5.5points
##   ..- attr(*, "unit")= int 8
##  $ panel.spacing.x           : NULL
##  $ panel.spacing.y           : NULL
##  $ panel.grid                :List of 6
##   ..$ colour       : chr "grey92"
##   ..$ linewidth    : NULL
##   ..$ linetype     : NULL
##   ..$ lineend      : NULL
##   ..$ arrow        : logi FALSE
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_line" "element"
##  $ panel.grid.major          : NULL
##  $ panel.grid.minor          :List of 6
##   ..$ colour       : NULL
##   ..$ linewidth    : 'rel' num 0.5
##   ..$ linetype     : NULL
##   ..$ lineend      : NULL
##   ..$ arrow        : logi FALSE
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_line" "element"
##  $ panel.grid.major.x        : NULL
##  $ panel.grid.major.y        : NULL
##  $ panel.grid.minor.x        : NULL
##  $ panel.grid.minor.y        : NULL
##  $ panel.ontop               : logi FALSE
##  $ plot.background           : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ plot.title                :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : 'rel' num 1.2
##   ..$ hjust        : num 0
##   ..$ vjust        : num 1
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 0points 5.5points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ plot.title.position       : chr "panel"
##  $ plot.subtitle             :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : num 0
##   ..$ vjust        : num 1
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 0points 5.5points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ plot.caption              :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : 'rel' num 0.8
##   ..$ hjust        : num 1
##   ..$ vjust        : num 1
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 5.5points 0points 0points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ plot.caption.position     : chr "panel"
##  $ plot.tag                  :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : 'rel' num 1.2
##   ..$ hjust        : num 0.5
##   ..$ vjust        : num 0.5
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : NULL
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ plot.tag.position         : chr "topleft"
##  $ plot.margin               : 'margin' num [1:4] 5.5points 5.5points 5.5points 5.5points
##   ..- attr(*, "unit")= int 8
##  $ strip.background          : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ strip.background.x        : NULL
##  $ strip.background.y        : NULL
##  $ strip.clip                : chr "inherit"
##  $ strip.placement           : chr "inside"
##  $ strip.text                :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : chr "grey10"
##   ..$ size         : 'rel' num 0.8
##   ..$ hjust        : NULL
##   ..$ vjust        : NULL
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 4.4points 4.4points 4.4points 4.4points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ strip.text.x              : NULL
##  $ strip.text.x.bottom       : NULL
##  $ strip.text.x.top          : NULL
##  $ strip.text.y              :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : NULL
##   ..$ vjust        : NULL
##   ..$ angle        : num -90
##   ..$ lineheight   : NULL
##   ..$ margin       : NULL
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ strip.text.y.left         :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : NULL
##   ..$ vjust        : NULL
##   ..$ angle        : num 90
##   ..$ lineheight   : NULL
##   ..$ margin       : NULL
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ strip.text.y.right        : NULL
##  $ strip.switch.pad.grid     : 'simpleUnit' num 2.75points
##   ..- attr(*, "unit")= int 8
##  $ strip.switch.pad.wrap     : 'simpleUnit' num 2.75points
##   ..- attr(*, "unit")= int 8
##  - attr(*, "class")= chr [1:2] "theme" "gg"
##  - attr(*, "complete")= logi TRUE
##  - attr(*, "validate")= logi TRUE
#Mission 6
table(france_data$crpdwk, france_data$yrbrn) %>%
 cprop()
##        
##         1905  1909  1910  1911  1912  1913  1914  1915  1916  1917  1918  1919 
##   1       0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
##   2     100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
##   Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
##        
##         1920  1921  1922  1923  1924  1925  1926  1927  1928  1929  1930  1931 
##   1       0.0   0.0   0.0   0.0   0.0   0.0   1.1   2.1   0.0   0.0   0.7   1.7
##   2     100.0 100.0 100.0 100.0 100.0 100.0  98.9  97.9 100.0 100.0  99.3  98.3
##   Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
##        
##         1932  1933  1934  1935  1936  1937  1938  1939  1940  1941  1942  1943 
##   1       1.2   1.1   0.6   0.0   1.0   0.5   1.1   0.6   0.0   1.0   2.7   0.9
##   2      98.8  98.9  99.4 100.0  99.0  99.5  98.9  99.4 100.0  99.0  97.3  99.1
##   Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
##        
##         1944  1945  1946  1947  1948  1949  1950  1951  1952  1953  1954  1955 
##   1       0.4   0.9   0.8   2.4   3.1   2.4   4.1   3.7   1.3   1.7   2.9   2.4
##   2      99.6  99.1  99.2  97.6  96.9  97.6  95.9  96.3  98.7  98.3  97.1  97.6
##   Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
##        
##         1956  1957  1958  1959  1960  1961  1962  1963  1964  1965  1966  1967 
##   1       1.3   3.1   5.2   0.0   1.3   2.9   0.0   1.6   1.7   0.0   3.8   3.1
##   2      98.7  96.9  94.8 100.0  98.7  97.1 100.0  98.4  98.3 100.0  96.2  96.9
##   Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
##        
##         1968  1969  1970  1971  1972  1973  1974  1975  1976  1977  1978  1979 
##   1       5.2   4.3   3.3   4.7   1.6   3.6   2.0   7.5   1.7   3.6   0.0   6.5
##   2      94.8  95.7  96.7  95.3  98.4  96.4  98.0  92.5  98.3  96.4 100.0  93.5
##   Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
##        
##         1980  1981  1982  1983  1984  1985  1986  1987  1988  1989  1990  1991 
##   1       4.4   6.1   3.4   4.5   2.4   6.8   6.7   1.5   6.5   5.5   8.1   6.7
##   2      95.6  93.9  96.6  95.5  97.6  93.2  93.3  98.5  93.5  94.5  91.9  93.3
##   Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
##        
##         1992  1993  1994  1995  1996  1997  1998  1999  2000  2001  2002  2003 
##   1      10.1   2.4   5.3   7.7   7.4   4.8   5.9  12.5   8.3   6.8   5.9   0.0
##   2      89.9  97.6  94.7  92.3  92.6  95.2  94.1  87.5  91.7  93.2  94.1 100.0
##   Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
##        
##         2004  2005  7777  8888  All  
##   1       7.7   0.0   0.0   0.0   2.5
##   2      92.3 100.0 100.0 100.0  97.5
##   Total 100.0 100.0 100.0 100.0 100.0
#Mission 7
crpdyr <- datasummary_crosstab(crpdwk ~ yrbrn, data = france_data)
crpdyr
crpdwk 1905 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 7777 8888 All
1 N 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 0 0 1 3 2 2 1 0 2 1 2 1 0 2 6 2 1 2 2 7 8 6 10 7 3 3 5 3 2 4 5 0 1 2 0 1 1 0 2 2 3 2 2 3 1 2 1 4 1 2 0 4 3 4 3 3 2 5 6 1 7 5 5 6 9 2 3 6 4 3 4 8 5 3 2 0 3 0 0 0 232
% row 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.4 0.9 0.0 0.0 0.4 1.3 0.9 0.9 0.4 0.0 0.9 0.4 0.9 0.4 0.0 0.9 2.6 0.9 0.4 0.9 0.9 3.0 3.4 2.6 4.3 3.0 1.3 1.3 2.2 1.3 0.9 1.7 2.2 0.0 0.4 0.9 0.0 0.4 0.4 0.0 0.9 0.9 1.3 0.9 0.9 1.3 0.4 0.9 0.4 1.7 0.4 0.9 0.0 1.7 1.3 1.7 1.3 1.3 0.9 2.2 2.6 0.4 3.0 2.2 2.2 2.6 3.9 0.9 1.3 2.6 1.7 1.3 1.7 3.4 2.2 1.3 0.9 0.0 1.3 0.0 0.0 0.0 100.0
2 N 1 2 1 2 5 2 8 9 5 8 12 16 40 40 66 55 64 70 89 92 115 124 143 176 161 183 167 167 200 197 188 173 191 202 220 213 222 209 264 284 252 247 231 183 222 172 166 120 147 124 91 82 75 67 63 60 58 57 51 63 55 44 58 61 60 53 50 49 57 54 52 58 65 62 84 64 83 68 84 65 100 86 57 84 80 82 54 72 50 60 64 56 55 41 32 41 36 26 5 1 9225
% row 0.0 0.0 0.0 0.0 0.1 0.0 0.1 0.1 0.1 0.1 0.1 0.2 0.4 0.4 0.7 0.6 0.7 0.8 1.0 1.0 1.2 1.3 1.6 1.9 1.7 2.0 1.8 1.8 2.2 2.1 2.0 1.9 2.1 2.2 2.4 2.3 2.4 2.3 2.9 3.1 2.7 2.7 2.5 2.0 2.4 1.9 1.8 1.3 1.6 1.3 1.0 0.9 0.8 0.7 0.7 0.7 0.6 0.6 0.6 0.7 0.6 0.5 0.6 0.7 0.7 0.6 0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.9 0.7 0.9 0.7 0.9 0.7 1.1 0.9 0.6 0.9 0.9 0.9 0.6 0.8 0.5 0.7 0.7 0.6 0.6 0.4 0.3 0.4 0.4 0.3 0.1 0.0 100.0
All N 1 2 1 2 5 2 8 9 5 8 12 16 41 40 67 56 64 70 90 96 115 126 144 180 163 185 169 169 206 201 195 179 199 211 238 235 243 246 307 368 341 348 351 311 358 337 352 284 355 343 309 323 323 338 339 347 317 333 337 337 340 345 346 358 321 317 291 315 287 303 311 280 311 278 298 217 245 211 229 179 222 206 140 174 153 137 99 125 84 89 97 78 79 59 38 45 41 26 6 1 19038
% row 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.1 0.2 0.2 0.4 0.3 0.3 0.4 0.5 0.5 0.6 0.7 0.8 0.9 0.9 1.0 0.9 0.9 1.1 1.1 1.0 0.9 1.0 1.1 1.3 1.2 1.3 1.3 1.6 1.9 1.8 1.8 1.8 1.6 1.9 1.8 1.8 1.5 1.9 1.8 1.6 1.7 1.7 1.8 1.8 1.8 1.7 1.7 1.8 1.8 1.8 1.8 1.8 1.9 1.7 1.7 1.5 1.7 1.5 1.6 1.6 1.5 1.6 1.5 1.6 1.1 1.3 1.1 1.2 0.9 1.2 1.1 0.7 0.9 0.8 0.7 0.5 0.7 0.4 0.5 0.5 0.4 0.4 0.3 0.2 0.2 0.2 0.1 0.0 0.0 100.0
#Mission 8
library(ggplot2)
library(dplyr)
library(tidyr)
         
france_data <- france_data %>%
  mutate(crpdwk_recode = ifelse(crpdwk == 1, "no", "yes"))

contingency_table <- table(france_data$crpdwk, france_data$yrbrn)
conditional_proportions <- as.data.frame(prop.table(contingency_table, margin = 2))

ggplot(france_data, aes(x = as.integer(as.character(yrbrn)), y = crpdwk, color = crpdwk)) +
  geom_line() +
  xlim(1905, 2005) +
  labs(title = "Control Paid Work by Birth Year",
    x = "Birth Year", y = "Control Paid Work",
    color = "Control Paid Work")
## Warning: Removed 7 rows containing missing values (`geom_line()`).

#Mission 9
library(ggplot2)
ggplot(france_data, aes(x = factor(edulvla), y = yrbrn, fill = factor(edulvla))) +
  geom_boxplot() +
  labs(
    title = "Highest Level of Education for across Ages",
    x = "Highest Level of Education",
    y = "Age"
  ) +
  scale_y_continuous(limits = c(1905, 2005)) +  
  theme_minimal()
## Warning: Removed 7 rows containing non-finite values (`stat_boxplot()`).

#Mission 10
library(dplyr)
library(ggplot2)

france_data <- france_data %>%
  mutate(yrbrn = ifelse(yrbrn %in% c(7777, 8888, 999), NA, yrbrn))

france_clean <- france_data %>%
filter(!is.na(yrbrn) & !is.na(trstplt))

france_probs <- france_data %>%
 count(trstplt, yrbrn) %>%
group_by(yrbrn) %>%
 mutate(prob = n / sum(n))

ggplot(france_probs, aes(x = as.factor(trstplt), y = prob, color = yrbrn)) +
 geom_point() +
geom_line(aes(group = yrbrn)) +
 labs(title = "Conditional Probabilities of Trust in Politicians",
   subtitle = "Year Born",
     x = "Trust Scale", 
     y = "Probability") +
  theme_minimal()

#Mission 11
france_data %>%
   mutate(yrbrn = case_when(
    yrbrn %in% c(7777, 8888) ~ NA_integer_,
    TRUE ~ as.integer(yrbrn)
  )) %>% 
  group_by(yrbrn) %>%
  summarise(avg_trstplt = mean(trstplt, na.rm = TRUE)) %>%
  ggplot(aes(x = yrbrn, y = avg_trstplt)) +
  geom_line() +
  labs(title = "Average Trust in Politicians by Year of Birth", 
       x = "Year of Birth", y = "Average Satisfaction with Democracy (0-10)") +
  theme_minimal()
## Warning: Removed 1 row containing missing values (`geom_line()`).

#Mission 12
france_data %>%
   mutate(yrbrn = case_when(
    yrbrn %in% c(7777, 8888) ~ NA_integer_,
    TRUE ~ as.integer(yrbrn)
  )) %>% 
  group_by(yrbrn) %>%
  summarise(avg_trstplt = mean(trstplt, na.rm = TRUE)) %>%
  ggplot(aes(x = yrbrn, y = avg_trstplt)) +
  geom_line() +
  labs(title = "Average Trust in Politicians by Year of Birth", 
       x = "Year of Birth", y = "Average Trust in Politicians (0-10)") +
  ylim (3,5)+
  theme_minimal()
## Warning: Removed 2 rows containing missing values (`geom_line()`).

#Mission 13 
library(dplyr)
library(tidyr)
library(ggplot2)

france_data <- france_data %>%
  mutate(trstplt = ifelse(trstplt%in% c(77, 88, 99), NA, trstplt)) 

avg_trust_france <- france_data %>%
  group_by(yrbrn) %>%
  summarize(mean_trust_france = mean(trstplt, na.rm = TRUE))

avg_trust_all_countries <- ess %>%
  group_by(yrbrn) %>%
  summarize(mean_trust_all = mean(trstplt, na.rm = TRUE))

combined_data <- left_join(avg_trust_france, avg_trust_all_countries, by = "yrbrn")

long_data <- combined_data %>%
  pivot_longer(
    cols = starts_with ("mean_trust"),
    names_to = "Trust_Type",
    values_to = "Mean_Trust"
  )

ggplot(long_data) +
  geom_line(aes(x = yrbrn, y = Mean_Trust, color = Trust_Type), size = 1) +
  geom_point(aes(x = yrbrn, y = Mean_Trust, color = Trust_Type), size = 3) +
  labs(title = "Average Trust in Politicians in France (1905-2005)",
       x = "Year",
       y = "Mean Trust (0-10 scale)") +
  ylim(4, 6) +
  scale_color_manual(
    values = c("blue", "red"),
    name = "Dataset",
    labels = c("France", "All ESS")
  ) +
  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.
## Warning: Removed 45 rows containing missing values (`geom_line()`).
## Warning: Removed 129 rows containing missing values (`geom_point()`).

#Mission 14
country_averages <- ess %>%
  group_by(cntry) %>%
  summarise(avg_trstplte = mean(trstplt, na.rm = TRUE))

ggplot(country_averages, aes(x = cntry, y = avg_trstplte, label = cntry)) +
  geom_point(aes(color = avg_trstplte), size = 3) + # scatter plot dots
  geom_text(nudge_y = 0.2, size = 3) + # labels for dots
  labs(
    x = "Country",
    y = "Average Trust in Politicians",
    title = "Average Trust in Politicians by Country",
    color = "Average Trust in Politicians (0-10"
  ) +
  theme_minimal()

#Mission 15
france_data_ <- france_data %>%
  mutate(polintr = ifelse(polintr %in% c(7, 8, 9), NA, polintr))

france_avg_interest <- france_data_ %>%
  group_by(yrbrn) %>%
  summarize(avg_interest_fr = mean(polintr, na.rm = TRUE))

uk_data <- ess %>%
  filter(cntry == "GB" & yrbrn > 1905 & yrbrn < 2005) %>%
  group_by(yrbrn) %>%
  summarise(avg_interest_uk = mean(polintr, na.rm = TRUE))

austria_data <- ess %>%
  filter(cntry == "AT" & yrbrn > 1905 & yrbrn < 2005) %>%
  group_by(yrbrn) %>%
  summarise(avg_interest_at = mean(polintr, na.rm = TRUE))
#Mission 16 - Wasn't able to complete this mission since there is not survey year ('year') variable in the data set
#Mission 17
library(ggridges)
france_data %>%
   mutate(yrbrn = case_when(
    yrbrn %in% c(7777, 8888) ~ NA_integer_,
    TRUE ~ as.integer(yrbrn)
  )) %>%
  ggplot(aes(x = polintr, y = as.factor(yrbrn), fill = as.factor(yrbrn))) +
  geom_density_ridges() +
  labs(title = "Average Level of interest in Politics Across Survey Years", 
       x = "Average Level of interest in Politics (0-10)", y = "Survey Year",
       fill = "Survey Year") +
  theme_minimal() +
  theme(legend.position = "none")
## Picking joint bandwidth of 0.272
## Warning: Removed 10 rows containing non-finite values
## (`stat_density_ridges()`).

#Mission 18
france_data %>%
   mutate(yrbrn = case_when(
    yrbrn %in% c(7777, 8888) ~ NA_integer_,
    TRUE ~ as.integer(yrbrn)
  )) %>%
group_by(yrbrn) %>%
  summarise(avg_polintr = mean(polintr, na.rm = TRUE)) %>%
  ggplot(aes(x = yrbrn, y = avg_polintr)) +
  geom_smooth(method = "loess", se = FALSE, color = "grey50") + 
  geom_point(aes(color = avg_polintr), size = 2.5) +
  labs(color = "Level of Interest in Politics")+
  scale_color_gradient(low = "darkred", high = "pink") +
  labs(title = "Average Level of Interest in Politics by Year of Birth", 
       x = "Year of Birth", y = "Average Level of Interest in Politics") +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 1 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 1 rows containing missing values (`geom_point()`).