
Visualizing Global Trade with Chord Graphs
This image is largely the result of me brushing up on my R skills while looking for an excuse to create a Chord Graph. The image below shows the imports and exports of the countries with the Top 50 values in these metrics, according to Worldbank.org.
The first step was to download the data available above, and use an R script to combine ~200 .csv files into a single table:
library( plyr )
setwd("C:...")
trade <- ldply( .data = list.files(pattern="*.CSV"),
.fun = read.csv,
header = FALSE,
col.names=c("Reporter", "Partner", "Product categories", "Indicator Type", "Indicator", "2018", "2017", "2016", "2015", "2014", "2013", "2012", "2011", "2010", "2009", "2008", "2007", "2006" ,"2005" ,"2004" ,"2003" ,"2002" ,"2001" ,"2000", "1999", "1998", "1997", "1996", "1995", "1994", "1993", "1992", "1991", "1990", "1989", "1988"))
This single large table was filtered to only include the countries with the highest reported totals. I did not distinguish between imports and exports, I just totaled the “2018” column by “Reporter” and kept the top 50 countries. From there, a matrix of trade relationships was built. The (i,j) entry represents exports from country i to country j. With an adjacency matrix, a list of labels for the countries, and the chorddiag package, it’s fairly straightforward to create a graph:
setwd("C:...")
trade_matrix = as.matrix(read.csv("trade_matrix_top50.csv",header=FALSE))
trade_matrix <- matrix(as.numeric(trade_matrix), ncol = ncol(trade_matrix))
trade_matrix[is.na(trade_matrix)] = 0
countries = c("United States","European Union","China","Germany","Hong Kong, China","Japan","Mexico","Canada","Korea, Rep.","France","Netherlands","United Kingdom","Belgium","Italy","Singapore","Other Asia, nes","United Arab Emirates","India","Saudi Arabia","Spain","Switzerland","Russian Federation","Australia","Poland","Malaysia","Thailand","Brazil","Czech Republic","Indonesia","Austria","Ireland","Sweden","Turkey","Norway","Hungary","Denmark","Portugal","Philippines","Slovak Republic","Chile","Romania","Kuwait","South Africa","Finland","Israel","Argentina","Qatar","Nigeria","Kazakhstan")
dimnames(trade_matrix) <- list(Exporters = countries,
Importers = countries)
library(chorddiag)
groupColors <- c("#069668", "#e96b22", "#a0b099", "#fef48e", "#bef451", "#591489", "#155392", "#4b381d", "#992a13", "#51f310", "#788ce0", "#ce3bf6", "#fd1e6e", "#38f0ac", "#c0852a", "#c87ef8", "#0b5313", "#e5808e", "#25b8ea", "#0f144c", "#fbedf1", "#89aa0d", "#871550", "#ee0d0e", "#b0fff9", "#21a708", "#270fe2","#069668", "#e96b22", "#a0b099", "#fef48e", "#bef451", "#591489", "#155392", "#4b381d", "#992a13", "#51f310", "#788ce0", "#ce3bf6", "#fd1e6e", "#38f0ac", "#c0852a", "#c87ef8", "#0b5313", "#e5808e", "#25b8ea", "#0f144c", "#fbedf1", "#89aa0d", "#871550", "#ee0d0e", "#b0fff9", "#21a708", "#270fe2","#069668", "#e96b22", "#a0b099", "#fef48e", "#bef451", "#591489", "#155392", "#4b381d", "#992a13", "#51f310", "#788ce0", "#ce3bf6", "#fd1e6e", "#38f0ac", "#c0852a", "#c87ef8", "#0b5313", "#e5808e", "#25b8ea", "#0f144c", "#fbedf1", "#89aa0d", "#871550", "#ee0d0e", "#b0fff9", "#21a708", "#270fe2","#069668", "#e96b22", "#a0b099", "#fef48e", "#bef451", "#591489", "#155392", "#4b381d", "#992a13", "#51f310", "#788ce0", "#ce3bf6", "#fd1e6e", "#38f0ac", "#c0852a", "#c87ef8", "#0b5313", "#e5808e", "#25b8ea", "#0f144c", "#fbedf1", "#89aa0d", "#871550", "#ee0d0e", "#b0fff9", "#21a708", "#270fe2","#069668", "#e96b22", "#a0b099", "#fef48e", "#bef451", "#591489", "#155392", "#4b381d", "#992a13", "#51f310", "#788ce0", "#ce3bf6", "#fd1e6e", "#38f0ac", "#c0852a", "#c87ef8", "#0b5313", "#e5808e", "#25b8ea", "#0f144c", "#fbedf1", "#89aa0d", "#871550", "#ee0d0e", "#b0fff9", "#21a708", "#270fe2","#069668", "#e96b22", "#a0b099", "#fef48e", "#bef451", "#591489", "#155392", "#4b381d", "#992a13", "#51f310", "#788ce0", "#ce3bf6", "#fd1e6e", "#38f0ac", "#c0852a", "#c87ef8", "#0b5313", "#e5808e", "#25b8ea", "#0f144c", "#fbedf1", "#89aa0d", "#871550", "#ee0d0e", "#b0fff9", "#21a708", "#270fe2","#069668", "#e96b22", "#a0b099", "#fef48e", "#bef451", "#591489", "#155392", "#4b381d", "#992a13", "#51f310", "#788ce0", "#ce3bf6", "#fd1e6e", "#38f0ac", "#c0852a", "#c87ef8", "#0b5313", "#e5808e", "#25b8ea", "#0f144c", "#fbedf1", "#89aa0d", "#871550", "#ee0d0e", "#b0fff9", "#21a708", "#270fe2","#069668", "#e96b22", "#a0b099", "#fef48e", "#bef451", "#591489", "#155392", "#4b381d", "#992a13", "#51f310", "#788ce0", "#ce3bf6", "#fd1e6e", "#38f0ac", "#c0852a", "#c87ef8", "#0b5313", "#e5808e", "#25b8ea", "#0f144c", "#fbedf1", "#89aa0d", "#871550", "#ee0d0e", "#b0fff9", "#21a708", "#270fe2","#069668", "#e96b22", "#a0b099", "#fef48e", "#bef451", "#591489", "#155392", "#4b381d", "#992a13", "#51f310", "#788ce0", "#ce3bf6", "#fd1e6e", "#38f0ac", "#c0852a", "#c87ef8", "#0b5313", "#e5808e", "#25b8ea")
groupColors <- groupColors[1:49]
export_graph <- chorddiag(trade_matrix, groupColors = groupColors, groupnamePadding = 10, showTicks = FALSE)
library(htmlwidgets)
saveWidget(export_graph, file=paste0( getwd(), "/chord_interactive.html"))