# List of packages
packages <- c("tidyverse", "infer", "fst", "modelsummary", "broom") # 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
## [[1]]
##  [1] "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
##  [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics" 
## [13] "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[2]]
##  [1] "infer"     "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"    
##  [7] "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"    
## [13] "graphics"  "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[3]]
##  [1] "fst"       "infer"     "lubridate" "forcats"   "stringr"   "dplyr"    
##  [7] "purrr"     "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse"
## [13] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"  
## [19] "base"     
## 
## [[4]]
##  [1] "modelsummary" "fst"          "infer"        "lubridate"    "forcats"     
##  [6] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [11] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[5]]
##  [1] "broom"        "modelsummary" "fst"          "infer"        "lubridate"   
##  [6] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [11] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [16] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [21] "base"
ess <- read_fst("All-ESS-Data.fst")
#sub-setting to dataset being used and filtering for country 
GreatBritain_Data <- ess %>%
  filter(cntry == "GB")
#re-coding ESS rounds to correspond to years
GreatBritain_Data$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
   GreatBritain_Data$year[GreatBritain_Data$essround == i] <- replacements[i]
}
#cleaning educ_level, yrbrn and imwbcnt variables and subsetting them
GreatBritain_Data <- ess %>%
  filter(cntry == "GB") %>%
  mutate(
    imwbcnt = case_when(
      imwbcnt == 0 ~ "Worse place to live",
      imwbcnt == 1 ~ "Detrimental",
      imwbcnt == 2 ~ "Detrimental",
      imwbcnt == 3 ~ "Detrimental",
      imwbcnt == 4 ~ "Detrimental",
      imwbcnt == 5 ~ "Indifferent",
      imwbcnt == 6 ~ "Favourable",
      imwbcnt == 7 ~ "Favourable",
      imwbcnt == 8 ~ "Favourable",
      imwbcnt == 9 ~ "Favourable",
      imwbcnt == 10 ~ "Better place to live",
      imwbcnt %in% c(77, 88, 99) ~ NA_character_, 
      TRUE ~ as.character(imwbcnt)
   ), 
   
    # Set 'other' to NA for rounds 1-4
    edulvla = case_when(
      essround < 5 & edulvla == 55 ~ NA_real_,
      TRUE ~ edulvla
    ),
    # Set 'other' to NA for rounds 5 and later
    edulvlb = case_when(
      essround >= 5 & edulvlb == 5555 ~ NA_real_,
      TRUE ~ edulvlb
    ),
    # Create educ_level column
    educ_level = case_when(
      essround < 5 & edulvla == 5 ~ "BA",
      essround >= 5 & edulvlb > 600 ~ "BA",
      TRUE ~ "No BA"
    ),
   generation = case_when(
      (yrbrn > 1980 & yrbrn < 1997) ~ "Millennials",
      (yrbrn > 1965 & yrbrn < 1980) ~ "Generation X",
      (yrbrn > 1945 & yrbrn < 1965) ~ "Baby Boomers",
      TRUE ~ NA_character_
   ))
   

table(GreatBritain_Data$educ_level)
## 
##    BA No BA 
##  6116 14863
table(GreatBritain_Data$generation)
## 
## Baby Boomers Generation X  Millennials 
##         6723         4798         3227
table(GreatBritain_Data$imwbcnt)
## 
## Better place to live          Detrimental           Favourable 
##                  712                 6812                 6865 
##          Indifferent  Worse place to live 
##                 4946                 1281
#making sure data is clean for both variables
GreatBritain_Data <- GreatBritain_Data %>% filter(!is.na(imwbcnt))
GreatBritain_Data <- GreatBritain_Data %>% filter(!is.na(educ_level))
GreatBritain_Data <- GreatBritain_Data %>% filter(!is.na(generation))


