This is the development version of the completeness report. The version distributed with the package is under inst/templates
This report is generated automatically based on data from the UN Comtrade API. Tables and graphs are in development. They are intended for internal use within the TradeFLowsDB project. Country numbers in tables below might be slightly overestimated, use them as a rough indication only.
library(dplyr) library(tradeflows) library(reshape2) library(ggplot2) library(knitr)
message("You can now develop reports directly in the inst/templates folder !!!") message("There is a way to generate them without building the package by specifying the template inputpath") tfdata <- readdbproduct(440799, "raw_flow_yearly") productcodeinreport <- 440799 # This report is intended for one product only tfdata2 <- tfdata %>% addregion %>% filter(productcode == productcodeinreport) %>% filterworldeu28 completeness <- tfdata2 %>% select(reporter, classification, year, quantity, weight, tradevalue) %>% melt(id=c("reporter", "classification", "year")) %>% mutate(value = !is.na(value)) %>% unique %>% arrange(classification, reporter, year) productcodeinreport <- as.character(productcodeinreport)
flagcountry <- tfdata2 %>% group_by(reporter, year, flow, flag) %>% filter(flow %in% c("Import", "Export")) %>% summarise(tradevalue = sum(tradevalue), quantity = sum(quantity, na.rm = TRUE), weight = sum(weight, na.rm = TRUE)) %>% data.frame %>% # Needed otherwise it will return factors with only one level Factor w/ 1 level "0": mutate(flag = as.factor(flag)) flagregion <- tfdata2 %>% group_by(regionreporter, year, flow, flag) %>% filter(flow %in% c("Import", "Export")) %>% # Remove re-import and re-export summarise(tradevalue = sum(tradevalue), quantity = sum(quantity, na.rm = TRUE), weight = sum(weight, na.rm = TRUE)) %>% data.frame %>% # Needed otherwise it will return factors with only one level Factor w/ 1 level "0": mutate(flag = as.factor(flag)) str(flagregion$flag)
Product r productcodeinreport
has the following definitions:
productname <- tfdata2 %>% select(classification, productdescription) %>% arrange(classification) %>% unique for(code in productname$classification){ cat("\n\n__",code,":__ ", sep="") cat(productname$product[productname$classification == code]) }
Number of reporting countries by year and by classification type.
completeness %>% select(reporter, year, classification) %>% unique() %>% # Keep only unique combinations of reporter, year, classification group_by(year, classification) %>% dcast(year ~ classification, value.var="reporter", length) %>% kable()
List of countries using each classification type in r max(completeness$year)
.
reporterbyclassif <- completeness %>% filter(year == max(year)) %>% select(year, classification, reporter) %>% unique() # group_by(classification) %>% # do(head(.,2)) # works! how to use this do for (code in unique(reporterbyclassif$classification)){ cat("\n\n__",code,":__ ", sep="") cat(reporterbyclassif$reporter[reporterbyclassif$classification == code], sep=", ") }
# Prepare table to show partner countries # remove world tfdata3 <- tfdata2 %>% filterworldeu28 %>% select(year, classification, flow, reporter, partner) %>% arrange(year, classification, flow) for (r in unique(completeness$reporter)){ cat("\n\n###", r, "\n") # plot completeness p <- ggplot(data=filter(completeness, reporter == r), aes(x = year, y = variable, xend = year + 1, yend = variable, color = value, linetype = value)) + scale_colour_manual(values = c("TRUE" = "blue", "FALSE" = "red")) + scale_linetype_manual(values = c("TRUE" = 2, "FALSE" = 1)) + geom_segment(size=3) + facet_grid(classification~.) + ggtitle(paste("Data completeness of", productcodeinreport, "in", r)) plot(p) ###################################################### # # Number of trade partners per classification and year # ###################################################### # # Reported by the country npartnersbyr <- tfdata3 %>% filter(reporter == r) %>% dcast(year + classification ~ flow, value.var="partner", length) try(print(kable(npartnersbyr, caption = paste("Number of trade partners reported by", r)))) # Reported by partner countries try(npartnersbyothers <- tfdata3 %>% filter(partner == r) %>% dcast(year + classification ~ flow, value.var="reporter", length)) try(print(kable(npartnersbyothers, caption = paste("Number of countries who report trading with", r)))) }
r = "France" # plot completeness p <- ggplot(data=filter(completeness, reporter == r), aes(x = year, y = variable, xend = year + 1, yend = variable, linetype = value)) + scale_colour_manual(values = c("TRUE" = "blue", "FALSE" = "red")) + scale_linetype_manual(values = c("TRUE" = 2, "FALSE" = 1)) + geom_segment(aes(color = value), size=3) + geom_text(aes(y = "classification", label=classification)) + # coord_cartesian(ylim = c(-0.3, 3.5)) + ggtitle(paste("Data completeness of", productcodeinreport, "in", r)) plot(p) createreportfromdb # plot flag ########## pflagfr <- ggplot(data = filter(flagcountry, reporter == r), aes(x= year, y = tradevalue, fill = flag)) + geom_bar(stat = "identity") + facet_grid(flow ~ ., scales = "free_y") plot(pflagfr) pflagfr + aes(y = weight) pflagfr + aes(y = quantity) message("Change flag labels to a short description for the plot") pflag <- ggplot(data = flagregion, aes(x = year, y = tradevalue, fill = flag)) + geom_bar(stat = "identity") + facet_grid(flow ~ regionreporter, scales = "free_y") plot(pflag) pflag + aes(y = weight) pflag + aes(y = quantity) # what are those extreme quantities? bigquantity <-filter(tfdata2, quantity>1e6) %>% mutate(flag = paste("Flag", flag)) ggplot(data = bigquantity, aes(x = reporter,y = quantity, label = partner, color = as.factor(year))) + geom_text() + scale_y_log10() + facet_grid(flag ~ flow) + theme_bw() ggplot(data = bigquantity, aes(x = year, y = quantity, fill = as.factor(flag))) + geom_bar(stat = "identity") + facet_grid(flow ~ regionreporter, scales = "free_y") + ggtitle("It seems all large quantities are flag 0 or 4") ggplot(data = filter(tfdata2, quantity<1e7), aes(x = year, y = quantity, fill = as.factor(flag))) + geom_bar(stat = "identity") + facet_grid(flow ~ regionreporter, scales = "free_y") + ggtitle("quantity < 1e7")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.