R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(ggplot2)
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(maps)
library(tibble)
library(tidyr)
library(RColorBrewer)


#Load Dataset

trade_data <- read.csv("Merged.csv")
head(trade_data)
##   Country Partner.Name Trade.Flow  Product.Group             Indicator Year
## 1   China   Bangladesh     Import   All Products Import (US$ Thousand) 1996
## 2   China   Bangladesh     Import   All Products Import (US$ Thousand) 1996
## 3   China         Iran     Import   All Products Import (US$ Thousand) 1996
## 4   China         Iran     Import   All Products Import (US$ Thousand) 1996
## 5   China         Iran     Import   All Products Import (US$ Thousand) 1996
## 6   China         Iran     Import   All Products Import (US$ Thousand) 1996
##      Amount SIPRI.AT.Database.ID Supplier  Recipient   Designation
## 1  34217.72                27731    China Bangladesh F-7M Airguard
## 2  34217.72                27731    China Bangladesh F-7M Airguard
## 3 386141.35                32698    China       Iran         JY-14
## 4 386141.35                32698    China       Iran         JY-14
## 5 386141.35                32698    China       Iran         JY-14
## 6 386141.35                39941    China       Iran   Type-86 APC
##        Description Armament.category Order.date Order.date.is.estimate
## 1 fighter aircraft          Aircraft       1996                    Yes
## 2 fighter aircraft          Aircraft       1996                    Yes
## 3 air search radar           Sensors       1996                    Yes
## 4 air search radar           Sensors       1996                    Yes
## 5 air search radar           Sensors       1996                    Yes
## 6              APC Armoured vehicles       1996                    Yes
##   Numbers.delivered Numbers.delivered.is.estimate Delivery.year
## 1                 3                            No          1999
## 2                 1                            No          2000
## 3                 1                           Yes          1999
## 4                 1                           Yes          2000
## 5                 1                           Yes          2001
## 6                10                           Yes          2001
##   Delivery.year.is.estimate Status SIPRI.estimate TIV.deal.unit
## 1                        No    New           11.0          11.0
## 2                        No    New           11.0          11.0
## 3                       Yes    New           13.5          13.5
## 4                       Yes    New           13.5          13.5
## 5                       Yes    New           13.5          13.5
## 6                        No    New            0.3           0.3
##   TIV.delivery.values Local.production TII TII.Score
## 1                33.0               No TII  4.128203
## 2                11.0               No TII  4.128203
## 3                13.5               No TII  1.198521
## 4                13.5               No TII  1.198521
## 5                13.5               No TII  1.198521
## 6                 3.0              Yes TII  1.198521
# Coordinates


region_mapping <- tibble(
  Region = c("North America", "North America", "North America", "South and Central America", "Europe & Eurasia", "Asia", "Asia",
             "South and Central America", "Asia", "Asia", "Africa", "Europe & Eurasia", "Africa", "Middle East",  "Europe & Eurasia", "Middle East", 
             "Africa", "Asia Pacific", "Europe & Eurasia"),
  Country = c("United States", "Canada", "Mexico", "Brazil", "Germany", "China", "India",
              "Ecuador","Bangladesh", "Pakistan","Nigeria", "Netherlands", "Kenya", "Yemen", "United Kingdom", "Saudi Arabia",
              "South Africa", "Australia", "Russia"),
  Latitude = c(37.0902, 56.130366, 23.634501, -14.235004, 51.165691, 35.8617, 20.5937,
               -1.8312, 23.6850, 30.3753,9.0820, 52.1326, -1.2864, 15.552727, 55.378051,23.885942,
               -30.559482, -25.274398, 61.52401),
  Longitude = c(-95.7129,  -106.346771, -102.552784, -51.92528, 10.451526, 104.1954, 78.96288,
                -78.1834, 90.3563, 69.3451, 8.6753, 5.2913,37.9062, 48.5164,-3.4359, 45.0792,
                22.937506, 133.775136, 105.318756))

