Attach WDI
library(WDI)
## Warning: package 'WDI' was built under R version 4.1.3
df = WDI(indicator = 'NY.GDP.PCAP.CD',country=c('CN','MX','TR','USA'),start=2000,end=2021)
head(df)
## country iso2c iso3c year NY.GDP.PCAP.CD
## 1 China CN CHN 2021 12556.333
## 2 China CN CHN 2020 10408.670
## 3 China CN CHN 2019 10143.838
## 4 China CN CHN 2018 9905.342
## 5 China CN CHN 2017 8816.987
## 6 China CN CHN 2016 8094.363
Attach dplyr
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,
countrycodeiso3 = 3,
year = 4,
NY.GDP.PCAP.CD = 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 = "NY.GDP.PCAP.CD")
tail(data_genis)
## year China Mexico Turkiye United States
## 17 2016 8094.363 8744.516 10894.603 57866.74
## 18 2017 8816.987 9287.850 10589.668 59914.78
## 19 2018 9905.342 9686.985 9454.348 62805.25
## 20 2019 10143.838 9950.218 9121.515 65094.80
## 21 2020 10408.670 8431.665 8536.433 63027.68
## 22 2021 12556.333 9926.423 9586.612 69287.54
GDP PER CAPITA of all countries in the data set
df2 <- WDI(country = "all",
indicator = "NY.GDP.PCAP.CD",
start = 2000,
end = 2021,
extra = TRUE,
cache = NULL,
latest = NULL,
language = "tr" )
## Warning in WDI(country = "all", indicator = "NY.GDP.PCAP.CD", start = 2000, :
## This language is only supported partially.
Convert data-set to Time series Use tsveris
tsveri <- ts(data_genis, start=2000, frequency=1)
tail(tsveri)
## year China Mexico Turkiye United States
## [17,] 2016 8094.363 8744.516 10894.603 57866.74
## [18,] 2017 8816.987 9287.850 10589.668 59914.78
## [19,] 2018 9905.342 9686.985 9454.348 62805.25
## [20,] 2019 10143.838 9950.218 9121.515 65094.80
## [21,] 2020 10408.670 8431.665 8536.433 63027.68
## [22,] 2021 12556.333 9926.423 9586.612 69287.54
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
Plot time series
plot(tsveri[,2:5])
Plot 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 = "NY.GDP.PCAP.CD")
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()