kevinwolz / hisafer

An R toolbox for the Hi-sAFe biophysical agroforestry model
6 stars 4 forks source link

Add function to plot daily C allocation in within the tree #110

Closed kevinwolz closed 5 years ago

kevinwolz commented 6 years ago

Here's a start using some testing code. This would be integrated into a similar framework to that used by plot_hisafe_cycle_daily().

YEARS <- seq(1996, 2016, 5)#c(1997, 2002, 2007, 2012, 2017)
test <- hop$trees %>%
  filter(Year %in% YEARS) %>%
  filter(carbonAllocToGrowth > 0) %>%
  filter(idTree == 1)

pheno.dates   <- get_pheno_dates(hop) %>% filter(Year %in% YEARS)
pruning.dates <- get_pruning_dates(hop, "branch") %>% filter(Year %in% YEARS)

test_plot <- function(x) {
  lwd <- 1
  out <- ggplot(x, aes(x = Date, y = value)) +
    labs(x = "Date", y = "Allocation fraction", fill = "Component") +
    facet_grid(SimulationName ~ Year, scales = "free_x") +
    geom_col(na.rm = TRUE, aes(fill = component), width = 1) +
    scale_fill_manual(values = cbPalette) +
    scale_x_date(expand = c(0,0)) +
    scale_y_continuous(expand = c(0,0)) +
    geom_vline(data = pruning.dates, aes(xintercept = Date), size = lwd, linetype = "dashed") +
    geom_vline(data = pheno.dates, aes(xintercept = Date), size = lwd, linetype = "solid") +
    theme_hisafe_ts() +
    theme(legend.position = "bottom",
          aspect.ratio      = 1/2,
          axis.ticks.length = unit((18 * 0.5), "points"),
          axis.text.x       = element_text(margin = margin(t = (18 * 0.3),
                                                           unit = "points"),
                                           angle = 90, hjust = 0.5, vjust = 0.5))
  return(out)
}

## AB ALLOC TEST
ab.test <- test %>%
  select(SimulationName, Year, Date, aboveGroundAllocFrac, belowGroundAllocFrac) %>%
  gather(key = "component", value = "value", aboveGroundAllocFrac, belowGroundAllocFrac) %>%
  mutate(component = factor(component,
                            levels = c("aboveGroundAllocFrac", "belowGroundAllocFrac"),
                            labels = c("Aboveground", "Belowground")))

out <- test_plot(ab.test)

## AG ALLOC TEST
ag.test <- test %>%
  select(SimulationName, Year, Date, aboveGroundAllocFrac, stumpAllocFrac, stemAllocFrac, branchAllocFrac, foliageAllocFrac) %>%
  rename(global = aboveGroundAllocFrac) %>%
  gather(key = "component", value = "value", stumpAllocFrac, stemAllocFrac, branchAllocFrac, foliageAllocFrac) %>%
  mutate(component = factor(component,
                            levels = rev(c("stumpAllocFrac", "stemAllocFrac", "branchAllocFrac", "foliageAllocFrac")),
                            labels = rev(c("Stump", "Stem", "Branches", "Foliage"))))

out <- test_plot(ag.test)

## BG ALLOC TEST
bg.test <- test %>%
  select(SimulationName, Year, Date, belowGroundAllocFrac, stumpAllocFrac, coarseRootAllocFrac, fineRootAllocFrac) %>%
  rename(global = belowGroundAllocFrac) %>%
  gather(key = "component", value = "value", stumpAllocFrac, coarseRootAllocFrac, fineRootAllocFrac) %>%
  mutate(component = factor(component,
                            levels = c("stumpAllocFrac", "coarseRootAllocFrac", "fineRootAllocFrac"),
                            labels = c("Stump", "Coarse roots", "Fine roots")))

out <- test_plot(bg.test)