trade_data_coords <- trade_data %>%
  left_join(region_mapping, by = c("Supplier" = "Country")) %>%
  rename(Export_Lat = Latitude, Export_Lon = Longitude, Region_Export = Supplier) %>%
  left_join(region_mapping, by = c("Recipient" = "Country")) %>%
  rename(Import_Lat = Latitude, Import_Lon = Longitude, Region_Import = Recipient)


head(trade_data_coords)
##   Country Partner.Name Trade.Flow  Product.Group             Indicator Year
## 1   China   Bangladesh     Import   All Products Import (US$ Thousand) 1996
## 2   China   Bangladesh     Import   All Products Import (US$ Thousand) 1996
## 3   China         Iran     Import   All Products Import (US$ Thousand) 1996
## 4   China         Iran     Import   All Products Import (US$ Thousand) 1996
## 5   China         Iran     Import   All Products Import (US$ Thousand) 1996
## 6   China         Iran     Import   All Products Import (US$ Thousand) 1996
##      Amount SIPRI.AT.Database.ID Region_Export Region_Import   Designation
## 1  34217.72                27731         China    Bangladesh F-7M Airguard
## 2  34217.72                27731         China    Bangladesh F-7M Airguard
## 3 386141.35                32698         China          Iran         JY-14
## 4 386141.35                32698         China          Iran         JY-14
## 5 386141.35                32698         China          Iran         JY-14
## 6 386141.35                39941         China          Iran   Type-86 APC
##        Description Armament.category Order.date Order.date.is.estimate
## 1 fighter aircraft          Aircraft       1996                    Yes
## 2 fighter aircraft          Aircraft       1996                    Yes
## 3 air search radar           Sensors       1996                    Yes
## 4 air search radar           Sensors       1996                    Yes
## 5 air search radar           Sensors       1996                    Yes
## 6              APC Armoured vehicles       1996                    Yes
##   Numbers.delivered Numbers.delivered.is.estimate Delivery.year
## 1                 3                            No          1999
## 2                 1                            No          2000
## 3                 1                           Yes          1999
## 4                 1                           Yes          2000
## 5                 1                           Yes          2001
## 6                10                           Yes          2001
##   Delivery.year.is.estimate Status SIPRI.estimate TIV.deal.unit
## 1                        No    New           11.0          11.0
## 2                        No    New           11.0          11.0
## 3                       Yes    New           13.5          13.5
## 4                       Yes    New           13.5          13.5
## 5                       Yes    New           13.5          13.5
## 6                        No    New            0.3           0.3
##   TIV.delivery.values Local.production TII TII.Score Region.x Export_Lat
## 1                33.0               No TII  4.128203     Asia    35.8617
## 2                11.0               No TII  4.128203     Asia    35.8617
## 3                13.5               No TII  1.198521     Asia    35.8617
## 4                13.5               No TII  1.198521     Asia    35.8617
## 5                13.5               No TII  1.198521     Asia    35.8617
## 6                 3.0              Yes TII  1.198521     Asia    35.8617
##   Export_Lon Region.y Import_Lat Import_Lon
## 1   104.1954     Asia     23.685    90.3563
## 2   104.1954     Asia     23.685    90.3563
## 3   104.1954     <NA>         NA         NA
## 4   104.1954     <NA>         NA         NA
## 5   104.1954     <NA>         NA         NA
## 6   104.1954     <NA>         NA         NA
trade_data_clean = trade_data_coords %>%
  filter(!is.na(Region_Export), !is.na(Region_Import)) %>%
  mutate(TradeRoute = paste(Region_Export, "to", Region_Import)) %>%
  filter(!is.na(Export_Lon), !is.na(Export_Lat), !is.na(Import_Lon), !is.na(Import_Lat))  %>%
  filter(!(Export_Lon == Import_Lon & Export_Lat == Import_Lat))