unique(GreatBritain_Data$educ_level)
## [1] "BA"    "No BA"
table(GreatBritain_Data$educ_level)
## 
##    BA No BA 
##  4825  9732
unique(GreatBritain_Data$imwbcnt)
## [1] "Favourable"           "Detrimental"          "Worse place to live" 
## [4] "Indifferent"          "Better place to live"
table(GreatBritain_Data$imwbcnt)
## 
## Better place to live          Detrimental           Favourable 
##                  570                 4474                 5215 
##          Indifferent  Worse place to live 
##                 3478                  820
unique(GreatBritain_Data$generation)
## [1] "Generation X" "Millennials"  "Baby Boomers"
table(GreatBritain_Data$generation)
## 
## Baby Boomers Generation X  Millennials 
##         6641         4735         3181
#cleaning all 3 variables, and then providing statistics for 3 different age cohorts
GreatBritain_Data_subset <- ess %>%
 mutate(
   imwbcnt = ifelse(imwbcnt %in% c(77,88,99), NA, imwbcnt),
   edulvla = ifelse(edulvla %in% c(55,77,88,99), NA, edulvla),
   yrbrn = ifelse(yrbrn %in% c(7777,8888,9999), NA, yrbrn)
 )

millennials_data <- ess %>%
  filter(cntry == "GB", yrbrn > 1980 & yrbrn < 1997) 

boomers_data <- ess %>%
  filter(cntry == "GB", yrbrn > 1945 & yrbrn < 1965)

gen_x_data <- ess %>%
  filter(cntry == "GB" , yrbrn > 1965 & yrbrn < 1980)

millennials_data_cleaned <- millennials_data %>%
  mutate(
    imwbcnt = ifelse(imwbcnt %in% c(77, 88, 99), NA, imwbcnt),
    edulvla = ifelse(edulvla %in% c(55,77, 88, 99), NA, edulvla),
    yrbrn = ifelse(yrbrn %in% c(7777, 8888, 9999), NA, yrbrn)
)

boomers_data_cleaned <- boomers_data %>%
  mutate(
    imwbcnt = ifelse(imwbcnt %in% c(77, 88, 99), NA, imwbcnt),
    edulvla = ifelse(edulvla %in% c(55,77, 88, 99), NA, edulvla),
    yrbrn = ifelse(yrbrn %in% c(7777, 8888, 9999), NA, yrbrn)
)

gen_x_data_cleaned <- gen_x_data %>%
  mutate(
    imwbcnt = ifelse(imwbcnt %in% c(77, 88, 99), NA, imwbcnt),
    edulvla = ifelse(edulvla %in% c(55,77, 88, 99), NA, edulvla),
    yrbrn = ifelse(yrbrn %in% c(7777, 8888, 9999), NA, yrbrn)
)

summary_table_1 <- datasummary_skim(millennials_data_cleaned%>% select(imwbcnt, edulvla, yrbrn),output = "flextable")
## 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.
summary_table_2 <- datasummary_skim(boomers_data_cleaned%>% select(imwbcnt, edulvla, yrbrn),output = "flextable")
## 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.
summary_table_3 <- datasummary_skim(gen_x_data_cleaned%>% select(imwbcnt, edulvla, yrbrn),output = "flextable")
## 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.
summary_table_1

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

imwbcnt

12

1

5.4

2.5

0.0

5.0

10.0

edulvla

5

71

2.8

1.4

1.0

2.0

5.0

yrbrn

16

0

1986.9

4.2

1981.0

1986.0

1996.0

summary_table_2

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

imwbcnt

12

1

4.8

2.5

0.0

5.0

10.0

edulvla

5

61

3.1

1.7

1.0

3.0

5.0

yrbrn

19

0

1955.0

5.6

1946.0

1955.0

1964.0

summary_table_3

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

imwbcnt

12

1

5.1

2.5

0.0

5.0

10.0

edulvla

5

57

3.4

1.5

1.0

3.0

5.0

yrbrn

14

0

1972.1

4.0

1966.0

1972.0

1979.0

#creating one combined table which shows data for every age cohort, all data is cleaned and displayed in a flextable
gb_data_combined_tables <- ess %>%
  filter(cntry == "GB", (yrbrn > 1980 & yrbrn < 1997) | (yrbrn > 1945 & yrbrn < 1965) | (yrbrn > 1965 & yrbrn < 1980)) %>%
  mutate(
    generation = case_when(
      (yrbrn > 1980 & yrbrn < 1997) ~ "Millennials",
      (yrbrn > 1945 & yrbrn < 1965) ~ "Baby Boomers",
      (yrbrn > 1965 & yrbrn < 1980) ~ "Generation X",
      TRUE ~ "Other"
    ),
    imwbcnt = ifelse(imwbcnt %in% c(77, 88, 99), NA, imwbcnt),
    edulvla = ifelse(edulvla %in% c(55,77, 88, 99), NA, edulvla),
    yrbrn = ifelse(yrbrn %in% c(7777, 8888, 9999), NA, yrbrn)
  )

