\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", template = "overviewquantity.Rmd", beginyear = 2010, endyear = 2014)

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

# Generate the tfdata dataset for development purposes within this template
tfdata <- createoverviewreport(reporterinreport, dataonly = TRUE, inputpath = "inst/templates") 

unique(tfdata$product)
tfdata %>% group_by(product) %>%
    summarise(tradevalue = sum(tradevalue),
              quantity = sum(quantity)) %>% data.frame() %>%
    arrange(desc(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", "Volume in cubic meters","Import")
#' }
largepartnersbyflow <- function(dtf, product_, unit_, flow_, n = 5){
    dtf %>%
        filter(product %in% product_ & 
                   unit == unit_ & 
                   flow == flow_) %>%
        # Group by year as well
        group_by(year, reporter, partner, partnercode, flow, product, unit) %>%
        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 unit_ unit
#' @param flow character, flow name
#' @examples
#'\dontrun{
#' filterlargepartners(tfdata,"ROUNDWOOD", "Volume in cubic meters","Import")
#' }
filterlargepartners <- function(dtf, product_, unit_, flow_){
    # Prepare this object first otherwise query is very slow
    partnercodes <- largepartnersbyflow(dtf, product_, unit_, flow_)$partnercode
    # Data
    dtf2 <- dtf %>% 
        filter(product == product_ & 
                   unit == unit_ & 
                   flow == flow_ & 
                   partnercode %in% partnercodes)
    # might fail for some products
    dtf2 <- mutate(dtf2, partner = stringr::str_wrap(partner, width = 14))
    return(dtf2)
}
#' 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_, unit_){
    # Check if the given product is in the tfdata data frame
    stopifnot(product_ %in% unique(tfdata$product))
    import <- filterlargepartners(tfdata, product_, unit_, "Import")
    export <- filterlargepartners(tfdata, product_, unit_, "Export")
    combined <- rbind(import, export)
    # test if there are more than one unit, give a warning
    p <- ggplot(NULL,
                aes(x = year, y = quantity,
                    fill = productcode)) +
        # geom_bar(stat="identity") +
        ylab(paste(unique(combined$unit))) +
        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))
    # Description call has to be placed here 
    # because the combined dataframe is created in this function
    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")
    # Have to use the print method because 
    # argument 1 (type 'list') cannot be handled by 'cat' 
    print(e)
}
# message("Try to see if it's possible to have 2 plots sharing the same legend, check tile plot from earlier versions")

#' @examples
#' titleandplot("PAPER AND PAPERBOARD","Weight in kilograms")
titleandplot <- function(product_,unit_){
    cat("\n## ",as.character(product_), " (",unit_,")\n\n", sep="")
    tryCatch(productplot(product_, unit_),
             error = ploterror)
    cat("\n\\newpage\n\n")
    return(data.frame(one = c(1)))
}

donotdisplay <- tfdata %>% 
    select(product, unit) %>% 
    distinct() %>%
    group_by(product,unit) %>%
    arrange() %>%
    # Do operation is like a nested loop on the product and unit variables
    do(titleandplot(.$product, .$unit))


if (FALSE){
    unique(tfdata$product)
    # plot one product only
    productplot("ROUNDWOOD")
    # Encoding problem for Gabon
    productplot("PAPER AND PAPERBOARD")

    # 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)


}

When displaying validated data, trade flows "reported by" one country can also mean that these flows have been estimated or corrected.



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