library(readr)
library(ggplot2)
## Registered S3 methods overwritten by 'ggplot2':
## method from
## [.quosures rlang
## c.quosures rlang
## print.quosures rlang
library(tidyverse)
## -- Attaching packages ------------------------------------------------------- tidyverse 1.2.1 --
## v tibble 2.1.3 v dplyr 0.8.1
## v tidyr 0.8.3 v stringr 1.4.0
## v purrr 0.3.2 v forcats 0.4.0
## -- Conflicts ---------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(dplyr)
library(plotly)
## Warning: package 'plotly' was built under R version 3.6.1
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(gapminder)
## Warning: package 'gapminder' was built under R version 3.6.1
library(RColorBrewer)
library(reshape)
##
## Attaching package: 'reshape'
## The following object is masked from 'package:plotly':
##
## rename
## The following object is masked from 'package:dplyr':
##
## rename
## The following objects are masked from 'package:tidyr':
##
## expand, smiths
library(quantmod)
## Warning: package 'quantmod' was built under R version 3.6.1
## Loading required package: xts
## Warning: package 'xts' was built under R version 3.6.1
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Registered S3 method overwritten by 'xts':
## method from
## as.zoo.xts zoo
##
## Attaching package: 'xts'
## The following objects are masked from 'package:dplyr':
##
## first, last
## Loading required package: TTR
## Warning: package 'TTR' was built under R version 3.6.1
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Version 0.4-0 included new data defaults. See ?getSymbols.
library(scales)
##
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
##
## discard
## The following object is masked from 'package:readr':
##
## col_factor
To check out the data set
tradeko <- read_csv(file = "trade_table.csv")
## Warning: Missing column names filled in: 'X1' [1]
## Parsed with column specification:
## cols(
## X1 = col_double(),
## year = col_double(),
## country = col_character(),
## export_weight = col_double(),
## import_weight = col_double(),
## export_amount = col_double(),
## import_amount = col_double(),
## trade_balance = col_double(),
## HS_code = col_double(),
## description = col_character()
## )
head(tradeko, 4)
## # A tibble: 4 x 10
## X1 year country export_weight import_weight export_amount
## <dbl> <dbl> <chr> <dbl> <dbl> <dbl>
## 1 1 2013 Slovak~ 0 0 0
## 2 2 2013 Austria 0 0.1 1
## 3 3 2013 Romania 0 0.2 1
## 4 4 2013 Poland 0 0.2 1
## # ... with 4 more variables: import_amount <dbl>, trade_balance <dbl>,
## # HS_code <dbl>, description <chr>
dim(tradeko)
## [1] 13671 10
str(tradeko)
## Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'data.frame': 13671 obs. of 10 variables:
## $ X1 : num 1 2 3 4 5 6 7 8 9 10 ...
## $ year : num 2013 2013 2013 2013 2013 ...
## $ country : chr "Slovakia" "Austria" "Romania" "Poland" ...
## $ export_weight: num 0 0 0 0 1 0 0 0 0 0 ...
## $ import_weight: num 0 0.1 0.2 0.2 29.4 0 0.4 0 0.2 0.2 ...
## $ export_amount: num 0 1 1 1 106 0 0 1 0 0 ...
## $ import_amount: num 0 6 2 5 1511 ...
## $ trade_balance: num 0 -5 -2 -4 -1404 ...
## $ HS_code : num 1 1 1 1 1 1 1 1 1 1 ...
## $ description : chr "LIVE ANIMALS" "LIVE ANIMALS" "LIVE ANIMALS" "LIVE ANIMALS" ...
## - attr(*, "spec")=
## .. cols(
## .. X1 = col_double(),
## .. year = col_double(),
## .. country = col_character(),
## .. export_weight = col_double(),
## .. import_weight = col_double(),
## .. export_amount = col_double(),
## .. import_amount = col_double(),
## .. trade_balance = col_double(),
## .. HS_code = col_double(),
## .. description = col_character()
## .. )
To show summary of the sum of export and imprort per year and country by using group_by
tradeko2 <- tradeko %>% group_by(year,country) %>% summarise(export = sum(export_amount), import = sum(import_amount))
exportsum5yr <- summary(tradeko2$export)
importsum5yr <- summary(tradeko2$import)
head(tradeko2)
## # A tibble: 6 x 4
## # Groups: year [1]
## year country export import
## <dbl> <chr> <dbl> <dbl>
## 1 2013 Austria 606901 1382573
## 2 2013 Belgium 2240762 1585908
## 3 2013 Bulgaria 120364 171894
## 4 2013 Croatia 46307 16603
## 5 2013 Cyprus 66369 1299334
## 6 2013 Czech Republic 1682863 557478
exportsum5yr
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 10705 326406 889878 1653788 2433543 8483848
importsum5yr
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 7010 112328 598460 1888967 1587345 21298751
Box Plot - Sum of Trade from 2013 to 2018 (with 24 Trade partners in EU)
First box plot - export/10000 was used to properly show the Y axis numbers. I summed the imports for a country in excel and verified the value is correct.
Second box plot - exactly same as the first box plot but for imports
tradebox <- ggplot(tradeko2, aes(x = country, y = (export/10000), fill = country)) +
geom_boxplot(outlier.colour="black", outlier.shape=1, outlier.size=1) +
theme(axis.text.x = element_text(angle = 70, hjust = 0.9)) +
ggtitle("Sum of Export from 2013 to 2018") +
xlab(" ") +
ylab("Export: USD $billion") +
theme(legend.position = "right")
tradebox <- ggplotly(tradebox)
tradebox
tradebox1 <- ggplot(tradeko2, aes(x = country, y = (import/10000), fill = country)) +
geom_boxplot(outlier.colour="black", outlier.shape=1, outlier.size=1) +
theme(axis.text.x = element_text(angle = 70, hjust = 0.9)) +
ggtitle("Sum of Import from 2013 to 2018") +
xlab("Country") +
ylab("Import: USD $billion") +
theme(legend.position = "right")
tradebox1 <- ggplotly(tradebox1)
tradebox1
Trade balance per year and country by using group_by
tradeko1 <- tradeko %>% group_by(year, country) %>% summarise(balance = mean(trade_balance))
head(tradeko1, 3)
## # A tibble: 3 x 3
## # Groups: year [1]
## year country balance
## <dbl> <chr> <dbl>
## 1 2013 Austria -8431.
## 2 2013 Belgium 6967.
## 3 2013 Bulgaria -613.
balance5yravg <- summary(tradeko1$balance)
balance5yravg
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -153503 -5648 801 -1593 9526 54811
library(ggthemes)
## Warning: package 'ggthemes' was built under R version 3.6.1
library(tibbletime)
## Warning: package 'tibbletime' was built under R version 3.6.1
##
## Attaching package: 'tibbletime'
## The following object is masked from 'package:stats':
##
## filter
library(gganimate)
## Warning: package 'gganimate' was built under R version 3.6.1
Line Chart
This chart shows average trade balance.
tradeline <- ggplot(tradeko1,
aes(x = year, y = (balance/10000), color = country, group = 1)
) +
theme_economist() +
geom_line() +
geom_point(
aes(text =
paste(paste("Rate: ", tradeko1$country, "<br>"),
paste("Year: ", tradeko1$year, "<br>"),
paste("Trade Balance: ", tradeko1$balance/10000))
),
size = 2,
data = tradeko1) +
scale_x_continuous(breaks=seq(2013, 2018, 1)) +
scale_color_pander() +
labs(x="Year", y="USD $billion") +
ggtitle("Average Trade Balance by Country")
## Warning: Ignoring unknown aesthetics: text
tradeline1 <- ggplotly(tradeline, tooltip = "text")
tradeline1