library(countrycode)
## Warning: package 'countrycode' was built under R version 4.4.2
library(haven)
## Warning: package 'haven' was built under R version 4.4.3
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
library(plotly)
## Warning: package 'plotly' was built under R version 4.4.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.4.3
## Warning: package 'tidyr' was built under R version 4.4.3
## Warning: package 'readr' was built under R version 4.4.3
## Warning: package 'purrr' was built under R version 4.4.3
## Warning: package 'dplyr' was built under R version 4.4.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ lubridate 1.9.3     ✔ tibble    3.2.1
## ✔ purrr     1.1.0     ✔ tidyr     1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks plotly::filter(), stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
imputed_variables <- readRDS("C:/Users/Rumeysa Görgülü/Desktop/imputed_variables.rds")
imputed_variables <- imputed_variables %>%
  mutate(CNTRYID = as.numeric(CNTRYID))

ESCS Scores by Country

p <- ggplot(imputed_variables, aes(x = CNT, y = ESCS, fill = CNT)) +
  geom_boxplot(outlier.size = .01, outlier.colour = "black") +
  labs(title = "ESCS Scores by Country", x = "Country", y = "ESCS Scores") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  guides(fill = "none")

ggplotly(p)

World Maps of Academic Scores

world_map <- map_data("world")

imputed_variables <- imputed_variables %>%
  mutate(COUNTRY_NAME = countrycode(CNTRYID, origin = "un", destination = "country.name", warn = FALSE)) %>%
  mutate(
    COUNTRY_NAME = case_when(
      CNTRYID == 158 ~ "Taiwan",
      CNTRYID == 383 ~ "Kosovo",
      CNTRYID == 826 ~ "UK",
      CNTRYID == 840 ~ "USA",
      CNTRYID == 901 ~ "Special Region or Entity",
      TRUE ~ COUNTRY_NAME
    )
  )

plot_map <- function(world_map, score_column, name) {
  world_map <- world_map %>%
    group_by(region) %>%
    mutate(score = score_column[match(region, imputed_variables$COUNTRY_NAME)])

  ggplot(data = world_map) +
    geom_polygon(aes(x = long, y = lat, group = group, fill = score),
                 color = "black", linewidth = 0.1) +
    scale_fill_viridis_c(option = "viridis", na.value = "grey60") +
    labs(title = paste("World Map of", name, "Scores"))
}

Math Scores

ggplotly(plot_map(world_map, imputed_variables$MATHH, "Math"))

Reading Scores

ggplotly(plot_map(world_map, imputed_variables$READD, "Reading"))

Science Scores

ggplotly(plot_map(world_map, imputed_variables$SCIEE, "Science"))