library(here) 
## here() starts at C:/Users/siwei/Desktop/CASA0005/Week4_practical
library(dplyr) 
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(janitor) 
## 
## Attaching package: 'janitor'
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
library(sf) 
## Linking to GEOS 3.13.1, GDAL 3.11.0, PROJ 9.6.0; sf_use_s2() is TRUE
library(countrycode) 
## Warning: package 'countrycode' was built under R version 4.5.2
library(ggplot2) 
library(tmap)

2. Data insert

df_gii <- read.csv("HDR25_Composite_indices_complete_time_series.csv", 
                   fileEncoding = "latin1",
                   na.strings = "n/a") %>%
  select(country, iso3, gii_2010, gii_2019) %>%
  mutate(diff2019_10 = gii_2010 - gii_2019) # poitive indicates GII lower down, be the improvement

head(df_gii)
##               country iso3 gii_2010 gii_2019 diff2019_10
## 1         Afghanistan  AFG    0.704    0.676       0.028
## 2             Albania  ALB    0.192    0.131       0.061
## 3             Algeria  DZA    0.508    0.385       0.123
## 4             Andorra  AND       NA       NA          NA
## 5              Angola  AGO    0.556    0.536       0.020
## 6 Antigua and Barbuda  ATG       NA       NA          NA
worldmap <- st_read("World_Countries_(Generalized)_8414823838130214587.gpkg") %>%
  clean_names()
## Reading layer `World_Countries_Generalized' from data source 
##   `C:\Users\siwei\Desktop\CASA0005\Week4_practical\World_Countries_(Generalized)_8414823838130214587.gpkg' 
##   using driver `GPKG'
## Simple feature collection with 251 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -20037510 ymin: -30240970 xmax: 20037510 ymax: 18418390
## Projected CRS: WGS 84 / Pseudo-Mercator
head(worldmap)
## Simple feature collection with 6 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -19015950 ymin: -2039467 xmax: 8339582 ymax: 5260415
## Projected CRS: WGS 84 / Pseudo-Mercator
##          country iso    countryaff aff_iso                          SHAPE
## 1    Afghanistan  AF   Afghanistan      AF MULTIPOLYGON (((6821275 424...
## 2        Albania  AL       Albania      AL MULTIPOLYGON (((2178615 511...
## 3        Algeria  DZ       Algeria      DZ MULTIPOLYGON (((512443 4423...
## 4 American Samoa  AS United States      US MULTIPOLYGON (((-19007124 -...
## 5        Andorra  AD       Andorra      AD MULTIPOLYGON (((160949.7 52...
## 6         Angola  AO        Angola      AO MULTIPOLYGON (((2613349 -19...
gii_map <- worldmap %>%
  mutate(iso = countrycode(iso, origin = "iso2c", destination = "iso3c")) %>%
  left_join(., df_gii, by = c("iso" = "iso3")) %>%
  # Transform coordinate system to ESRI:54030 for consistent projection
  st_transform(., crs = "ESRI:54030")

3. GII Histogram of Changes (ggplot)

gghist <- ggplot(df_gii, 
               aes(x = diff2019_10)) + 
  geom_histogram(color = "black", 
                 fill = "lightpink", 
                 bins = 60) +
  labs(title = "Distribution of change in the GII (2010–2019)", 
       x = "Variation of GII (GII_2010 - GII_2019)",
       y = "Frequency") +
  # Add a mean line and center the title
  geom_vline(aes(xintercept = mean(diff2019_10, na.rm = TRUE)),
             color = "yellow", 
             linetype = "dashed", 
             linewidth = 1) +
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) 


gghist
## Warning: Removed 41 rows containing non-finite outside the scale range
## (`stat_bin()`).

4. GII Spatial Distribution Map (tmap)

tmap_mode("view") 
## ℹ tmap modes "plot" - "view"
## ℹ toggle with `tmap::ttm()`
# tmap_mode("plot") 
tm_shape(gii_map) + 
  tm_polygons("diff2019_10",
              style = "jenks", 
              palette = "carto.blu_grn",
              alpha = 0.9,
              title = "Difference in inequality index") + 
  tm_basemap(server = "OpenStreetMap") + 
  tm_compass(type = "arrow", position = c("left", "bottom")) + 
  tm_scalebar(position = c("left", "bottom")) +
  tm_layout(title = "Difference in Gender inequality index 2010 - 2019", 
            title.size = 1.2, 
            title.position = c("center", "top"))
## 
## ── tmap v3 code detected ───────────────────────────────────────────────────────
## [v3->v4] `tm_polygons()`: instead of `style = "jenks"`, use fill.scale =
## `tm_scale_intervals()`.
## ℹ Migrate the argument(s) 'style', 'palette' (rename to 'values') to
##   'tm_scale_intervals(<HERE>)'[v3->v4] `tm_polygons()`: use `fill_alpha` instead of `alpha`.[v3->v4] `tm_polygons()`: migrate the argument(s) related to the legend of the
## visual variable `fill` namely 'title' to 'fill.legend = tm_legend(<HERE>)'[v3->v4] `tm_layout()`: use `tm_title()` instead of `tm_layout(title = )`