summary_combined_table <- datasummary_skim(gb_data_combined_tables%>% select(imwbcnt, edulvla, yrbrn),output = "flextable")
## 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.
summary_combined_table

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

imwbcnt

12

1

5.0

2.5

0.0

5.0

10.0

edulvla

5

62

3.2

1.6

1.0

3.0

5.0

yrbrn

49

0

1967.6

13.5

1946.0

1967.0

1996.0

datasummary_skim(gb_data_combined_tables %>% select(imwbcnt, edulvla, yrbrn))
Unique (#) Missing (%) Mean SD Min Median Max
imwbcnt 12 1 5.0 2.5 0.0 5.0 10.0
edulvla 5 62 3.2 1.6 1.0 3.0 5.0
yrbrn 49 0 1967.6 13.5 1946.0 1967.0 1996.0
#cleaning variables to generate visuals through infer package. the variables of educational level (BA or No BA) and imwbcnt (Immigrants make country worse or better place to live) are used, we recoded the numeric values of imwbcnt to the best of our abilities so that we could calculate the chi-squared test line and the p-value. 
GreatBritain_Data <- ess %>%
  filter(cntry == "GB") %>%
  
  mutate(
    edulvla = case_when(
      essround < 5 & edulvla == 55 ~ NA_real_,
      TRUE ~ edulvla
    ),
    # Set 'other' to NA for rounds 5 and later
    edulvlb = case_when(
      essround >= 5 & edulvlb == 5555 ~ NA_real_,
      TRUE ~ edulvlb
    ),
    # Create educ_level column
    educ_level = case_when(
      essround < 5 & edulvla == 5 ~ "BA",
      essround >= 5 & edulvlb > 600 ~ "BA",
      TRUE ~ "No BA"
    ), 
    
     imwbcnt = case_when(
      imwbcnt == 0 ~ "Worse place to live",
      imwbcnt == 1 ~ "Detrimental",
      imwbcnt == 2 ~ "Detrimental",
      imwbcnt == 3 ~ "Detrimental",
      imwbcnt == 4 ~ "Detrimental",
      imwbcnt == 5 ~ "Indifferent",
      imwbcnt == 6 ~ "Favourable",
      imwbcnt == 7 ~ "Favourable",
      imwbcnt == 8 ~ "Favourable",
      imwbcnt == 9 ~ "Favourable",
      imwbcnt == 10 ~ "Better place to live",
      imwbcnt %in% c(77, 88, 99) ~ NA_character_, 
      TRUE ~ as.character(imwbcnt)
    ),
    imwbcnt = ifelse(imwbcnt %in% c(77, 88, 99), NA, imwbcnt),
)
##Generating the tables of expected proportions and frequencies
#Table of Expected Proportions 
table_data <- GreatBritain_Data

gbtab <- table(table_data$imwbcnt, table_data$educ_level)

rsums <- rowSums(gbtab)

csums <- colSums(gbtab)

N <- sum(gbtab)

ptab <- tcrossprod(rsums/N, csums/N)
cat("Table of Expected Proportions:\n")
## Table of Expected Proportions:
print(ptab)
##            [,1]       [,2]
## [1,] 0.01011161 0.02442467
## [2,] 0.09674200 0.23368097
## [3,] 0.09749469 0.23549910
## [4,] 0.07024162 0.16966913
## [5,] 0.01819238 0.04394382
#Table of Expected frequencies 
ftab <- N * ptab
cat("Table of Expected Frequencies:\n")
## Table of Expected Frequencies:
print(ftab)
##           [,1]      [,2]
## [1,]  208.4610  503.5390
## [2,] 1994.4331 4817.5669
## [3,] 2009.9505 4855.0495
## [4,] 1448.1013 3497.8987
## [5,]  375.0541  905.9459
#Critical Value for independence
alpha <- 0.05
c_val <- qchisq(alpha, df = 1, lower.tail = FALSE)
cat("Critical Value:", round(c_val, 3), "\n")
## Critical Value: 3.841
#calculating the chi-squared test value
test_stat <- GreatBritain_Data %>%
  specify(explanatory = educ_level, 
          response = imwbcnt) %>% 
  hypothesize(null = "independence") %>%
  calculate(stat = "chisq")
## Warning: Removed 363 rows containing missing values.
print(test_stat$stat)
## X-squared 
##   1383.45
#printing the p-value, which under frequentist hypothesis testing will indicate if there is evidence to reject or accept the null-hypothesis. 
null_distribution <- GreatBritain_Data %>%
  specify(
    explanatory = educ_level,
    response = imwbcnt
  ) %>%
  hypothesize(null = "independence") %>%
  generate(reps = 2000, type = "permute") %>%
  calculate(stat = "chisq")
## Warning: Removed 363 rows containing missing values.
p_val <- null_distribution %>%
  get_pvalue(obs_stat = test_stat, direction = "two-sided")
## Warning: Please be cautious in reporting a p-value of 0. This result is an
## approximation based on the number of `reps` chosen in the `generate()` step.
## See `?get_p_value()` for more information.
print(p_val)
## # A tibble: 1 × 1
##   p_value
##     <dbl>
## 1       0
#generating the null distribution to show the amount of replications and the chi-squared test (red line). 
null_distribution %>%
  visualize() +
  shade_p_value(obs_stat = test_stat, direction = "two-sided")
## Warning: Chi-Square usually corresponds to right-tailed tests. Proceed with
## caution.

null_distribution
## Response: imwbcnt (factor)
## Explanatory: educ_level (factor)
## Null Hypothesis: independence
## # A tibble: 2,000 × 2
##    replicate  stat
##        <int> <dbl>
##  1         1  4.66
##  2         2  3.89
##  3         3  2.37
##  4         4  1.56
##  5         5  3.06
##  6         6 14.5 
##  7         7  1.47
##  8         8  1.65
##  9         9  1.94
## 10        10  1.76
## # ℹ 1,990 more rows
#creating a visual for the null-distribution, due to the nature of data and the visual looking very uninformative, we as a group decided to shrink the x-axis to increase the size of the null distributions so that we can better visualize it. We have a visual with the chi-squared test of 1383.45 in the chunk above.

library(ggplot2)

null_distribution %>%
  visualize() +
  shade_p_value(obs_stat = test_stat, direction = "two-sided") +
  coord_cartesian(xlim = c(0, 250))  # Adjust x-axis limits
## Warning: Chi-Square usually corresponds to right-tailed tests. Proceed with
## caution.

test_stat <- 1383.45
conf_int <- null_distribution %>%
  get_confidence_interval(level = 0.95, type = "percentile")

# Visualize the null distribution with manual x-axis limits
null_distribution_w_ci <- null_distribution %>%
  visualize(bins = 15, method = "simulation", dens_color = "black") +
  geom_vline(aes(xintercept = test_stat), color = "red", linetype = "dashed", size = 1) +
  shade_p_value(obs_stat = test_stat, direction = "greater") +
  shade_confidence_interval(endpoints = conf_int) +
  coord_cartesian(xlim = c(0, max(null_distribution$stat) * 1.4)) + # Set manual x-axis limits
  labs(title = "Simulation-Based Null Distribution",
       x = "stat",
       y = "Count")
## 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.
null_distribution_w_ci
## Warning in min(diff(unique_loc)): no non-missing arguments to min; returning
## Inf

ggsave(filename = "null_distribution_with_ci.jpeg", plot = null_distribution_w_ci, device = "jpeg", width = 8, height = 6)
## Warning in min(diff(unique_loc)): no non-missing arguments to min; returning
## Inf
#Linear regression modelling
#cleaning imwbcnt, educ_level and yrbrn to make sure no NA are in data

GreatBritain_Data <- ess %>%
  filter (cntry == "GB") %>%
  mutate(imwbcnt=ifelse(imwbcnt %in% c(77, 88, 99), NA, imwbcnt))
unique(GreatBritain_Data$imwbcnt)
##  [1]  9  4  2  6  1  3  0  5  7 10  8 NA
GreatBritain_Data <- GreatBritain_Data %>% filter(!is.na(imwbcnt))
unique(GreatBritain_Data$imwbcnt)
##  [1]  9  4  2  6  1  3  0  5  7 10  8
#cleaning educ_level to make sure no NA are in data
GreatBritain_Data <- GreatBritain_Data %>%
  mutate(edulvla = case_when(
      essround < 5 & edulvla == 55 ~ NA_real_,
      TRUE ~ edulvla
    ),
    # Set 'other' to NA for rounds 5 and later
    edulvlb = case_when(
      essround >= 5 & edulvlb == 5555 ~ NA_real_,
      TRUE ~ edulvlb
    ),
    # Create educ_level column
    educ_level = case_when(
      essround < 5 & edulvla == 5 ~ "BA",
      essround >= 5 & edulvlb > 600 ~ "BA",
      TRUE ~ "No BA"
  ),
  
   generation = case_when(
      (yrbrn > 1980 & yrbrn < 1997) ~ "Millennials",
      (yrbrn > 1965 & yrbrn < 1980) ~ "Generation X",
      (yrbrn > 1945 & yrbrn < 1965) ~ "Baby Boomers",
      TRUE ~ NA_character_
      
   ))

table(GreatBritain_Data$generation)
## 
## Baby Boomers Generation X  Millennials 
##         6641         4735         3181
unique(GreatBritain_Data$generation)
## [1] "Generation X" "Millennials"  NA             "Baby Boomers"
GreatBritain_Data <- GreatBritain_Data %>% filter(!is.na(generation))

table(GreatBritain_Data$educ_level)
## 
##    BA No BA 
##  4825  9732
unique(GreatBritain_Data$educ_level)
## [1] "BA"    "No BA"
GreatBritain_Data <- GreatBritain_Data %>% filter(!is.na(educ_level))
unique(GreatBritain_Data$educ_level)
## [1] "BA"    "No BA"
model_1 <- lm(imwbcnt ~ educ_level, data = GreatBritain_Data)
coefficients <- coef(model_1)
print(coefficients)
##     (Intercept) educ_levelNo BA 
##        6.020518       -1.489486
remotes :: install_github ("datalorax/equatiomatic")
## Skipping install of 'equatiomatic' from a github remote, the SHA1 (39e8c3ea) has not changed since last install.
##   Use `force = TRUE` to force installation
equatiomatic::extract_eq(model_1, use_coefs = TRUE)

\[ \operatorname{\widehat{imwbcnt}} = 6.02 - 1.49(\operatorname{educ\_level}_{\operatorname{No\ BA}}) \]

GreatBritain_Data$weight <- GreatBritain_Data$dweight * GreatBritain_Data$pweight
model1 <- lm(imwbcnt ~ educ_level, data = GreatBritain_Data, weights = weight)
model2 <- lm(imwbcnt ~ educ_level + generation, data = GreatBritain_Data, weights = weight)
model3 <- lm(imwbcnt ~ educ_level + generation*educ_level, data = GreatBritain_Data, weights = weight)

modelsummary(
  list(model1, model2, model3),
  fmt = 1,
  estimate  = c( "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}"),
  statistic = NULL,
  coef_omit = "Intercept")
 (1)   (2)   (3)
educ_levelNo BA −1.5 (0.0)*** −1.5 (0.0)*** −1.4 (0.1)***
generationGeneration X 0.2 (0.0)*** 0.3 (0.1)***
generationMillennials 0.7 (0.0)*** 0.7 (0.1)***
educ_levelNo BA × generationGeneration X −0.2 (0.1)+
educ_levelNo BA × generationMillennials −0.1 (0.1)
Num.Obs. 14557 14557 14557
R2 0.076 0.088 0.088
R2 Adj. 0.076 0.088 0.088
AIC 68746.1 68561.6 68561.8
BIC 68768.8 68599.5 68614.9
Log.Lik. −34370.039 −34275.805 −34273.895
RMSE 2.42 2.41 2.41
df <- GreatBritain_Data %>%
 mutate(
   imwbcnt = ifelse(imwbcnt %in% c(77,88,99), NA, imwbcnt),
   edulvla = ifelse(edulvla %in% c(55,77,88,99), NA, edulvla),
   yrbrn = ifelse(yrbrn %in% c(7777,8888,9999), NA, yrbrn)
 )

figure3boxplot <- ggplot(df, aes(x = reorder(educ_level, -imwbcnt, FUN=median), y = imwbcnt, fill = educ_level)) +
  geom_boxplot() +
  theme_minimal() + 
  theme(legend.position = "none") + 
  labs(title = "Figure 2. Boxplot Comparison for the belief that Immigrants make country worse or better place to live and Educational Attainment",
       x = "Educational Attainment", 
       y = "Immigrants make country worse or better place to live (0-10)")

figure3boxplot