I am calling WDI
library(WDI)
## Warning: package 'WDI' was built under R version 4.1.3
df = WDI(indicator='EN.ATM.CO2E.PC', country=c('MX','TR','CHN','US'), start=2000, end=2022)
head(df)
## country iso2c iso3c year EN.ATM.CO2E.PC
## 1 China CN CHN 2021 NA
## 2 China CN CHN 2020 NA
## 3 China CN CHN 2019 7.605937
## 4 China CN CHN 2018 7.487332
## 5 China CN CHN 2017 7.230985
## 6 China CN CHN 2016 7.115385
tail(df)
## country iso2c iso3c year EN.ATM.CO2E.PC
## 83 United States US USA 2005 19.46926
## 84 United States US USA 2004 19.59763
## 85 United States US USA 2003 19.50650
## 86 United States US USA 2002 19.44555
## 87 United States US USA 2001 20.17153
## 88 United States US USA 2000 20.46981
I’m installing “dplyr” from WDI
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
Rename Variables into columns so that the countries can be side by side
library(dplyr)
df<-df %>%
rename(countrycode = 1,
country = 2,
countrycodeiso = 3,
year = 4,
EN.ATM.CO2E.PC = 5 )
To monitor data broadly by Country. We need to install reshape2
library(reshape2)
## Warning: package 'reshape2' was built under R version 4.1.3
data_genis <- dcast(df, year ~ country, value.var="EN.ATM.CO2E.PC")
head(data_genis)
## year CN MX TR US
## 1 2000 2.650412 3.833980 3.421874 20.46981
## 2 2001 2.774761 3.777039 3.080902 20.17153
## 3 2002 2.975680 3.796046 3.194548 19.44555
## 4 2003 3.427437 3.925941 3.316114 19.50650
## 5 2004 3.954107 3.962113 3.351692 19.59763
## 6 2005 4.467700 4.077064 3.460796 19.46926
tail(data_genis)
## year CN MX TR US
## 17 2016 7.115385 3.837647 4.692472 15.14989
## 18 2017 7.230985 3.779372 5.127197 14.82326
## 19 2018 7.487332 3.586395 5.000359 15.22255
## 20 2019 7.605937 3.521600 4.756585 14.67341
## 21 2020 NA NA NA NA
## 22 2021 NA NA NA NA
To Download CO2 Emissions off All Countries
library(WDI)
df2 <- WDI(country = "all",
indicator = "EN.ATM.CO2E.PC",
start = 2000,
end = 2022,
extra = TRUE,
cache = NULL,
latest = NULL,
language = "tr")
## Warning in WDI(country = "all", indicator = "EN.ATM.CO2E.PC", start = 2000, :
## This language is only supported partially.
To Convert our “data_genis” to Time Series Use “tsveris”
tsveri <- ts(data_genis, start=2000, frequency=1)
head(tsveri)
## year CN MX TR US
## [1,] 2000 2.650412 3.833980 3.421874 20.46981
## [2,] 2001 2.774761 3.777039 3.080902 20.17153
## [3,] 2002 2.975680 3.796046 3.194548 19.44555
## [4,] 2003 3.427437 3.925941 3.316114 19.50650
## [5,] 2004 3.954107 3.962113 3.351692 19.59763
## [6,] 2005 4.467700 4.077064 3.460796 19.46926
tail(tsveri)
## year CN MX TR US
## [17,] 2016 7.115385 3.837647 4.692472 15.14989
## [18,] 2017 7.230985 3.779372 5.127197 14.82326
## [19,] 2018 7.487332 3.586395 5.000359 15.22255
## [20,] 2019 7.605937 3.521600 4.756585 14.67341
## [21,] 2020 NA NA NA NA
## [22,] 2021 NA NA NA NA
plot(tsveri[,2:5])
To Show It On The Same Graph We Have:
plot(tsveri[,2:5], plot.type = "single")
code To improve chart, as below we review the low.
plot(tsveri[,2],
type = "l",
col = 2,
ylim = c(0, 25),
xlab = "year",
ylab = "EN.ATM.CO2E.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)
GGPLOT
data_long <-melt(data_genis, id.vars = "year")
Installing ggplot2 and ggfortify
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
data_long <-melt(data_genis, id.vars = "year")
ggplot(data_long,
aes(x = year,
y = value,
col = variable)) +
geom_line()
## Warning: Removed 8 rows containing missing values (`geom_line()`).