library(tidyverse) #Data wrangling
library(plotly) #Interactive plot
library(WDI) #World Bank data access
library(plyr)
WDIsearch("gdp.*capita.*constant") %>% head()
## indicator
## [1,] "6.0.GDPpc_constant"
## [2,] "NY.GDP.PCAP.PP.KD.87"
## [3,] "NY.GDP.PCAP.PP.KD"
## [4,] "NY.GDP.PCAP.KN"
## [5,] "NY.GDP.PCAP.KD"
## name
## [1,] "GDP per capita, PPP (constant 2011 international $) "
## [2,] "GDP per capita, PPP (constant 1987 international $)"
## [3,] "GDP per capita, PPP (constant 2011 international $)"
## [4,] "GDP per capita (constant LCU)"
## [5,] "GDP per capita (constant 2010 US$)"
WDIsearch("gdp.*growth") %>% head()
## indicator name
## [1,] "6.0.GDP_growth" "GDP growth (annual %)"
## [2,] "5.51.01.10.gdp" "Per capita GDP growth"
## [3,] "NY.GDP.PCAP.PP.KD.ZG" "GDP per capita, PPP annual growth (%)"
## [4,] "NY.GDP.PCAP.KD.ZG" "GDP per capita growth (annual %)"
## [5,] "NY.GDP.MKTP.KN.87.ZG" "GDP growth (annual %)"
## [6,] "NY.GDP.MKTP.KD.ZG" "GDP growth (annual %)"
WDIsearch("gdp.*current") %>% head()
## indicator name
## [1,] "6.0.GDP_current" "GDP (current $)"
## [2,] "NY.GDP.PCAP.PP.CD" "GDP per capita, PPP (current international $)"
## [3,] "NY.GDP.PCAP.CN" "GDP per capita (current LCU)"
## [4,] "NY.GDP.PCAP.CD" "GDP per capita (current US$)"
## [5,] "NY.GDP.MKTP.PP.CD" "GDP, PPP (current international $)"
## [6,] "NY.GDP.MKTP.CN.AD" "GDP: linked series (current LCU)"
gdp_capita <-
WDI(indicator = "NY.GDP.PCAP.KD", #GDP per capita (constant 2010 US$)
country = c("CN", "HK"),
start = 1960, end = 2017)
gdp_growth <-
WDI(indicator = "NY.GDP.MKTP.KD.ZG", #GDP per capita growth (annual %)
country = c("CN", "HK"),
start = 1960, end = 2017)
gdp_current <-
WDI(indicator = "NY.GDP.MKTP.CD", #GDP (current US$)
country = c("CN", "HK"),
start = 1960, end = 2017)
gdp_capita <- gdp_capita[-2]
colnames(gdp_capita)[1] <- "country"
colnames(gdp_capita)[2] <- "gdp_capita"
gdp_current <- gdp_current[-2]
colnames(gdp_current)[1] <- "country"
colnames(gdp_current)[2] <- "gdp_current"
gdp_growth <- gdp_growth[-2]
colnames(gdp_growth)[1] <- "country"
colnames(gdp_growth)[2] <- "gdp_growth"
comp_gdp <-
gdp_current %>%
spread("country", "gdp_current") %>%
mutate(proportion = HK/CN)
comp_gdp$handover <- ifelse(comp_gdp[1] > 1998, 1, 0)
ggplot2: handy visualisation tool for creating static graphs.
Not a main topic of this article.
gdp_capita %>%
ggplot(aes(x = year, y = gdp_capita, color = country)) +
geom_line()
gdp_growth %>%
ggplot(aes(x = year, y = gdp_growth, color = country)) +
geom_line()
gdp_current %>%
ggplot(aes(x = year, y = gdp_current, color = country)) +
geom_line()
comp_gdp_plotly <-
comp_gdp %>%
select(year, proportion, handover) %>%
spread("handover", "proportion")
colnames(comp_gdp_plotly)[c(2, 3)] <- c('before', 'after')
plot_ly(
comp_gdp_plotly,
x = ~year,
y = ~before,
name = "UK rule",
type = "bar"
) %>%
add_trace(y = ~after, name = "Post Handover") %>%
layout(yaxis = list(tickformat = "%")) %>%
layout(title = "Relative Importance of Hong Kong in total Chinese Economy(in 2019 USD)",
xaxis = list(title = "Year"),
yaxis = list (title = "HK_GDP / CN_GDP in current USD"))
gdp_capita <-
gdp_capita %>%
spread("country", "gdp_capita")
plot_ly(gdp_capita,
x = ~year,
y = ~CN,
type = 'bar',
name = "China",
marker = list(color = 'red')) %>%
add_trace(
y = ~HK,
name = "Hong Kong",
marker = list(color = 'rgb(26, 118, 255')
) %>%
layout(title = 'GDP per capita comparison: China and Hong Kong (in 2010 USD)',
yaxis = list (title = "Real GDP per capita"),
xaxis = list (title = "Year"))
gdp_current <-
gdp_current %>%
spread("country", "gdp_current")
plot_ly(gdp_current,
x = ~year,
y = ~CN,
type = 'bar',
name = "China",
marker = list(color = 'red')) %>%
add_trace(
y = ~HK,
name = "Hong Kong",
marker = list(color = 'rgb(26, 118, 255')
) %>%
layout(title = 'Total GDP comparison: China and Hong Kong (in 2019 USD)',
yaxis = list (title = "Real GDP"),
xaxis = list (title = "Year"))
#annual, nominal
gdp_growth <-
gdp_growth %>%
spread("country", "gdp_growth")
plot_ly(gdp_growth,
x = ~year,
y = ~CN,
name = 'China',
type = 'scatter', mode = 'lines',
line = list(color = 'rgb(205, 12, 24)', width = 4)) %>%
add_trace(y = ~HK,
name = 'Hong Kong',
line = list(color = 'rgb(22, 96, 167)', width = 4)) %>%
layout(title = "Growth rate comparison (annual%, nominal)",
xaxis = list(title = "Year"),
yaxis = list (title = "Growth rate")) %>%
layout(yaxis = list(ticksuffix = "%"))
To sum up,