R implementation of the Greenhouse Gas Value Calculator
Citation: Kristina J. Teixeira and Evan H. Delucia 2011. The greenhouse gas value of ecosystems. Global Change Biology. 17(1):425–438 doi: 10.1111/j.1365-2486.2010.02220.x
inst/config.xml
example input fileinst/extdata/ghgvc1.Rdata
all objects used and provided by ghgvc 1.0inputs.Rdata
example of inputs as R objects for ghgvcr example (below)inst/extdata/multipft_input.xml
produced by example below:
inst/extdata/output.csv
inst/extdata/output.json
The bash and R code snippets below install dependencies, and only need to be run once.
sudo apt-get install git sudo apt-get install libcurl4-openssl-dev # dependency of Rcurl, git clone https://github.com/dlebauer/pecan.git pecan git clone https://github.com/dlebauer/ghgvcR.git ghgvcR R
install.packages(c("devtools", "roxygen2"), repos= "http://cran.us.r-project.org") library(devtools) install(ghgvcr) install(pecan/utils)
./src/ghgvc_script.R
library(ghgvcr)
options(warn=FALSE) #test("../ghgvcR") #example(ghgvcr) ## the following is equivalent to config.xml <- system.file("config.xml", package = "ghgvcr") config.list <- xmlToList(xmlParse(config.xml)) ecosystem_data <- config.list$ecosystem_data x <- ghgvcr::ghgvc(options = config.list$options, ecosystem_data = config.list$ecosystem_data) writeLines(x, "inst/extdata/output.json") write.csv(as.data.frame(fromJSON(x)), "inst/extdata/output.csv")
multisite_config.xml <- system.file("multisite_config.xml", package = "ghgvcr") multipft_config.list <- xmlToList(xmlParse(multipft_config.xml)) x2 <- ghgvcr::ghgvc2(multipft_config.list) writeLines(x2, "inst/extdata/multipft_output.json") write.csv(as.data.frame(fromJSON(x2)), "inst/extdata/multipft_output.csv")
library(ggplot2) # number of ecosystems: n.ecosystems <- length(names(ecosystem_data)) for(i in 1:n.ecosystems){ result <- ecosystem_data[[i]] ecosystem.name <- result$name if(i == 1){ result.df <- as.data.frame(result) } else { result.df <- rbind(result.df, as.data.frame(result)) } rownames(result.df)[i] <- gsub(" ", "", ecosystem.name) } # identify cols with numbers result.num <- suppressWarnings(as.numeric(result)) num.logical <- !(is.na(result.num) | result.num == -9999) result.df <- result.df[,!(result.num == -9999 | is.na(result.num))] # transpose data.frame for plotting: result.tdf <- cbind(variable = names(result.df), as.data.frame(t(result.df))) forcings.index <- grepl("F", names(result.df)) forcings.names <- names(result.df)[forcings.index] forcings <- result.tdf[forcings.index,] forcings.long <- melt(forcings, id.vars= "variable") colnames(forcings.long) <- c("variable", "ecosystem", "value")
ggplot(data = forcings.long, aes(x = variable, y = value, fill = ecosystem)) + geom_bar(position="dodge", stat = "identity") + ggtitle(label="Example plot: values of F for two ecosystems") + xlab("Variable") + ylab("Units of F") + coord_flip()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.