View(trade_data_clean)


# create map
world <- map_data("world")

world_regions <- world %>%
  mutate(Region = case_when(
    region %in% c("USA", "Canada", "Mexico") ~ "North America",
    region %in% c("Brazil", "Argentina", "Ecuador") ~ "South and Central America",
    region %in% c("Germany", "Russia", "France", "Netherlands", "UK") ~ "Europe & Eurasia",
    region %in% c("China", "India", "Bangladesh", "Pakistan") ~ "Asia",
    region %in% c("Japan", "Australia") ~ "Asia Pacific",
    region %in% c("Nigeria", "South Africa", "Kenya") ~ "Africa",
    region %in% c("Saudi Arabia", "Yemen") ~ "Middle East",
    TRUE ~ "Other"
  ))
# Color palette for regions
region_colors <- c(
  "North America" = "mistyrose1",
  "South and Central America" = "tomato",
  "Europe & Eurasia" = "lightskyblue3",
  "Asia" = "antiquewhite",
  "Asia Pacific" = "sandybrown",
  "Middle East" = "pink",
  "Africa" = "aquamarine",
  "Other" = "gray90")

summary(trade_data_clean)
##    Country          Partner.Name        Trade.Flow        Product.Group     
##  Length:3122        Length:3122        Length:3122        Length:3122       
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##                                                                             
##   Indicator              Year          Amount          SIPRI.AT.Database.ID
##  Length:3122        Min.   :1996   Min.   :     1410   Min.   :17856       
##  Class :character   1st Qu.:2001   1st Qu.:  1610552   1st Qu.:34289       
##  Mode  :character   Median :2007   Median :  5596013   Median :50209       
##                     Mean   :2007   Mean   : 18113453   Mean   :44862       
##                     3rd Qu.:2012   3rd Qu.: 18960148   3rd Qu.:55380       
##                     Max.   :2021   Max.   :568174902   Max.   :66265       
##                                    NA's   :56          NA's   :104         
##  Region_Export      Region_Import      Designation        Description       
##  Length:3122        Length:3122        Length:3122        Length:3122       
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##                                                                             
##  Armament.category    Order.date   Order.date.is.estimate Numbers.delivered
##  Length:3122        Min.   :1996   Length:3122            Min.   :    0.0  
##  Class :character   1st Qu.:2001   Class :character       1st Qu.:    5.0  
##  Mode  :character   Median :2007   Mode  :character       Median :   16.0  
##                     Mean   :2007                          Mean   :  111.8  
##                     3rd Qu.:2012                          3rd Qu.:   70.0  
##                     Max.   :2021                          Max.   :10000.0  
##                                                           NA's   :52       
##  Numbers.delivered.is.estimate Delivery.year  Delivery.year.is.estimate
##  Length:3122                   Min.   :   0   Length:3122              
##  Class :character              1st Qu.:2007   Class :character         
##  Mode  :character              Median :2013   Mode  :character         
##                                Mean   :1978                            
##                                3rd Qu.:2017                            
##                                Max.   :2022                            
##                                NA's   :52                              
##     Status          SIPRI.estimate    TIV.deal.unit     TIV.delivery.values
##  Length:3122        Min.   :   0.00   Min.   :   0.00   Min.   :   0.00    
##  Class :character   1st Qu.:   0.25   1st Qu.:   0.20   1st Qu.:   7.70    
##  Mode  :character   Median :   1.40   Median :   1.40   Median :  24.00    
##                     Mean   :  14.07   Mean   :  13.65   Mean   :  75.50    
##                     3rd Qu.:   5.00   3rd Qu.:   5.00   3rd Qu.:  65.25    
##                     Max.   :1250.00   Max.   :1250.00   Max.   :1950.00    
##                     NA's   :52        NA's   :52        NA's   :52         
##  Local.production       TII              TII.Score        Region.x        
##  Length:3122        Length:3122        Min.   :0.1089   Length:3122       
##  Class :character   Class :character   1st Qu.:0.9335   Class :character  
##  Mode  :character   Mode  :character   Median :1.2742   Mode  :character  
##                                        Mean   :1.4163                     
##                                        3rd Qu.:1.7969                     
##                                        Max.   :4.1282                     
##                                                                           
##    Export_Lat      Export_Lon       Region.y           Import_Lat   
##  Min.   :35.86   Min.   :-95.71   Length:3122        Min.   :20.59  
##  1st Qu.:37.09   1st Qu.:-95.71   Class :character   1st Qu.:20.59  
##  Median :37.09   Median :104.20   Mode  :character   Median :23.89  
##  Mean   :47.95   Mean   : 44.26                      Mean   :27.34  
##  3rd Qu.:61.52   3rd Qu.:105.32                      3rd Qu.:30.38  
##  Max.   :61.52   Max.   :105.32                      Max.   :61.52  
##                                                                     
##    Import_Lon      TradeRoute       
##  Min.   :-95.71   Length:3122       
##  1st Qu.: 69.35   Class :character  
##  Median : 78.96   Mode  :character  
##  Mean   : 73.38                     
##  3rd Qu.: 90.36                     
##  Max.   :105.32                     
## 
head(trade_data_clean)
##   Country  Partner.Name Trade.Flow  Product.Group             Indicator Year
## 1   China    Bangladesh     Import   All Products Import (US$ Thousand) 1996
## 2   China    Bangladesh     Import   All Products Import (US$ Thousand) 1996
## 3   China      Pakistan     Import   All Products Import (US$ Thousand) 1996
## 4   China      Pakistan     Import   All Products Import (US$ Thousand) 1996
## 5   China United States     Import   All Products Import (US$ Thousand) 1996
## 6   China    Bangladesh     Export   All Products Export (US$ Thousand) 1996
##        Amount SIPRI.AT.Database.ID Region_Export Region_Import   Designation
## 1    34217.72                27731         China    Bangladesh F-7M Airguard
## 2    34217.72                27731         China    Bangladesh F-7M Airguard
## 3   342263.08                28922         China      Pakistan     Type-347G
## 4   342263.08                28922         China      Pakistan     Type-347G
## 5 16155120.43                   NA         China United States             0
## 6   653039.64                27731         China    Bangladesh F-7M Airguard
##          Description Armament.category Order.date Order.date.is.estimate
## 1   fighter aircraft          Aircraft       1996                    Yes
## 2   fighter aircraft          Aircraft       1996                    Yes
## 3 fire control radar           Sensors       1996                    Yes
## 4 fire control radar           Sensors       1996                    Yes
## 5                  0                 0       1996                      0
## 6   fighter aircraft          Aircraft       1996                    Yes
##   Numbers.delivered Numbers.delivered.is.estimate Delivery.year
## 1                 3                            No          1999
## 2                 1                            No          2000
## 3                 1                            No          1997
## 4                 1                            No          1999
## 5                 0                             0             0
## 6                 3                            No          1999
##   Delivery.year.is.estimate Status SIPRI.estimate TIV.deal.unit
## 1                        No    New           11.0          11.0
## 2                        No    New           11.0          11.0
## 3                        No    New            2.5           2.5
## 4                        No    New            2.5           2.5
## 5                         0      0            0.0           0.0
## 6                        No    New           11.0          11.0
##   TIV.delivery.values Local.production TII TII.Score Region.x Export_Lat
## 1                33.0               No TII  4.128203     Asia    35.8617
## 2                11.0               No TII  4.128203     Asia    35.8617
## 3                 2.5               No TII  2.369284     Asia    35.8617
## 4                 2.5               No TII  2.369284     Asia    35.8617
## 5                 0.0                0 TII  1.184385     Asia    35.8617
## 6                33.0               No TII  4.128203     Asia    35.8617
##   Export_Lon      Region.y Import_Lat Import_Lon             TradeRoute
## 1   104.1954          Asia    23.6850    90.3563    China to Bangladesh
## 2   104.1954          Asia    23.6850    90.3563    China to Bangladesh
## 3   104.1954          Asia    30.3753    69.3451      China to Pakistan
## 4   104.1954          Asia    30.3753    69.3451      China to Pakistan
## 5   104.1954 North America    37.0902   -95.7129 China to United States
## 6   104.1954          Asia    23.6850    90.3563    China to Bangladesh
# Create base map
tradebasic <- ggplot() +
  geom_polygon(data = world_regions, aes(x = long, y = lat, group = group, fill = Region), color = "black") +
  geom_curve(data = trade_data_clean, aes(x = Export_Lon, y = Export_Lat,
                                          xend = Import_Lon, yend = Import_Lat, color = TradeRoute, size = Amount), 
             size = 0.3 ,curvature = 0.1, arrow = arrow(length = unit(0.1, "cm"))) +
  scale_fill_manual(values = region_colors, name = "Region") +  
  coord_fixed(1.3) +
  labs(title = "China, US, Russia Trade Routes", x = "Longitude", y = "Latitude") +
  theme_minimal() +
  theme(legend.position = "bottom")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# create plotly map

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
library(dplyr)
library(tidyr)

