NAFTA trade dashboard

June 18, 2017

(This article was first published on En El Margen - R-English, and kindly contributed to R-bloggers)

Tradewatch is a dashboard, written in R using the flexdashboard framework. Visualizations are made possible thanks to highcharter and other packages.

The main purpose of the dashboard is to monitor trade, mostly from the Mexican perspective, of goods and commodities with countries that are in NAFTA.

In the age of fake news and policy-by-twitter, hopefully this tool will help guide more informed debate (and decisions) with respect to trade between these three countries.

Notes and methodology

The dashboard consists of basically three sections:

  • Bilateral Trade: These contain two, side-by-side comparisons, of exports and imports of products at a 2-level HS code depth. The color denotes year-over-year growth rate, while the size is related to the dollar value (in the newest month) of exports. The average growth rate for 2 digit codes is a weighted-mean of each 4-digit classification.
  • US Share-shift: A visualization that describes the shift in share in the U.S. market of Mexican goods relative to Canadian goods. When the shift is lower than 0, Mexican imports lost share (vs. Canada last year) in the United States, and vice-versa. Important caveat: the share is calculated only for goods from Mexico or Canada (Because they are not collectively exhaustive, it is perfectly posible that both lost share against a third competitor)
  • Terms of Trade & Fx: Prices are important. This section aims to gauge the impact of prices in the trade dynamic. To obtain exchange rates, I use the tidyquant package. The other indexes refer to the price index of products imported from each of those countries.

The names of HS Codes have been abbreviated manually, they can be found here, however the code is still in between parenthesis. Should you want to look it up further, I recommend the UN Comtrade website


This release is a first and fast dive into the data, so I would be happy to listen to improvements to the tool, including some new indicators I haven’t added. FYI, I tried to build the export matrixes at a 4-level granularity, but highcharts couldn’t handle it, any suggestions on how to do it would be welcome.

You can contact me via twitter or at my blog’s issue page.


The newest I could build is from november 2016, which is the month all three countries have statistics ready.


The code used to build the dashboard consists of two files: a functions.R file that contains some functions for downloading and cleaning the data and a db.Rmd file that builds the dashboard using the knitr/flexdashboard process in RStudio.

Both of these files can be found here and here.

FYI, I borrowed heavily from here and here.

To leave a comment for the author, please follow the link and comment on their blog: En El Margen - R-English. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)