Installing WDI
library(WDI)
## Warning: package 'WDI' was built under R version 4.1.3
df = WDI(indicator='AG.LND.FRST.K2', country=c('MX','TR','CHN','US'), start=2000, end=2022)
head(df)
## country iso2c iso3c year AG.LND.FRST.K2
## 1 China CN CHN 2021 NA
## 2 China CN CHN 2020 2199782
## 3 China CN CHN 2019 2180986
## 4 China CN CHN 2018 2162190
## 5 China CN CHN 2017 2143395
## 6 China CN CHN 2016 2124599
tail(df)
## country iso2c iso3c year AG.LND.FRST.K2
## 83 United States US USA 2005 3061280
## 84 United States US USA 2004 3056096
## 85 United States US USA 2003 3050912
## 86 United States US USA 2002 3045728
## 87 United States US USA 2001 3040544
## 88 United States US USA 2000 3035360
Renaming Variables into Columns
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,
countrycode = 2,
countrycodeiso3 = 3,
year = 4,
AG.LND.FRST.K2 =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="AG.LND.FRST.K2")
head(data_genis)
## year China Mexico Turkiye United States
## 1 2000 1770006 683813.5 201483.5 3035360
## 2 2001 1793615 682375.5 202418.2 3040544
## 3 2002 1817225 680937.4 203353.0 3045728
## 4 2003 1840835 679499.4 204287.7 3050912
## 5 2004 1864445 678061.3 205222.4 3056096
## 6 2005 1888055 676623.3 206157.1 3061280
tail(data_genis)
## year China Mexico Turkiye United States
## 17 2016 2124599 662031.5 216303.0 3100950
## 18 2017 2143395 660753.8 217524.6 3097950
## 19 2018 2162190 659476.1 219083.6 3097950
## 20 2019 2180986 658198.5 220643.6 3097950
## 21 2020 2199782 656920.8 222203.6 3097950
## 22 2021 NA NA NA NA
Forest Areas of all Countries data set
df2 <- WDI(
country = "all",
indicator = "AG.LND.FRST.K2",
start = 2010,
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
To Convert our “data_genis” to Time Series Use “tsveris”
tsveri <- ts(data_genis, start=2000, frequency=1)
head(tsveri)
## year China Mexico Turkiye United States
## [1,] 2000 1770006 683813.5 201483.5 3035360
## [2,] 2001 1793615 682375.5 202418.2 3040544
## [3,] 2002 1817225 680937.4 203353.0 3045728
## [4,] 2003 1840835 679499.4 204287.7 3050912
## [5,] 2004 1864445 678061.3 205222.4 3056096
## [6,] 2005 1888055 676623.3 206157.1 3061280
tail(tsveri)
## year China Mexico Turkiye United States
## [17,] 2016 2124599 662031.5 216303.0 3100950
## [18,] 2017 2143395 660753.8 217524.6 3097950
## [19,] 2018 2162190 659476.1 219083.6 3097950
## [20,] 2019 2180986 658198.5 220643.6 3097950
## [21,] 2020 2199782 656920.8 222203.6 3097950
## [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 = "AG.LND.FRST")
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 = 2,
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 4 rows containing missing values (`geom_line()`).