trade_data_clean = trade_data_coords %>%
  filter(!is.na(Region_Export), !is.na(Region_Import)) %>%
  mutate(TradeRoute = paste(Region_Export, "to", Region_Import)) %>%
  filter(!is.na(Export_Lon), !is.na(Export_Lat), !is.na(Import_Lon), !is.na(Import_Lat))  %>%
  filter(!(Export_Lon == Import_Lon & Export_Lat == Import_Lat))

View(trade_data_clean)


trade_data_plot <- trade_data_clean %>% 
  rowwise() %>%
  mutate(
    lon = list(c(Export_Lon, Import_Lon, NA)),
    lat = list(c(Export_Lat, Import_Lat, NA))
  ) %>%
  unnest(c(lon, lat))

View(trade_data_plot)

plot <- plot_geo() %>%
  add_trace(
    type = "scattergeo",
    mode = "lines",
    lon = ~lon,
    lat = ~lat,
    split = ~TradeRoute,
    line = list(width = 2.5, color = "steelblue"),
    color = ~TradeRoute,
    colors = "viridis",
    data = trade_data_plot
  ) %>%
  add_trace(
    type = "scattergeo",
    mode = "markers",
    lon = ~Export_Lon,
    lat = ~Export_Lat,
    text = ~paste("Country", Country),
    marker = list(size = 5, color = "hotpink2"),
    name = "Export"
  ) %>%
  add_trace(
    type = "scattergeo",
    mode = "markers",
    lon = ~Import_Lon,
    lat = ~Import_Lat,
    text = ~paste("Country", Partner.Name),
    marker = list(size = 5, color = "red"),
    name = "Import"
  ) %>%
  layout(
    title = "Interactive US and China Trade Route",
    geo = list(
      showland = TRUE,
      landcolor = "darkseagreen",
      showcoastlines = "black",
      projection = list(type = "equirectagular")
    )
  )

