# List of packages
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer", 
              "fst", "viridis", "knitr", "kableExtra", "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
## Loading required package: viridisLite
## 
## 
## Attaching package: 'kableExtra'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     group_rows
## [[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] "kableExtra"   "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] "rmarkdown"    "kableExtra"   "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"    "kableExtra"   "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"        
## 
## [[11]]
##  [1] "ggridges"     "rmarkdown"    "kableExtra"   "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"        
## 
## [[12]]
##  [1] "questionr"    "ggridges"     "rmarkdown"    "kableExtra"   "knitr"       
##  [6] "viridis"      "viridisLite"  "fst"          "RColorBrewer" "modelsummary"
## [11] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [16] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [21] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [26] "methods"      "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]
}
sweden_data <- ess[ess$cntry == "SE", ]
  1. Provide either a time trend of the mean for an outcome variable by survey year. If you only have say one or two rounds, provide instead a graph by birth year. Other options include showcasing the distribution or frequency counts.
se_data_subset <- sweden_data %>%
  filter(essround %in% c("8")) %>%
  mutate(
    vote = ifelse(vote %in% c(7, 8, 9), NA, vote),
    prtvtbse = ifelse(prtvtbse %in% c(66, 77, 88, 99), NA, prtvtbse),
    gvslvue  = ifelse(gvslvue  %in% c(77, 88, 99), NA, gvslvue),
    hinctnta = ifelse(hinctnta %in% c(77, 88, 99), NA, hinctnta),
    mbtru = ifelse(mbtru %in% c(7, 8, 9), NA, mbtru),
    edlvdse = ifelse(edlvdse %in% c(5555, 7777, 8888, 9999), NA, edlvdse),
    yrbrn = ifelse(yrbrn %in% c(7777, 8888, 9999), NA, yrbrn),
     
  ) %>%
  select(vote, prtvtbse, gvslvue, mbtru, edlvdse, hinctnta, essround, year, yrbrn)
## check and clean always
table(se_data_subset$yrbrn)
## 
## 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 
##   18    5    6    6    3    8   11    5   14   14   12    7   24   18   19    9 
## 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 
##   21   19   20   28   33   36   36   36   18   30   34   30   28   24   30   26 
## 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 
##   21   16   30   26   21   25   27   27   23   21   26   30   15   18   27   23 
## 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 
##   24   31   30   26   22   16   26   18   16   27   18   24   15   19   17   17 
## 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 
##   22   19   20   19   12   15   21   17   14   15    5   18
table(se_data_subset$gvslvue)
## 
##   0   1   2   3   4   5   6   7   8   9  10 
##   2   5   9  34  80 214 256 326 305 116 185
se_data_subset %>%
  filter(essround %in% c("8")) %>%
  group_by(yrbrn) %>%
  summarise(avg_gvslvue = mean(gvslvue, na.rm = TRUE)) %>%
  ggplot(aes(x = yrbrn, y = avg_gvslvue)) +
  geom_line() +
  ylim(0, 10) +
  labs(title = "Average Attitudes Towards Government's Responsibility for \nStandard of Living for Unemployed by Birth Year", 
       x = "Birth Year", y = "Average Attitudes Government Responsibility \nfor Standard of Living Unemployed (0-10)") 
## Warning: Removed 1 row containing missing values (`geom_line()`).

  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
  1. Provide the same main trend graph but this time comparing it to either the ESS baseline (all countries) or specified countries. If choosing the latter, it must be justified. Meaning, you should just choosing 2-5 other countries at random. Rather, focus say on countries often compared to in the literature or inclusive of the same region (e.g., Western Europe) or chosen to reflect one of the regions (e.g., one country for Western Europe, one for Eastern Europe, Scandinavia, etc.).
ess$gvslvue[ess$gvslvue %in% c(77, 88, 99)] <- NA

avg_gvslvue_se <- se_data_subset %>%
  group_by(yrbrn) %>%
  summarize(mean_gvslvue_se = mean(gvslvue, na.rm = TRUE))

avg_gvslvue_all_countries <- ess %>%
  filter(essround == "8") %>%
  group_by(yrbrn) %>%
  summarize(mean_gvslvue_all = mean(gvslvue, na.rm = TRUE))

