Storytelling

The topic is about the unbalance of trading between America and China. Comparing U.S. and Chinese Trade deficit Data The size of the U.S. bilateral trade deficit with China has been continues to be an important issue in bilateral trade relations. President Trump and some Members of Congress view the deficit as a sign of unfair economic policies in China.

Data Source: Congressional Research Service

Load packages and prepare data

library(ggplot2)
## Registered S3 methods overwritten by 'ggplot2':
##   method         from 
##   [.quosures     rlang
##   c.quosures     rlang
##   print.quosures rlang
library(shiny)
library(dplyr)
## 
## 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
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
library(na.tools)
library(plotly)
## 
## 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
## Importing data and basic manipulations
data1 <- read.csv("The difference trading between America and China.csv")

data1_sum <- select(data1,Year, US.Export.to.China, US.Imports.from.China, Trade.Balance,
                      China.Exports.to.US, China.Imports.from.US, Trade.Balance.1)
head(data1_sum)
##   Year US.Export.to.China US.Imports.from.China Trade.Balance
## 1 2001             19.396               102.570       -83.174
## 2 2002             22.317               125.498      -103.181
## 3 2003             28.646               152.974      -124.328
## 4 2004             34.833               197.456      -162.623
## 5 2005             41.873               244.699      -202.825
## 6 2006             54.813               289.246      -234.433
##   China.Exports.to.US China.Imports.from.US Trade.Balance.1
## 1              54.277                26.204          28.073
## 2              69.959                27.228          42.731
## 3              92.510                33.883          58.627
## 4             124.973                44.653          80.320
## 5             162.939                48.735         114.204
## 6             203.516                59.222         144.294
## update indicator varaible menu
updatemenus <- list(
  list(
    active = 0,
    x = -.125,
    type = 'buttons',
    buttons = list(
      list(
        label = "USA Export to China",
        method = "update",
        args = list(list(visible = c(TRUE, "legendonly", "legendonly", 
                                     "legendonly", "legendonly" )))),
      list(
        label = "USA Imports from China",
        method = "update",
        args = list(list(visible = c("legendonly", TRUE, "legendonly", 
                                     "legendonly", "legendonly")))),
      list(
        label = " USA Trade Balance",
        method = "update",
        args = list(list(visible = c("legendonly", "legendonly", TRUE, 
                                     "legendonly", "legendonly")))),
      list(
        label = "China Exports to USA",
        method = "update",
        args = list(list(visible = c("legendonly", "legendonly", "legendonly", 
                                     TRUE, "legendonly")))),      
      list(
        label = "China Imports from USA",
        method = "update",
        args = list(list(visible = c("legendonly", "legendonly", "legendonly", 
                                     "legendonly", TRUE)))),
      list(
        label = "China Trade Balance ",
        method = "update",
        args = list(list(visible = c("legendonly", "legendonly", "legendonly", 
                                     "legendonly", TRUE))))
      
    )
  )
)
## Using plot_ly () to plot Bar Chart and get interactive Storytelling effectiness
p1 <- plot_ly(data = data1_sum) %>% 
  add_lines(x=data1$Year, y = ~data1$US.Export.to.China,    text = "USA Export to China",     visible = "TRUE") %>%
  add_lines(x=data1$Year, y = ~data1$US.Imports.from.China, text = "USA Imports from China",  visible = "legendonly") %>%
  add_lines(x=data1$Year, y = ~data1$Trade.Balance,         text = "USA-China Trade Balance", visible = "legendonly") %>%
  add_lines(x=data1$Year, y = ~data1$China.Exports.to.US,   text = "China Exports to USA",    visible = "legendonly") %>%
  add_lines(x=data1$Year, y = ~data1$China.Imports.from.US, text = "China Imports from USA",  visible = "legendonly") %>%
  add_lines(x=data1$Year, y = ~data1$Trade.Balance.1,       text = "China-USA Trade Balance", visible = "legendonly") %>%
  
  layout(title = " 2001-2017 
                   U.S. and Chinese Merchandise Trade Figures, $ unit billion ", showlegend=TRUE,
         xaxis=list(zeroline = FALSE,title=" Year"),
         yaxis=list(zeroline = FALSE,title="Count $ unit: billion"),
         updatemenus=updatemenus)

p1
## Visulisation and tell the story about "Top Five Discrepancies for U.S. Imports from China, 2017"
# Reading data 
 data2 <- read.csv("Top five Discrepancies for U.S Imports from China.csv")

 head(data2)
##                      HS.Chapter U.S..Imports.from.China
## 1            Electrical Machina                 146.997
## 2                     Machinery                 109.629
## 3       Toys and Sporting goods                  25.524
## 4 Optical and Medical Equipment                  11.958
## 5                       Footwar                  14.255
##   China.Exports.to.U.S Difference
## 1              107.120     39.878
## 2               91.429     18.200
## 3               19.042      6.482
## 4                9.660      2.298
## 5               12.037      2.219
## Roatated Bar Chart Labels and interactive storytelling effectiness
  x= c('U.S..Imports.from.China', 'China.Exports.to.U.S', 'Difference')
  y1 <- c(146.997, 107.120, 39.878)
  y2 <- c(109.629,  91.429, 18.200)
  y3 <- c( 25.524,  19.042,  6.482)
  y4 <- c( 11.958,   9.660,  2.298)
  y5 <- c( 14.255,  12.037,  2.219)
  data <- data.frame(x,y1,y2,y3,y4,y5)
