Blog - BS Data, hackery, stories

Quick R: the UK foreign trade balance in ggplot2

rdf:RDF dc:formatimage/svg+xml/dc:format dc:title/dc:title /cc:Work /rdf:RDF Trade surplus Trade deficit United States Germany 0 25,000 50,000 75,000 100,000 125,000 0 20,000 40,000 60,000 80,000 Imports (in £m) Exports (in £m) first export destination France andthe Netherlands China

No trick for this example: we're plotting imports and exports on different axes, thus isolating the most important trade partners of the United Kingdom.

To obtain different colours depending on wether the balance of trade with this country is a surplus or an excedent, we run some quick dplyr transformations:

  1. We calculate the trade balance (exports minus imports) and store this in a new column, thanks to mutate():
d.2015.balance <- d.2015 %>% mutate(balance = Exports - Imports)
  1. If the balance is a surplus, it will be positive (more exports than imports). If it is a deficit, it will be negative. We check the sign with R's built-in sign() method, and send the result to a new column, aptly named balance, again with mutate():
d.2015.balance <- d.2015.balance %>% mutate(surplus = sign(balance))

The plotting is very straightforward. We save the file as SVG and open it up in Inkscape to make it come to life a little bit.

We add some smooth curved lines for annotations and liven up the key. Here is the result, as a GIF:

library(dplyr)
library(readr)
library(scales)
library(ggplot2)
library(ggthemes)

# read csv file from Github
d <- read_csv("https://raw.githubusercontent.com/times/trade-component/master/app/static/data/join_simple.csv?token=AFb-IG5fP6yINxBFEfRkwqATsjFRXihXks5Y2_pPwA%3D%3D")

# only select 2015 data
d.2015 <- d %>% filter(Date == 2015)

# add a 'balance' column
d.2015.balance <- d.2015 %>% mutate(balance = Exports - Imports)

# add a 'surplus' column (-1, 0, or 1) so we have
# categorical colours on the plot
# -1 is deficit (more imports than exports),
# 0 is even balance,
# and 1 is trade surplus (more exports than imports)
d.2015.balance <- d.2015.balance %>% mutate(surplus = sign(balance))

p <- ggplot(data = d.2015.balance,
  aes( x = Imports, y = Exports, fill = surplus)) +
  geom_point(shape = 21, size = 5) +
  stat_smooth(method = "lm", se = FALSE) +
  scale_x_continuous(limits = c(0,80000), labels = comma) +
  scale_y_continuous(limits = c(0,120000), labels = comma) +
  theme_solarized() +
  theme(legend.position = "none")

# annotation layer
p + annotate("text", x = 20000, y = 50000, label = "Trade surplus") +
    annotate("text", x = 50000, y = 2000, label = "Trade deficit") +
    annotate("text", x = 50000, y = 95000, label = "United States") +
    annotate("text", x = 66000, y = 45000, label = "Germany")

Data from the Office for National Statistics - click to download (XLSX).