combined_data <- left_join(avg_gvslvue_se, avg_gvslvue_all_countries, by = "yrbrn")

long_data <- combined_data %>%

  pivot_longer(
    cols = starts_with("mean_gvslvue"), 
    
    names_to = "Gvslvue_Type",  
    
    values_to = "Mean_Gvslvue"  
  )
ggplot(long_data) +
  geom_line(aes(x = yrbrn, y = Mean_Gvslvue, color = Gvslvue_Type), size = 1) +
  geom_point(aes(x = yrbrn, y = Mean_Gvslvue, color = Gvslvue_Type), size = 1) +
  labs(title = "Average Attitudes Towards Government's Responsibility for \nStandard of Living for Unemployed in Sweden (2016)", 
       x = "Birth Year", 
       y = "Mean Attitudes Towards Government's Responsibility for \nStandard of Living for Unemployed (0-10)") +
  ylim(5, 10) +
  scale_color_manual(values = c("blue", "red"), 
                     name = "Dataset", 
                     labels = c("Sweden", "All ESS"), 
                     breaks = c("mean_gvslvue_se", "mean_gvslvue_all")) + 
  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 2 rows containing missing values (`geom_line()`).
## Warning: Removed 2 rows containing missing values (`geom_point()`).

  1. Summarize two variables related to your outcome of interest, along with three to five other variables that are related to potential explanatory variables. These latter variables can be tied to socio-demographics, but they do not have to. Decisions on the explanatory variables should be anchored in the literature (i.e., which ones to focus on and why). Note: use datasummary_skim from the modelsummary package. You can alter aesthetics if you wish. See details here: https://modelsummary.com/articles/datasummary.html.

Discuss your table in terms of what is showcases (mean, SD, min, max, etc.) and describe some of what is contained in the table (not all).