#animated interactive map

plot_animate <- plot_geo() %>%
  add_trace(
    type = "scattergeo",
    mode = "lines",
    lon = ~lon,
    lat = ~lat,
    split = ~TradeRoute,
    line = list(width = 2.5, color = "steelblue"),
    color = ~TradeRoute,
    colors = "viridis",
    frame = ~Year,
    data = trade_data_plot
  ) %>%
  add_trace(
    type = "scattergeo",
    mode = "markers",
    lon = ~Export_Lon,
    lat = ~Export_Lat,
    text = ~paste("Country", Country),
    marker = list(size = 5, color = "hotpink2"),
    name = "Export"
  ) %>%
  add_trace(
    type = "scattergeo",
    mode = "markers",
    lon = ~Import_Lon,
    lat = ~Import_Lat,
    text = ~paste("Country", Partner.Name),
    marker = list(size = 5, color = "red"),
    name = "Import"
  ) %>%
  layout(
    title = "Interactive US and China Trade Route",
    geo = list(
      showland = TRUE,
      landcolor = "darkseagreen4",
      showcoastlines = "black",
      projection = list(type = "equirectagular")
    ),
    updatemenus = list(
      list(
        type = "buttons",
        showactive = FALSE,
        buttons = list(
          list(label = "Play",
               method = "animate",
               args = list(NULL, list(frame = list(duration = 700, redraw = TRUE), fromcurrent = TRUE))),
          list(label = "Play",
               method = "animate",
               args = list(NULL, list(mode = "immediate", frame = list(duration = 0))))
        )
      )
    )
  )



