# Set same path for knitr evaluation as for interactive use library(knitr) opts_knit$set(root.dir = '../..') opts_chunk$set(fig.width=10)
library(ggplot2) library(dplyr) library(reshape2) # opts_chunk$set(cache=TRUE) # Set global chunk options options(width=80)
FAOSTAT is the source of forest products data. We have loaded this data for another project, reuse it here. May need to update this data?
load("/home/paul/hubic/work/EFI/Y/forestproductsdemand/rawdata/roundwood.Rdata") # EU = read.csv("rawdata/EUCountries.csv") rwd = roundwood$entity # Give a shorter name to the data frame
load("enddata/GFPM_training_scenarios.RDATA") country = "United Kingdom" product = "IndRound" gfpm = subset(trainingScenarios$entity, Country == country & Product == product & Scenario=="Base" )
Could change this to import forestproductsdemand package
calculateConsumptionNetTrade = function(dtf){ # Change NA values to 0 - Not recommended # But makes sence at least that import into Finland and Sweden are 0 dtf[is.na(dtf)] = 0 # Calculate apparent consumption and net trade dtf = mutate(dtf, Consumption = Production + Import_Quantity - Export_Quantity, Net_Trade = Export_Quantity - Import_Quantity) return(dtf) } rwd <- calculateConsumptionNetTrade(rwd) # names tolower? # names(airquality) <- tolower(names(airquality)) rwd <- melt(rwd, id=c("FAOST_CODE", "Country", "Year", "Item"), value.name = "Volume", variable.name = "Element")
rwduk <- filter(rwd, Item=="Roundwood" & Element %in% c("Production", "Import_Quantity", "Export_Quantity", "Consumption", "Net_Trade")& Country=="United Kingdom") ggplot(data=rwduk) + geom_line(aes(x=Year, y=Volume, colour=Element)) + ylab("M3") + theme_bw()
gfpm_product <- subset(gfpm, Element!="DPrice") p = ggplot(data=gfpm_product) + aes(x=Period, y=Volume, colour=Element, label = Element) + ggtitle(paste(country)) + geom_line() + # Subset last period to print country labels on the curves geom_text(data=subset(gfpm_product, Period==max(gfpm_product$Period))) + xlim(1,5.5) print(p)
Naming issues are by-passed because we use only one country, one product and one variable. To merge tables containing many countries and products, we will need correspondance tables between the 2 encoding schemes.
# elements --> we will need a matching table unique(rwduk$Element) unique(gfpm$Element) # Items or product --> We will need a matching table unique(rwduk$Item) unique(gfpm$Product) # Countries or geographical area unique(rwduk$Country) unique(gfpm$Country) # Add gfpm years years <- data.frame(Period=seq(1,5), Year=seq(2010,2030,5)) # There is no roundwood demand! gfpmdemand <- gfpm %>% merge(years) %>% filter(Element=="Demand") %>% select(Country, Year, Volume) %>% mutate(Volume = 1000 * Volume) # Merge by keys: # rwd <- melt(rwd, id=c("FAOST_CODE", "Country", "Year", "Item")) rwdukdemand <- rwduk %>% filter(Element=="Consumption") %>% select(Country, Year, Volume) %>% mutate(Country = "United Kingdom") %>% rbind(gfpmdemand)
Show sawnwood demand for the different years
ggplot(data=rwdukdemand) + aes(x=Year, y=Volume) + geom_line() + ylim(0,NA)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.