# The default order will be alphabetized unless specified as below:
  data$x <- factor(data$x, levels = data[["x"]])
  
  p2 <- plot_ly(data, x = ~x, y = ~y1, type = 'bar', name = 'Electrical Machine', text= 'Electrical Machine', 
                                                              marker = list(color = 'rgb(49,130,189)'))%>%
    add_trace(y = ~y2, name = 'Machinery',text = 'Machinery', marker = list(color ='rgb(204,204,204)'))%>% 
    add_trace(y = ~y3, name = 'Toys and Sporting goods', text = 'Toys and Sporting goods', 
                                                              marker = list(color ='rgb(green)'))%>% 
    add_trace(y = ~y4, name = 'Optical and Medical Equipment ', text = 'Optical and Medical Equipment', 
                                                              marker = list(color ='rgb(yellow)'))%>% 
    add_trace(y = ~y5, name = ' Footware', text = 'Footware', marker = list(color ='rgb(red)'))%>% 
    
    layout(title = "Year 2017 
                   Top Five Discrepancies for U.S. Imports from China, $ unit: billion ", showlegend=TRUE,
           xaxis = list(title = "Products", tichange = -45 ),
           yaxis = list(title = " Count $ unit: billion "),
           barmode = 'group')
           
    
  p2
data2 <- read.csv("Top five Discrepancies for U.S Imports from China.csv")

head(data2)
##                      HS.Chapter U.S..Imports.from.China
## 1            Electrical Machina                 146.997
## 2                     Machinery                 109.629
## 3       Toys and Sporting goods                  25.524
## 4 Optical and Medical Equipment                  11.958
## 5                       Footwar                  14.255
##   China.Exports.to.U.S Difference
## 1              107.120     39.878
## 2               91.429     18.200
## 3               19.042      6.482
## 4                9.660      2.298
## 5               12.037      2.219
X = c('U.S..Imported.from.China', 'U.S.Expoted.to.China')
y1 <- c(186.5, 17.9)
  y2 <- c( 49.9,  3.4)
  y3 <- c( 44.0,  3.7)
  y4 <- c( 38.7, 11.1)
  y5 <- c( 29.8,  0.0)
  y6 <- c( 26.5,  0.0)
  y7 <- c( 25.8,  0.0)
  y8 <- c( 21.7, 27.8)
  y9 <- c( 21.4, 16.2)
  y10 <-c( 20.2,  0.0)
  y11 <-c( 20.0,  0.0)
  y12 <-c(  0.0,  7.1)
  y13 <-c(  0.0,  5.9)
  y14 <-c(  0.0,  3.4)
  
  data <- data.frame(X,y1,y2,y3,y4,y4,y5,y6,y7,y8,y9,y10,y11,y12,y13,y14)
 #The default order will be alphabetized unless specified as below:
  data$X <- factor(data$X, levels = data[["X"]])
   p3 <- plot_ly(data, x = ~X, y = ~y1, type = 'bar', name = ' Computers $ Electronics', 
                marker = list(color = 'green'))%>%
    add_trace(y = ~y2, name = 'Electrical Equipment', marker = list(color ='yellow'))%>% 
    add_trace(y = ~y3, name = 'Miscellaneous Manufacturing', marker = list(color ='rgb(55,83,109)'))%>% 
    add_trace(y = ~y4, name = 'Machinaery', marker = list(color ='rgb(26,118,255)'))%>% 
    add_trace(y = ~y5, name = 'Clothing', marker = list(color ='rgb(107,107,107)'))%>% 
    add_trace(y = ~y6,  name = 'Fabricated Metal',          marker = list(color ='rgb(204,204,204)'))%>% 
    add_trace(y = ~y7,  name = 'Furniture',                 marker = list(color ='rgb( 55, 83,109)'))%>% 
    add_trace(y = ~y8,  name = 'Transportation Equipment',  marker = list(color ='rgb( 26,118,255)'))%>% 
    add_trace(y = ~y9,  name =  'Chemicals',                marker = list(color ='rgb(107,107,107)'))%>% 
    add_trace(y = ~y10, name = 'Plastic $ rubber Products', marker = list(color ='rgb( 22, 96,167)'))%>% 
    add_trace(y = ~y11, name = 'Leather and Similar Goods', marker = list(color ='rgb(205, 12, 24)'))%>% 
    add_trace(y = ~y12, name = 'Oil $ Gas',                 marker = list(color ='rgb( 26,118,255)'))%>% 
    add_trace(y = ~y13, name = 'Farm Crops',                marker = list(color ='rgb(107,107,107)'))%>% 
    add_trace(y = ~y14, name = 'Waste $ Scrap',             marker = list(color ='rgb( 49,130,189)'))%>% 
    
    
    layout(title = "U.S Imported $ 539 billion in goods from China. 
           U.S. Exported $120.3 billion in Goods to China ", showlegend=TRUE,
           xaxis = list(title = "Products", tichange = -45 ),
           yaxis = list(title = " Count $ unit: billion "),
           barmode = 'group')
  p3