#animated plotly


trade_data_clean = trade_data_coords %>%
  filter(!is.na(Region_Export), !is.na(Region_Import)) %>%
  mutate(TradeRoute = paste(Region_Export, "to", Region_Import)) %>%
  filter(!is.na(Export_Lon), !is.na(Export_Lat), !is.na(Import_Lon), !is.na(Import_Lat))  %>%
  filter(!(Export_Lon == Import_Lon & Export_Lat == Import_Lat))

head(trade_data_clean)
##   Country  Partner.Name Trade.Flow  Product.Group             Indicator Year
## 1   China    Bangladesh     Import   All Products Import (US$ Thousand) 1996
## 2   China    Bangladesh     Import   All Products Import (US$ Thousand) 1996
## 3   China      Pakistan     Import   All Products Import (US$ Thousand) 1996
## 4   China      Pakistan     Import   All Products Import (US$ Thousand) 1996
## 5   China United States     Import   All Products Import (US$ Thousand) 1996
## 6   China    Bangladesh     Export   All Products Export (US$ Thousand) 1996
##        Amount SIPRI.AT.Database.ID Region_Export Region_Import   Designation
## 1    34217.72                27731         China    Bangladesh F-7M Airguard
## 2    34217.72                27731         China    Bangladesh F-7M Airguard
## 3   342263.08                28922         China      Pakistan     Type-347G
## 4   342263.08                28922         China      Pakistan     Type-347G
## 5 16155120.43                   NA         China United States             0
## 6   653039.64                27731         China    Bangladesh F-7M Airguard
##          Description Armament.category Order.date Order.date.is.estimate
## 1   fighter aircraft          Aircraft       1996                    Yes
## 2   fighter aircraft          Aircraft       1996                    Yes
## 3 fire control radar           Sensors       1996                    Yes
## 4 fire control radar           Sensors       1996                    Yes
## 5                  0                 0       1996                      0
## 6   fighter aircraft          Aircraft       1996                    Yes
##   Numbers.delivered Numbers.delivered.is.estimate Delivery.year
## 1                 3                            No          1999
## 2                 1                            No          2000
## 3                 1                            No          1997
## 4                 1                            No          1999
## 5                 0                             0             0
## 6                 3                            No          1999
##   Delivery.year.is.estimate Status SIPRI.estimate TIV.deal.unit
## 1                        No    New           11.0          11.0
## 2                        No    New           11.0          11.0
## 3                        No    New            2.5           2.5
## 4                        No    New            2.5           2.5
## 5                         0      0            0.0           0.0
## 6                        No    New           11.0          11.0
##   TIV.delivery.values Local.production TII TII.Score Region.x Export_Lat
## 1                33.0               No TII  4.128203     Asia    35.8617
## 2                11.0               No TII  4.128203     Asia    35.8617
## 3                 2.5               No TII  2.369284     Asia    35.8617
## 4                 2.5               No TII  2.369284     Asia    35.8617
## 5                 0.0                0 TII  1.184385     Asia    35.8617
## 6                33.0               No TII  4.128203     Asia    35.8617
##   Export_Lon      Region.y Import_Lat Import_Lon             TradeRoute
## 1   104.1954          Asia    23.6850    90.3563    China to Bangladesh
## 2   104.1954          Asia    23.6850    90.3563    China to Bangladesh
## 3   104.1954          Asia    30.3753    69.3451      China to Pakistan
## 4   104.1954          Asia    30.3753    69.3451      China to Pakistan
## 5   104.1954 North America    37.0902   -95.7129 China to United States
## 6   104.1954          Asia    23.6850    90.3563    China to Bangladesh
inter_trade <- trade_data_clean %>% 
  rowwise() %>%
  mutate(
    interpolated = list(
      tibble(
    lon = seq(Export_Lon, Import_Lon, length.out = 25),
    lat = seq(Export_Lat, Import_Lat, length.out = 25),
    step = 1:25
      )
    )
    )%>%
  unnest(interpolated)