datasummary_skim(se_data_subset$vote)
Unique (#) Missing (%) Mean SD Min Median Max
data 4 0 1.2 0.5 1.0 1.0 3.0
datasummary_skim(se_data_subset$prtvtbse)
Unique (#) Missing (%) Mean SD Min Median Max
data 12 19 5.4 2.1 1.0 6.0 11.0
datasummary_skim(se_data_subset$gvslvue)
Unique (#) Missing (%) Mean SD Min Median Max
data 12 1 7.0 1.9 0.0 7.0 10.0
datasummary_skim(se_data_subset$mbtru)
Unique (#) Missing (%) Mean SD Min Median Max
data 4 0 1.7 0.8 1.0 2.0 3.0
datasummary_skim(se_data_subset$edlvdse)
Unique (#) Missing (%) Mean SD Min Median Max
data 21 1 8.8 5.0 1.0 8.0 20.0
datasummary_skim(se_data_subset$hinctnta)
Unique (#) Missing (%) Mean SD Min Median Max
data 11 7 6.3 2.9 1.0 7.0 10.0
  1. Provide column percentages (representing conditional probabilities if divided by 100) for your 3-5 explanatory variables relative to a central outcome variable. Along with these, provide the mean and standard deviation (e.g., average male, average female for outcome variable).

Note: use cprop() from the questionr package for column percentages. For the mean and std, you can use, e.g., group_by() and summarise() by piping with tidyverse.

Highlight a few of the generated statistics (not all) from the tables that stand out to you.

# mbtru
table(se_data_subset$mbtru, se_data_subset$gvslvue) %>%
  cprop()
##        
##         0     1     2     3     4     5     6     7     8     9     10    All  
##   1      50.0  60.0  44.4  38.2  48.1  45.3  48.4  50.6  52.8  50.0  48.1  49.2
##   2      50.0  20.0  44.4  38.2  31.6  29.4  28.7  27.9  27.9  35.3  29.2  29.5
##   3       0.0  20.0  11.1  23.5  20.3  25.2  22.8  21.5  19.3  14.7  22.7  21.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
sd_mbtru <- sd(se_data_subset$mbtru, na.rm = TRUE)
sd_mbtru
## [1] 0.7947318
mean_mbtru <- se_data_subset %>%
  summarise(avg_mbtru = mean(mbtru, na.rm = TRUE))
mean_mbtru
##   avg_mbtru
## 1  1.726214
# edlvdse
table(se_data_subset$edlvdse, se_data_subset$gvslvue) %>%
  cprop()
##        
##         0     1     2     3     4     5     6     7     8     9     10    All  
##   1       0.0   0.0   0.0   0.0   2.5   2.3   2.0   0.3   1.6   1.7   2.2   1.6
##   2       0.0   0.0   0.0   5.9   8.9  10.3   4.7   9.3   5.9   3.5   7.6   7.1
##   3       0.0  20.0  11.1   8.8   5.1  11.7   7.1  11.4   8.9  10.4   8.2   9.4
##   4       0.0   0.0   0.0   5.9   2.5   6.1   7.5   6.2   6.2   8.7   6.5   6.4
##   5      50.0  20.0  11.1   2.9  11.4   7.9   5.9   9.9  11.5   8.7  10.3   9.2
##   6      50.0  20.0   0.0   2.9   5.1   1.9   4.3   4.0   5.6   6.1   5.4   4.5
##   7       0.0  40.0   0.0  20.6   7.6  10.7  12.9  10.8   8.2   7.8  15.8  11.1
##   8       0.0   0.0  33.3   5.9   5.1   2.8   4.7   4.0   3.3   4.3   2.2   3.9
##   9       0.0   0.0   0.0   2.9   6.3   3.7   2.4   3.7   3.0   1.7   2.7   3.1
##   10      0.0   0.0  22.2   2.9  11.4   7.0   5.1   3.7   6.6   7.0   6.0   6.0
##   11      0.0   0.0  11.1   0.0   3.8   7.5   6.3   7.7   7.2   5.2   4.3   6.4
##   12      0.0   0.0   0.0  11.8   6.3   5.1   6.3   4.0   5.3   0.9   3.8   4.8
##   13      0.0   0.0  11.1  17.6   8.9   6.5   7.5   5.9   7.6   5.2   9.2   7.3
##   14      0.0   0.0   0.0   0.0   3.8   2.8   4.7   4.9   4.6   7.8   1.6   4.1
##   15      0.0   0.0   0.0   0.0   1.3   1.9   2.7   4.9   5.3   6.1   5.4   4.0
##   16      0.0   0.0   0.0   0.0   0.0   1.4   1.2   0.6   0.7   0.0   1.1   0.8
##   17      0.0   0.0   0.0   2.9   2.5   3.7   4.7   3.4   2.3   7.8   3.8   3.7
##   18      0.0   0.0   0.0   2.9   7.6   4.7   6.3   3.7   3.3   5.2   3.8   4.5
##   19      0.0   0.0   0.0   0.0   0.0   0.9   0.0   0.0   0.3   0.0   0.0   0.2
##   20      0.0   0.0   0.0   5.9   0.0   0.9   3.9   1.5   2.6   1.7   0.0   1.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
sd_edlvdse <- sd(se_data_subset$edlvdse, na.rm = TRUE)
sd_edlvdse
## [1] 5.008842
mean_edlvdse <- se_data_subset %>%
  summarise(avg_edlvdse = mean(edlvdse, na.rm = TRUE))
mean_edlvdse
##   avg_edlvdse
## 1    8.768332
# hinctnta
table(se_data_subset$hinctnta, se_data_subset$gvslvue) %>%
  cprop()
##        
##         0     1     2     3     4     5     6     7     8     9     10    All  
##   1       0.0  25.0  22.2   6.1  10.8   9.2   5.8   5.0   5.9   2.7   9.9   6.8
##   2       0.0   0.0  22.2   9.1   6.8   9.7   6.2   6.9   9.4  10.6  11.7   8.6
##   3      50.0   0.0   0.0   6.1   6.8   9.2   5.3   9.9   5.2   6.2   5.8   7.0
##   4       0.0   0.0  11.1   9.1   9.5   5.1   6.6   8.9   7.0  12.4  11.1   8.2
##   5       0.0   0.0   0.0   3.0   4.1  10.7   7.0   6.6  10.5   7.1   6.4   7.7
##   6      50.0   0.0  11.1  12.1   4.1   8.2   8.6   7.6   8.4   8.8  11.1   8.5
##   7       0.0   0.0   0.0  21.2   6.8   9.7   6.2   9.9  13.6  12.4   7.0   9.8
##   8       0.0  25.0  11.1  21.2   8.1   9.7  18.1  14.9   9.8  10.6  13.5  13.0
##   9       0.0  50.0   0.0   0.0  16.2  12.2  16.9  14.2  14.6  14.2  13.5  14.1
##   10      0.0   0.0  22.2  12.1  27.0  16.3  19.3  16.2  15.7  15.0   9.9  16.2
##   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
sd_hinctnta <- sd(se_data_subset$hinctnta, na.rm = TRUE)
sd_hinctnta
## [1] 2.917055
mean_hinctnta <- se_data_subset %>%
  summarise(avg_hinctnta = mean(hinctnta, na.rm = TRUE))
mean_hinctnta
##   avg_hinctnta
## 1     6.298197
  1. Provide a visualization for one of the explanatory variable relative to your main outcome.
sweden_data <- ess[ess$cntry == "SE", ]
sweden_variables_cleaned <- sweden_data %>% 
  filter(cntry == "SE", essround == 8) 
sweden_variables_cleaned <- sweden_variables_cleaned %>% 
  filter(cntry == "SE") %>%
  mutate(
  prtvtbse = ifelse(prtvtbse %in% c(66, 77, 88, 99), NA, prtvtbse),
  mbtru = ifelse(mbtru %in% c(7, 8, 9), NA, mbtru))%>%
  select(prtvtbse, mbtru)
datasummary_skim(sweden_variables_cleaned)
Unique (#) Missing (%) Mean SD Min Median Max
prtvtbse 12 19 5.4 2.1 1.0 6.0 11.0
mbtru 4 0 1.7 0.8 1.0 2.0 3.0
table(sweden_variables_cleaned)
##         mbtru
## prtvtbse   1   2   3
##       1   41  27  12
##       2   34  32  12
##       3   23   8  13
##       4   67  15  14
##       5  134 115  80
##       6  248 128  43
##       7   49  28   3
##       8   20   3   5
##       9    3   0   0
##       10  43  38  13
##       11   2   1   6
plot_df<- sweden_variables_cleaned %>%
          group_by(mbtru, prtvtbse)%>%
          summarise(count = n())
## `summarise()` has grouped output by 'mbtru'. You can override using the
## `.groups` argument.
#remove NA

plot_df <- drop_na(plot_df)

#make sure the variable you want to color by is factor

plot_df$mbtru <- as.factor(plot_df$mbtru)
plot_df$prtvtbse <- as.factor(plot_df$prtvtbse)
ggplot(plot_df, aes(x = prtvtbse, y= count, fill = mbtru)) +
  geom_bar(stat="identity", position = "dodge2") +
  
  # Customize fill colors and add labels to the legend
  scale_fill_manual(values = c("1" = "blue", "2" = "lightblue", "3" = "purple"),
                    labels = c("1" = "Yes, Currently", 
                               "2" = "Yes, Previously", 
                               "3" = "No")) +
  
  scale_x_discrete(labels = c("1" = "Centern", "2" = "Folkpartiet liberalerna", "3" = "Kristdemokraterna", "4" = "Miljöpartiet de gröna", "5" = "Moderata samlingspartiet", "6" = "Socialdemokraterna", "7" = "Vänsterpartiet", "8" = "FI (Feministiskt initiativ)", "9" = "Piratpartiet", "10" = "Sverigedemokraterna", "11" = "Annat parti")) +
  
  # Customize other plot elements
    labs(title = "Comparison of Party Vote Choice by Union Membership",
       x = "Party Vote",
       y = "Count",
       color= "Union Membership",
       fill = "Union Membership") +
  
  theme_minimal() + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))

# Display the plot
print(plot)
## function (x, y, ...) 
## UseMethod("plot")
## <bytecode: 0x7ff6eae62718>
## <environment: namespace:base>