# Rendering this doc: # rmarkdown::render(paste0(getwd(),'/README.Rmd'), envir=globalenv()) knitr::opts_chunk$set(echo = TRUE) knitr::opts_chunk$set(engine = 'R')
## if package `devtools` not installed, first do this: # install.packages('devtools') devtools::install_github('alekrutkowski/wiod.diagrammer')
https://alekrutkowski.github.io/wiod.diagrammer/reference/index.html
Load WIOD data for a given year from an official WIOD .Rdata file (release 2016), which has to be first downloaded from http://www.wiod.org/protected3/data16/wiot_ROW/wiot_r_Nov16.zip and extracted (unzipped).
library(wiod.diagrammer)
library(magrittr) plotLinks <- function(...) # a trick to plot svg wiod.diagrammer::plotLinks(...) %>% DiagrammeRsvg::export_svg() %>% cat(file='Graph.svg') print <- function(d) # Just for clarity `if`(d %>% is.data.frame, base::print.data.frame(`colnames<-`(d, paste0(colnames(d),' ')), right=FALSE, row.names=FALSE), base::print.default(d))
W <- loadWIOD('WIOT2014_October16_ROW.RData')
# Check the names of the first 10 columns: cat(head(colnames(W), 20), sep='\n') # How many columns and rows? message(ncol(W),' columns; ',nrow(W),' rows')
Now let's flatten (reshape into long format):
W_flat <- flatWIOD(W) # See the structure of W: str(W_flat) head(W_flat, 10) tail(W_flat, 10)
wiod.diagrammer
uses internally the
data.table
package for performance. You can work with W_flat
using the
data.table
's semantcs (e.g. the :=
in-place column generation/modification)
or, if you prefer base R data.frame
s, you may convert W_flat
into
a regular data.frame
with the function as.data.frame
, do the modifications,
and then convert it back into data.table
with the function
data.table::as.data.table
for further processing (through the function
findLinks
described below).
Now let's find the top supplier-user linkages in WIOD (as defined by the
column value
in W_flat
which -- if not modified -- corresponds to the
intermediate consumption in W
).
First let's introduce a helper function tieRobustRankLessOrEqual
(available
in wiod.diagrammer
) comparing it with base R rank
:
x <- c(1,1,2,2,2,3,3) # A comparison: print(data.frame(x, rank(x) <= 2, tieRobustRankLessOrEqual(x, 2), check.names = FALSE))
Now, let's find e.g. the 3 main customers of German automotive industry as well as 2 main customers of those customers and 1 main customer of those customer's customers (we could go even deeper if we wanted or use different cut-off ranks, not necessarily in the declining order). So, we have 3 levels of linkages (the 1st one -- direct, the 2nd one and the 3rd one -- indirect). NB: Some industries may re-emerge at different rounds and they may be both customers and suppliers at the same time. Let's also differentiate our ranks (top 3, 2, and 1) by one more dimension: domestic vs foreign linkages.
W_flat[, domestic := # creating column in-place, following data.table's semantics ExpCountry == ImpCountry] # Let's get rid of self-produced intermediate consumption W_flat_noself <- W_flat[!(domestic & ExpSectorNr == ImpSectorNr)] # Let's keep only intra-EU trade COUNTRIES_DT <- countries() EU_COUNTRIES <- COUNTRIES_DT$Country[COUNTRIES_DT$isEUmember] # Let's keep only flows >= 1 billion USD for clarity W_flat_noself_truncated <- W_flat_noself[value >= 1000 & # original WIOD data is in million USD ExpCountry %in% EU_COUNTRIES & ImpCountry %in% EU_COUNTRIES] TOP_CUSTOMERS <- findLinks(partners = 'users', flat_wiod = W_flat_noself_truncated, start_countries = 'DEU', # We could add here other countries. start_sectors = 20, # "Manufacture of motor vehicles, trailers and semi-trailers" # We could add here other sectors. ListOfselectionFuns = # As discussed in the text above: list(function(flat_wiod) tieRobustRankLessOrEqual(-flat_wiod$value, 3), # minus because we function(flat_wiod) tieRobustRankLessOrEqual(-flat_wiod$value, 2), # want to rank from function(flat_wiod) tieRobustRankLessOrEqual(-flat_wiod$value, 1)), # the highest to the lowest by = c('domestic','ExpCountry','ExpSectorNr')) # as discussed above str(TOP_CUSTOMERS) head(TOP_CUSTOMERS, 20)
Now let's do a similar exercise, just "upstream" i.e. for suppliers of suppliers.
TOP_SUPPLIERS <- findLinks(partners = 'suppliers', flat_wiod = W_flat_noself_truncated, start_countries = 'DEU', # We could add here other countries. start_sectors = 20, # "Manufacture of motor vehicles, trailers and semi-trailers" # We could add here other sectors. ListOfselectionFuns = # As discussed in the text above: list(function(flat_wiod) tieRobustRankLessOrEqual(-flat_wiod$value, 3), # minus because we function(flat_wiod) tieRobustRankLessOrEqual(-flat_wiod$value, 2), # want to rank from function(flat_wiod) tieRobustRankLessOrEqual(-flat_wiod$value, 1)), # the highest to the lowest by = c('domestic','ImpCountry','ImpSectorNr')) # as discussed above str(TOP_SUPPLIERS) head(TOP_SUPPLIERS, 20)
Now, let's plot the "upstream" linkages, making all the German sectors blue. By default, the cross-border flows are dashed, while the domestic flows are solid lines (arrows). The numbers in the nodes (rectangles) represent, by default, the sector output (or the total intermediate consumption for the final use sectors such as final consumption or investment if they show up in the customers' graphs).
In wiod.diagrammer
the rendering of the plot is done internally by DiagrammeR::grViz
.
The plots can be saved manually in RStudio, or programmatically e.g. to an .svg file via
DiagrammeRsvg::export_svg
to a character vector and then cat
ed,
or to a .png file piping them through
DiagrammeRsvg::export_svg
,
charToRaw
and rsvg::rsvg_png
.
plotLinks(top_links_dt = TOP_SUPPLIERS, wiot = W, # this is necessary specificNodeOptionsFun = # this is optional, just to show-off: function(country_sector_dt) ifelse(country_sector_dt$Country=='DEU', 'style=filled, fillcolor=cadetblue1', "")) # GraphViz colour names can be found at: # http://www.graphviz.org/doc/info/colors.html
Click on the picture to zoom in:
What else is available in the package? The functions which produce
auxiliary data.table
s (that are used by wiod.diagrammer
's plotLinks
function if evaluated with default argument values).
COUNTRIES <- countries() # NB: no argument to function `countries` str(COUNTRIES) print(COUNTRIES) SECTORS <- sectors(W) SECTORS[, SectorLab := # truncate the long sector labels just for clarity below substr(SectorLab, 1, 30)] str(SECTORS) head(SECTORS, 20) AGGREGATES <- aggregates(W)
The variable/column names produced by the function aggregates
reflect those in WIOD. They are explained in the documentations
of aggregates
.
str(AGGREGATES) head(AGGREGATES, 20)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.