head(inter_trade)
## # A tibble: 6 × 36
##   Country Partner.Name Trade.Flow Product.Group    Indicator         Year Amount
##   <chr>   <chr>        <chr>      <chr>            <chr>            <int>  <dbl>
## 1 China   Bangladesh   Import     "  All Products" Import (US$ Tho…  1996 34218.
## 2 China   Bangladesh   Import     "  All Products" Import (US$ Tho…  1996 34218.
## 3 China   Bangladesh   Import     "  All Products" Import (US$ Tho…  1996 34218.
## 4 China   Bangladesh   Import     "  All Products" Import (US$ Tho…  1996 34218.
## 5 China   Bangladesh   Import     "  All Products" Import (US$ Tho…  1996 34218.
## 6 China   Bangladesh   Import     "  All Products" Import (US$ Tho…  1996 34218.
## # ℹ 29 more variables: SIPRI.AT.Database.ID <int>, Region_Export <chr>,
## #   Region_Import <chr>, Designation <chr>, Description <chr>,
## #   Armament.category <chr>, Order.date <int>, Order.date.is.estimate <chr>,
## #   Numbers.delivered <int>, Numbers.delivered.is.estimate <chr>,
## #   Delivery.year <int>, Delivery.year.is.estimate <chr>, Status <chr>,
## #   SIPRI.estimate <dbl>, TIV.deal.unit <dbl>, TIV.delivery.values <dbl>,
## #   Local.production <chr>, TII <chr>, TII.Score <dbl>, Region.x <chr>, …
unique(inter_trade$step)
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
custom_colors <- colorRampPalette(brewer.pal(9, "Set1"))(length(unique(inter_trade$Indicator)))



animated_plot <- plot_ly() %>%
  add_trace(
    type = "scattergeo",
    mode = "lines",
    lon = ~lon,
    lat = ~lat,
    frame = ~Year,
    color = ~Indicator,
    line = list(width = 5),
    data = inter_trade) %>%
  layout(
    title = "Animated US, Russia, and China Trade Route",
    geo = list(
      showland = TRUE,
      landcolor = "tan",
      showcoastlines = "black",
      projection = list(type = "equirectagular")
    ),
    updatemenus = list(
      list(
        type = "buttons",
        showactive = FALSE,
        buttons = list(
          list(label = "Play",
               method = "animate",
               args = list(NULL, list(frame = list(duration = 700, redraw = TRUE), fromcurrent = TRUE))),
          list(label = "Play",
               method = "animate",
               args = list(NULL, list(mode = "immediate", frame = list(duration = 0))))
        )
      )
    )
  )

Including Plots

You can also embed plots, for example:

## Warning in p$x$data[firstFrame] <- p$x$frames[[1]]$data: number of items to
## replace is not a multiple of replacement length
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.