Thursday, January 17, 2013

Maize trade Part I: Generate the network diagram

It has been several month since my last post, partially due to the fact that my laptop was lost and several deadlines was approaching. Fortunately I will be returning to Taiwan and get a new laptop within a week, and will be updating regularly again.

This post will provide a brief peak of the trade network which will be presented in the new FAO statistical yearbook 2013. The data area available from FAOSTAT ( under the "trade" and "detail trade matrix"section.

From the plot we can see that the United States of America is the biggest exporter of maize and Japan is the biggest importer as indicated by the size of the arrow which represents the size of trade. Argentina, Brazil, Hungary and France are also major exporters. This is an improvement from previous analysis and graphics where only bi-lateral relationship or aggregated time series were shown.

Instead of the igraph package, I have used the network package to generate the network this time. The code is much simpler and it  is also more integrated with the sna package for conducting analysis.

## Load required library

## Read in data
maize.df = read.csv("", header = TRUE,
    stringsAsFactors = FALSE)

## Present only the top 80% of the value traded to avoid over plotting.
maizeEx.df$cs = cumsum(maizeEx.df$Maize)
maizeFinal.df = subset(maizeEx.df, cs < tail(maizeEx.df$cs, 1) * 0.8)

## Create the network and set the size of arrow
maizeFinal.df$edgeSize = with(maizeFinal.df, Maize/sum(Maize))
maizeFinal.df$arrowSize = ifelse(maizeFinal.df$edgeSize * 30 < 0.5 , 0.5,
    maizeFinal.df$edgeSize * 15) = network(maizeFinal.df[, 1:2])

## Plot the network
plot(, displaylabels = TRUE, label.col = "steelblue",
     edge.lwd = c(maizeFinal.df$edgeSize) * 100,
     arrowhead.cex = c(maizeFinal.df$arrowSize),
     label.cex = 2, vertex.border = "white",
     vertex.col = "skyblue", edge.col = rgb(0, 0, 0, alpha = 0.5))


  1. Hello,
    I try to replicate the network chart but the code seems incomplete. For example:
    > maizeEx.df$cs = cumsum(maizeEx.df$Maize)
    Error: object 'maizeEx.df' not found

    I think I know what you are trying to do so I duplicate maizeEx.df <- maize.df plus some other minor tweaks. Then I get a of of oversized labels. If I turn them off the network does not look anything like the one on the webpage. Is the code the one you used for the chart?

    Thanks for sharing your work.


  2. Great diagram! I used a website called Lucidchart as a network diagram generator and it was really easy to understand. If you use diagrams often you should check it out!

  3. Nice diagram, is it a AON network diagram. Very graphical and professional diagrams can be drawn from Creately cloud based diagram software. Check that out!