PredictiveEcology / Biomass_core

Core vegetation dynamics module for LandR. based on LANDIS-II Biomass Succession Extension v3.2.1
Other
2 stars 4 forks source link

Create and output background mortality plots #68

Open CeresBarros opened 2 years ago

CeresBarros commented 2 years ago

A plot tracking biomass loss (per species or across the landscape) would be a good addition to B_core. Here's a code snippet done post-simulation -- this one plots total B, total aNPP and total mortality across the landscape per year

library(data.table)
library(raster)
library(ggplot2)
library(LandR)

files <- list.files(file.path("R/SpaDES/outputs/mar2022Runs/noPM"), "cohortData", full.names = TRUE)
files <- grep(paste("year", seq(2011, 2111, 5), sep = "", collapse = "|"), files, value = TRUE)

files2 <- list.files(file.path("R/SpaDES/outputs/mar2022Runs/noPM"), "pixelGroupMap", full.names = TRUE)
files2 <- grep(paste("year", seq(2011, 2111, 5), sep = "", collapse = "|"), files2, value = TRUE)

allCohortDataMarch <- mapply(cdfile = files, pgfile = files2, FUN = function(cdfile, pgfile) {
  if (as.integer(sub("\\.rds", "", sub(".*year", "", cdfile))) ==
      as.integer(sub("\\.rds", "", sub(".*year", "", pgfile)))) {
    dat <- readRDS(cdfile)
    map <- readRDS(pgfile)
    dat$year <- as.integer(sub("\\.rds", "", sub(".*year", "", cdfile)))
    dat <- addPixels2CohortData(cohortData = dat, pixelGroupMap = map)
    return(dat)
  } else stop("file years don't match")
}, SIMPLIFY = FALSE)
allCohortDataMarch <- rbindlist(allCohortDataMarch, use.names = TRUE)

allCohortDataMarch[, totalB := sum(B), by = .(pixelIndex, year)]
allCohortDataMarch[, totalMort := sum(mortality), by = .(pixelIndex, year)]
allCohortDataMarch[, totalANPP := sum(aNPPAct), by = .(pixelIndex, year)]

plotData <- melt(allCohortDataMarch, measure.vars = c("totalB", "totalMort", "totalANPP"),
                  variable.name = "variable", value.name = "B")

plot1 <- ggplot(plotData , aes(x = year, y = B.1, colour = variable)) +
  stat_summary(fun = "sum", geom = "line", size = 1) +
  theme_classic() 
CeresBarros commented 2 years ago

I just remembered we already have a total B, total aNPP and average age plot (across landscape and species) being produced by B_core. Total mortality could be simply added to that one -- I believe this would be in the plotAvgVegAttributes event function