\newpage

library(dplyr)
library(tradeflows)
library(knitr)
library(ggplot2)
options(digits = 4) #  Number of digits in knitr tables
opts_chunk$set(echo=FALSE, message=FALSE)

Abstract

This document presents an overview of validated UN COMTRADE trade flow data for key forest product categories. The key forest product categories follow the main product groups of the FAO/ITTO/ECE/Eurostat forest products classification system. For each of these main forest product categories, the value and quantity of trade is shown for the relevant 6-digit product codes of the Harmonized System, i.e. the world customs commodity classification system. You can query detailed trade statistics via the Data Query and Visualisation interface.

######### #
# ! This chunk is used for development purposes only, keep eval=FALSE.
######### #
# Test countries: Cameroon, Ghana, Indonesia
#----------------------------------------------------------
reporterinreport <- "France"
reporterinreport <- "Germany"
reporterinreport <- "Gabon"
# Generate the report from the version in development, JFSQ level 1 titles (default)
createoverviewreport(reporterinreport, inputpath = "inst/templates", beginyear = 2010, endyear = 2014)

# JFSQ level 2 titles
createoverviewreport(reporterinreport, inputpath = "inst/templates", beginyear = 2010, endyear = 2014, jfsqlevel = 2)

# Generate the tfdata dataset for development purposes within this template, JFSQ level 1 titles (default)
tfdata <- createoverviewreport(reporterinreport, dataonly = TRUE, inputpath = "inst/templates")
# dataset with level 2 titles
tfdata <- createoverviewreport(reporterinreport, dataonly = TRUE, inputpath = "inst/templates", jfsqlevel = 2)

unique(tfdata$product)
tfdata %>% group_by(product) %>%
    summarise(tradevalue = sum(tradevalue),
              quantity = sum(quantity)) %>% data.frame() %>%
    arrange(tradevalue) #%>% kable
# 100 largest trade flows
largetf <- tfdata %>% filterworldeu28 %>% data.frame() %>% # remove grouping
    arrange(desc(tradevalue)) %>% head(100)


#' n largest partners for the dataframe currently in use in the report
#' 
#' Filter data in dtf for those 10 large partners
#' @param dtf data frame of trade flow data
#' @param n number of parnters to select
#' @param product_ character vector of product names, when NULL, query all products
#' @examples
#'\dontrun{
#' largepartnersbyflow(tfdata,"ROUNDWOOD", "Import")
#' }
largepartnersbyflow <- function(dtf, product_, flow_, n = 5){
    dtf %>%
        filter(product %in% product_ & 
                   flow == flow_) %>%
        # Group by year as well
        group_by(year, reporter, partner, partnercode, flow, product) %>%
        summarise(tradevalue = sum(tradevalue)) %>%
        filterworldeu28() %>% 
        # Group again without years, this will get the sum of all years
        group_by(flow, partner, partnercode) %>% 
        summarise(tradevalue = sum(tradevalue)) %>%
        ungroup() %>%
        arrange(desc(tradevalue)) %>% 
        head(n) 
}



#' Filter trade flows for the largest partners
#'
#' @param dtf data frame of trade flows
#' @param product_ given product name
#' @param flow character, flow name
#' @examples
#'\dontrun{
#' filterlargepartners(tfdata,"ROUNDWOOD", "Volume in cubic meters","Import")
#' }
filterlargepartners <- function(dtf, product_, flow_){
    # Prepare this object first otherwise query is very slow
    partnercodes <- largepartnersbyflow(dtf, product_, flow_)$partnercode
    # Data
    dtf2 <- dtf %>% 
        filter(product == product_ & 
                   flow == flow_ & 
                   partnercode %in% partnercodes)
    # might fail for some products
    dtf2 <- mutate(dtf2, partner = stringr::str_wrap(partner, width = 14))
    return(dtf2)
}
#' Product description
#' Use:
#' description(c(440799, 440795))
description <- function(productcodes){
    descr <- classificationcomtrade$HS %>%
        filter(productcode %in% productcodes) 
    # Individual codes and description remove product code if its
    # at the begining of the description
    if (sum(as.character(descr$productcode) !=
        substring(descr$description, 1, 6))==0){
        descr$description <- substring(descr$description,7)
        }
    for(code in descr$productcode){
        cat("\n\n__",code,":__ ", sep="")
        cat(descr$description[descr$productcode == code])
    }
}
#' Plot product, to be used in the loop on product names below
#' For the moment tfdata is taken from the global environment is this bad?
#' @param product_ the itto name of a product
productplot <- function(product_){
    # Check if the given product is in the tfdata data frame
    stopifnot(product_ %in% unique(tfdata$product))
    import <- filterlargepartners(tfdata, product_, "Import")
    export <- filterlargepartners(tfdata, product_, "Export")

    p <- ggplot(NULL,
                aes(x = year, y = tradevalue,
                    fill = productcode)) +
        # geom_bar(stat="identity") +
        ylab("Trade value in  US dollars") +
        theme(legend.position= "bottom") +
        # Scale might be changed to avoid year overlapping when there are many years
        # scale_x_continuous(breaks = c(2010,2012)) +
        facet_grid(flow + reporter ~ partner, scales="free_y") + 
        guides(fill=guide_legend(nrow=2,byrow=TRUE))
    # One plot for import and one plot for export
    importp <- p + geom_bar(data = import, 
                            stat="identity") + 
        ggtitle(paste("Largest ", product_, " imports reported by", reporterinreport)) 
    exportp <- p + geom_bar(data = export, 
                            stat="identity") + 
        ggtitle(paste("Largest ", product_, " exports reported by", reporterinreport)) 
    try(print(importp))
    try(print(exportp))
    combined <- rbind(import, export)
    try(description(as.character(unique(combined$productcode))))
}


