R/CumulativeFootprint.R

Defines functions cumulative_footprint

Documented in cumulative_footprint

# Lag-time indicator
#' @title Plot the cumulative footprint at specified levels.
#' @description Function will accumulate the footprint at specified time lags
#' @param CatchData is average catch per year. Columns are ordered as "Year" "se" "sd" and "CPUE". CPUE is the mean CPUE in a givne year.
#' Note the required input can be generated by the CatchTable function
#' @param SelBank a numeric delimiter of the assessment region (1 = Banquereau and 2 = Grand Bank).
#' @param Banksize Coverge in square km of the bank curated by the Catchdata- catch table function.
#' @param stat variable defining whether the plot will have the percent of the bank covered ('percent') or the spatial footprint ('footprint' : DEFAULT)
#' @param lagtime is an integer or vector of integers specifying the desired lag-times in years (default = 5 years)
#' @rdname cumulative_footprint
#' @import ggplot2
#' @importFrom lubridate year
#' @importFrom dplyr summarise mutate ungroup filter
#' @export

cumulative_footprint <- function(CatchData,SelBank,stat="footprint",Banksize=NA,lagtime=5,returnData=FALSE){

  #preamble and warning
  if(!stat %in% c("footprint","percent")){stop("Variable stat must equal 'footprint' or 'percent'")}

  Banksize_NA <- Banksize
  if(is.na(Banksize)){Banksize=ifelse(SelBank==1,10110,49472)}

  if(is.na(Banksize_NA) & SelBank==1){print(paste("Banksize parameter not specified and has been set to default ",Banksize," square km for Banquereau Bank",sep=""))}
    if(is.na(Banksize_NA) & SelBank==2){print(paste("Banksize parameter not specified and has been set to default ",Banksize," square km for Grand Bank",sep=""))}

  is.wholenumber <-function(x, tol = .Machine$double.eps^0.5) { abs(x - round(x)) < tol}

  if(sum(!is.wholenumber(lagtime))>0){stop("Parameter lagtime must be a whole number or vector of whole numbers denoting the desired lagtime(s) in years")}

  #Calculation
  years=unique(CatchData$Year)

  Output=NULL
  for (n in lagtime){
      for(i in 1:length(years))
      {
        if(i<=n)
        {
          tstore <- sum(CatchData[1:i,"Area Dredged (km²)"],na.rm=T)
        } else{
          tstore <- sum(CatchData[(i-(n-1)):i,"Area Dredged (km²)"],na.rm=T)
        } # end else
        tout <- data.frame(year=years[i],lagtime=n,cumulative=tstore)
        Output <- rbind(Output,tout)
      }# end i
  }# end n

 Output$prop <- Output$cumulative/Banksize*100

 maxfoot=ifelse(SelBank==1,250,125)

 indicator=ifelse(stat=="footprint",maxfoot,maxfoot/Banksize*100)

 Output$flagtime <- factor(as.character(Output$lagtime),levels=names(table(Output$lagtime)))

 if(stat=="footprint"& !returnData){

   p1=ggplot(Output,aes(x=year,y=cumulative,col=flagtime,group=flagtime))+
   geom_hline(yintercept=indicator,lty=2)+
   geom_line()+
   geom_point(size=2)+theme_bw()+
   scale_x_continuous(breaks = seq(min(Output$year),max(Output$year),2))+
   theme(legend.justification = c(0, 1), legend.position = c(0, 1),
         axis.text.x=element_text(angle = 45,hjust=1),
         panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
   labs(x="",y=expression(paste("Footprint (",km^2,")",sep="")),
        col="Lag (years)")+scale_color_grey()

   return(p1)
 }

 if(stat=="percent" & !returnData){
   Output$prop <- Output$prop/100
   indicator <- indicator/100
   p1=ggplot(Output,aes(x=year,y=prop,col=flagtime,group=flagtime))+
     geom_hline(yintercept=indicator,lty=2)+
     geom_line()+
     geom_point(size=2)+theme_bw()+
     scale_x_continuous(breaks = seq(min(Output$year),max(Output$year),2))+
     theme(legend.justification = c(0, 1), legend.position = c(0, 1),
           axis.text.x=element_text(angle = 45,hjust=1),
           panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
     labs(x="",y="Footprint",
          col="Lag (years)")+
     scale_color_grey()+scale_y_continuous(labels=percent)
   return(p1)
 }

 if(returnData){return(Output)}

}
SurfClam/bio.surfclam documentation built on June 11, 2020, 4:24 p.m.