Attach WDI
library(WDI)
## Warning: package 'WDI' was built under R version 4.1.3
df = WDI(indicator = 'EG.USE.ELEC.KH.PC',country=c('CN','MX','TR','USA'),start=1995,end=2005)
head(df)
## country iso2c iso3c year EG.USE.ELEC.KH.PC
## 1 China CN CHN 2005 1782.3122
## 2 China CN CHN 2004 1585.8388
## 3 China CN CHN 2003 1379.4846
## 4 China CN CHN 2002 1194.8563
## 5 China CN CHN 2001 1076.5491
## 6 China CN CHN 2000 992.9434
tail(df)
## country iso2c iso3c year EG.USE.ELEC.KH.PC
## 39 United States US USA 2000 13671.05
## 40 United States US USA 1999 13281.87
## 41 United States US USA 1998 13154.76
## 42 United States US USA 1997 12889.83
## 43 United States US USA 1996 12854.30
## 44 United States US USA 1995 12659.61
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,
countryiso3 = 3,
year = 4,
EG.USE.ELEC.KH.PC = 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 = "EG.USE.ELEC.KH.PC")
head(data_genis)
## year China Mexico Turkiye United States
## 1 1995 770.2802 1419.903 1227.327 12659.61
## 2 1996 821.0809 1444.361 1328.351 12854.30
## 3 1997 852.7407 1528.184 1439.975 12889.83
## 4 1998 870.6172 1643.047 1520.096 13154.76
## 5 1999 913.9634 1689.186 1556.334 13281.87
## 6 2000 992.9434 1800.468 1652.746 13671.05
tail(data_genis)
## year China Mexico Turkiye United States
## 6 2000 992.9434 1800.468 1652.746 13671.05
## 7 2001 1076.5491 1834.151 1612.999 13046.61
## 8 2002 1194.8563 1843.964 1667.364 13296.18
## 9 2003 1379.4846 2001.018 1771.827 13307.49
## 10 2004 1585.8388 1927.074 1891.841 13388.59
## 11 2005 1782.3122 1996.393 2013.889 13704.58
Net Electricity power consumption of all countries in the data set
df2 <- WDI(
country = "all",
indicator = "EG.USE.ELEC.KH.PC",
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=1995, frequency=1)
head(tsveri)
## year China Mexico Turkiye United States
## [1,] 1995 770.2802 1419.903 1227.327 12659.61
## [2,] 1996 821.0809 1444.361 1328.351 12854.30
## [3,] 1997 852.7407 1528.184 1439.975 12889.83
## [4,] 1998 870.6172 1643.047 1520.096 13154.76
## [5,] 1999 913.9634 1689.186 1556.334 13281.87
## [6,] 2000 992.9434 1800.468 1652.746 13671.05
tail(tsveri)
## year China Mexico Turkiye United States
## [6,] 2000 992.9434 1800.468 1652.746 13671.05
## [7,] 2001 1076.5491 1834.151 1612.999 13046.61
## [8,] 2002 1194.8563 1843.964 1667.364 13296.18
## [9,] 2003 1379.4846 2001.018 1771.827 13307.49
## [10,] 2004 1585.8388 1927.074 1891.841 13388.59
## [11,] 2005 1782.3122 1996.393 2013.889 13704.58
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 = "EG.USE.ELEC.KH.PC")
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()