#' Function to return an error while plotting
#' tryCatch(stop(e), error = ploterror)
ploterror <- function(e) {
        cat("\nThere was an error while drawing the plot :\n")
        print(e)
}
# message("Try to see if it's possible to have 2 plots sharing the same legend, check tile plot from erlier versions")
for (product_ in unique(tfdata$product)){
    cat("\n##",product_,"\n\n")
    tryCatch(productplot(product_),
             error = ploterror)
    cat("\n\\newpage\n\n")
}


if (FALSE){
    unique(tfdata$product)
    # plot one product
    productplot("ROUNDWOOD")

    # There is an issue for some products 
    # Error: unsupported type for column 'partner' (NILSXP, classes = NULL) 
    # It seems to happen when "Côte d'Ivoire" is a partner, probably due to differences in encoding
    filterpaper <- "OTHER ARTICLES OF PAPER AND PAPERBOARD, READY FOR USE of which: FILTER PAPER AND PAPERBOARD , READY FOR USE"
    productplot(filterpaper)
    filterpaperdata <- filter(tfdata, product == filterpaper)
    unique(filterpaperdata$partner)


}

All products

tfsummarised <- tfdata %>%
    group_by(year, reporter, partner, partnercode, flow, product) %>%
    summarise(tradevalue = sum(tradevalue)) %>%
    collect()


#' 10 largest partners for the dataframe currently in use in the report
#' import and export are mixed
#' @param dtf data frame of summarised trade flow data. Is this realy needed?
#' @param n number of parnters to select
#' @param product_ character vector of product names, when NULL, query all products
#' @examples
#'\dontrun{
#' largepartners()
#' largepartners(c("LOGS","SAWNWOOD"))
#' }
largepartners <- function(product_=NULL, dtf = tfsummarised, n = 10){
    if (!is.null(product_)){
        dtf <- filter(dtf, product %in% product_) 
    }
    dtf <- dtf %>%
        filterworldeu28() %>% 
        group_by(flow, partner, partnercode) %>% 
        summarise(tradevalue = sum(tradevalue)) %>%
        ungroup() %>%
        arrange(desc(tradevalue)) %>% 
        # select(flow, partner, partnercode) %>%
        head(n) # Could add flow here
    return(dtf)
}


partnercodes <- largepartners()$partnercode
# filter(tfdata, partner %in% largetf$partner)
ggplot(filter(tfsummarised, partnercode %in% partnercodes),
       aes(x = as.numeric(year), y = tradevalue/1e6,
                       fill = as.factor(product))) +
    geom_bar(stat="identity") +
    ylab("Trade value in million US dollars") +
    theme(legend.position= "bottom") +
    scale_x_continuous(breaks = c(2010,2012)) +
    ggtitle(paste("Largest wood products flows reported by", reporterinreport)) +
    facet_grid(flow + reporter ~ partner) + 
    guides(fill=guide_legend(ncol=1,byrow=TRUE))

message("place country names over 2 lines if necessary")
# http://stackoverflow.com/questions/9052650/ggplot2-splitting-facet-strip-text-into-two-lines
# Using stringr::str_wrap
# stringr::str_wrap("lkj lkj lkjlkj lkj lkj ", width = 20) 
# Or with base functions, without the stringr package
# paste(strwrap("lkj lkj lkjlkj lkj lkj ", width = 20), collapse="\n")

# Optionally 
# Make the same plot with flows reported from those countries
### Trade between `r reporterinreport` and the EU
# EU is a reporter in the database, but not a partner.
ggplot(filter(tfdata, partner == "EU28"),
       aes(x = as.numeric(year), y = tradevalue/1e6,
                       fill = as.factor(product))) +
    geom_bar(stat="identity") +
    scale_x_continuous(breaks = c(2010,2012)) +
    ggtitle("100 Largest wood products import flows reported by the EU with China") +
    facet_grid(flow + reporter ~ partner) + 
    ylab("Trade value in million US dollars") +
    theme(legend.position= "bottom") +
    guides(fill=guide_legend(nrow=4,byrow=TRUE))


EuropeanForestInstitute/tradeflows documentation built on Oct. 7, 2019, 10:57 a.m.