#1. LOAD LIBRARIES

#2. LOAD RAW DATA FROM GitHub

# Read all CSV files in working directory
csv_files <- list.files(pattern = "\\.csv$")
data_list <- setNames(lapply(csv_files, read.csv), sub("\\.csv$", "", csv_files))

# Add ISO2 codes and handle Namibia / missing
data_list <- lapply(data_list, function(df) {
  if ("member" %in% names(df)) {
    df$iso2 <- countrycode(df$member, origin = "country.name", destination = "iso2c")
    df$iso2[df$member == "Namibia"] <- "NA"
    df$iso2[is.na(df$iso2)] <- "NA_missing"
  }
  return(df)
})

# Preview
unique(data_list$hr_status_cat$iso2)
##   [1] "AF" "AL" "DZ" "AD" "AO" "AG" "AR" "AM" "AU" "AT" "AZ" "BS" "BH" "BD" "BB"
##  [16] "BY" "BE" "BZ" "BJ" "BT" "BO" "BA" "BW" "BR" "BN" "BG" "BF" "BI" "KH" "CM"
##  [31] "CA" "CV" "CF" "TD" "CL" "CN" "CO" "KM" "CG" "CR" "HR" "CU" "CY" "CZ" "CD"
##  [46] "DK" "DJ" "DM" "DO" "TL" "EC" "EG" "SV" "GQ" "ER" "EE" "ET" "FJ" "FI" "FR"
##  [61] "GA" "GM" "GE" "DE" "GH" "GR" "GD" "GT" "GN" "GW" "GY" "HT" "HN" "HU" "IS"
##  [76] "IN" "ID" "IR" "IQ" "IE" "IL" "IT" "CI" "JM" "JP" "JO" "KZ" "KE" "KI" "KW"
##  [91] "KG" "LA" "LV" "LB" "LS" "LR" "LY" "LI" "LT" "LU" "MG" "MW" "MY" "MV" "ML"
## [106] "MT" "MH" "MR" "MU" "MX" "FM" "MD" "MC" "MN" "ME" "MA" "MZ" "MM" "NA" "NR"
## [121] "NP" "NL" "NZ" "NI" "NE" "NG" "KP" "MK" "NO" "OM" "PK" "PW" "PA" "PG" "PY"
## [136] "PE" "PH" "PL" "PT" "QA" "RO" "RU" "RW" "WS" "SM" "ST" "SA" "SN" "RS" "SC"
## [151] "SL" "SG" "SK" "SI" "SB" "SO" "ZA" "KR" "SS" "ES" "LK" "KN" "LC" "VC" "SD"
## [166] "SR" "SZ" "SE" "CH" "SY" "TJ" "TZ" "TH" "TG" "TO" "TT" "TN" "TR" "TM" "TV"
## [181] "UG" "UA" "AE" "GB" "US" "UY" "UZ" "VU" "VE" "VN" "YE" "ZM" "ZW"
sum(is.na(data_list$hr_status_cat$iso2))
## [1] 0

#3. LET’S CREATE A DF WITH MAPPING DATA FOR THE WORLD

wmap <- map_data("world")

# Visualize base world map
ggplot() +
  geom_polygon(data = wmap, aes(x = long, y = lat, group = group),
               fill = "lightgray", color = "white") +
  coord_fixed(1.3) +
  ggtitle("Base World Map") +
  theme_minimal()

#4. LET’s COMBINE THE TWO DATASETS

# Add ISO2 codes safely (no warnings)
wmap$iso2 <- countrycode(wmap$region, origin = "country.name", destination = "iso2c", warn = FALSE)
wmap$iso2[is.na(wmap$iso2)] <- "NA_missing"

# Extract target dataset
hr_data <- data_list$hr_status_cat

# Merge map and data
wmap_data_list <- wmap %>%
  left_join(hr_data, by = "iso2")

# Preview
head(wmap_data_list)
##        long      lat group order region subregion iso2 member state.party
## 1 -69.89912 12.45200     1     1  Aruba      <NA>   AW   <NA>          NA
## 2 -69.89571 12.42300     1     2  Aruba      <NA>   AW   <NA>          NA
## 3 -69.94219 12.43853     1     3  Aruba      <NA>   AW   <NA>          NA
## 4 -70.00415 12.50049     1     4  Aruba      <NA>   AW   <NA>          NA
## 5 -70.06612 12.54697     1     5  Aruba      <NA>   AW   <NA>          NA
## 6 -70.05088 12.59707     1     6  Aruba      <NA>   AW   <NA>          NA
##   year.ratification.or.accession signatory no.action status note
## 1                             NA        NA        NA   <NA> <NA>
## 2                             NA        NA        NA   <NA> <NA>
## 3                             NA        NA        NA   <NA> <NA>
## 4                             NA        NA        NA   <NA> <NA>
## 5                             NA        NA        NA   <NA> <NA>
## 6                             NA        NA        NA   <NA> <NA>

#5. MAKE THE MAP WITH THE “STATE.PARTY” VARIABLE:

ggplot(wmap_data_list, aes(long, lat, group = group)) +
  coord_fixed(1.3) +
  geom_polygon(aes(fill = state.party),
               colour = alpha("grey", 0.5),
               linewidth = 0.05) +
  geom_polygon(data = wmap_data_list, colour = "white", fill = NA) +
  ggtitle("Ratification of the 1966 International Convention on the Elimination of All Forms of Racial Discrimination") +
  scale_fill_distiller(palette = "RdBu", direction = 1, na.value = "lightgrey") +
  theme_minimal()

#6. MAP USING THE “STATUS” VARIABLE

ggplot(wmap_data_list, aes(long, lat, group = group)) +
  coord_fixed(1.3) +
  geom_polygon(aes(fill = status),
               colour = "white",
               linewidth = 0.05) +
  geom_polygon(data = wmap_data_list, colour = "white", fill = NA) +
  ggtitle("Ratification Status of the 1966 International Convention") +
  scale_fill_brewer(palette = "Set2", na.value = "lightgrey") +
  theme_minimal()

matched <- sum(wmap_data_list$iso2 != "NA_missing")
unmatched <- sum(wmap_data_list$iso2 == "NA_missing")

summary_table <- data.frame(
  Category = c("Matched Countries", "Unmatched Regions"),
  Count = c(matched, unmatched)
)

kable(summary_table, caption = "Summary of ISO2 Matches Between Map and Dataset")
Summary of ISO2 Matches Between Map and Dataset
Category Count
Matched Countries 98855
Unmatched Regions 483