Net Migration

For China, Mexico, Turkey & USA

From year 2000 - 2022

Attach WDI

library(WDI)
## Warning: package 'WDI' was built under R version 4.1.3
df = WDI(indicator = 'SM.POP.NETM',country=c('CN','MX','TR','USA'),start=2000,end=2022)
head(df)
##   country iso2c iso3c year SM.POP.NETM
## 1   China    CN   CHN 2021          NA
## 2   China    CN   CHN 2020          NA
## 3   China    CN   CHN 2019          NA
## 4   China    CN   CHN 2018          NA
## 5   China    CN   CHN 2017    -1741996
## 6   China    CN   CHN 2016          NA
tail(df)
##          country iso2c iso3c year SM.POP.NETM
## 83 United States    US   USA 2005          NA
## 84 United States    US   USA 2004          NA
## 85 United States    US   USA 2003          NA
## 86 United States    US   USA 2002     5334893
## 87 United States    US   USA 2001          NA
## 88 United States    US   USA 2000          NA

Attach dplyr to rename the column side by side

library(dplyr)
## Warning: package 'dplyr' was built under R version 4.1.3
## 
## 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
df<-df %>%
  rename(country = 1,
         countrycodeiso2 = 2,
         countryiso3 = 3,
         year = 4,
         SM.POP.NETM = 5)

Attach reshape2 to monitor data more broadly

library(reshape2)
## Warning: package 'reshape2' was built under R version 4.1.3
data_genis <- dcast(df, year ~ country, value.var = "SM.POP.NETM")
head(data_genis)
##   year    China   Mexico Turkiye United States
## 1 2000       NA       NA      NA            NA
## 2 2001       NA       NA      NA            NA
## 3 2002 -1965578 -2206114  -39998       5334893
## 4 2003       NA       NA      NA            NA
## 5 2004       NA       NA      NA            NA
## 6 2005       NA       NA      NA            NA
tail(data_genis)
##    year    China Mexico Turkiye United States
## 17 2016       NA     NA      NA            NA
## 18 2017 -1741996 -3e+05 1419610       4774029
## 19 2018       NA     NA      NA            NA
## 20 2019       NA     NA      NA            NA
## 21 2020       NA     NA      NA            NA
## 22 2021       NA     NA      NA            NA

Net Migration of all countries in the data set

df2 <- WDI(
  country = "all",
  indicator = "SM.POP.NETM",
  start = 2000,
  end = 2022,
  extra = TRUE,
  cache = NULL,
  latest = NULL,)

Attach ggplot2

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.1.3
library(ggfortify)
## Warning: package 'ggfortify' was built under R version 4.1.3

Convert data-set to Time series Use tsveris

tsveri <- ts(data_genis, start=2000, frequency=1)
head(tsveri)
##      year    China   Mexico Turkiye United States
## [1,] 2000       NA       NA      NA            NA
## [2,] 2001       NA       NA      NA            NA
## [3,] 2002 -1965578 -2206114  -39998       5334893
## [4,] 2003       NA       NA      NA            NA
## [5,] 2004       NA       NA      NA            NA
## [6,] 2005       NA       NA      NA            NA
tail(tsveri)
##       year    China Mexico Turkiye United States
## [17,] 2016       NA     NA      NA            NA
## [18,] 2017 -1741996 -3e+05 1419610       4774029
## [19,] 2018       NA     NA      NA            NA
## [20,] 2019       NA     NA      NA            NA
## [21,] 2020       NA     NA      NA            NA
## [22,] 2021       NA     NA      NA            NA

Plot time series

plot(tsveri[,2:5])

On a Single graph

plot(tsveri[,2:5], plot.type = "single")

Graph the time series

plot(tsveri[,2],
     type = "l",
     col = 2,
     ylim = c(0, 25),
     xlab = "year",
     ylab = "SM.POP.NETM")
lines(tsveri[,3],
      type = "l",
      col = 3)
lines(tsveri[,4],
      type = "l",
      col = 4)
lines(tsveri[,5],
      type = "l",
      col = 5)
legend("topright",                           
       c("China","Mexico", "Türkey", "US"),
       lty = 1,
       col = 2:5)

data_long <-melt(data_genis, id.vars = "year")
ggplot(data_long,                           
       aes(x = year,
           y = value,
           col = variable)) +
  geom_line()
## Warning: Removed 24 rows containing missing values (`geom_line()`).