knitr::opts_chunk$set(
  echo = FALSE,
  message = FALSE,
  warning = FALSE,
#   dev = "svg",
  fig.width = 12,
  fig.height = 7
  # fig.retina = 3
  )

#########print4plots#############

print4plots<-function(plotlist, datadl, title00, legends = c(T,T,T,T), titles = c(F,F,F,F)){

  b0<-list()
  for (i in 1:length(plotlist)){

    b<-plotlist[i][[1]]
    b<-b+
      geom_line(aes(group = cat), size = 2) +
      theme_classic(base_size = 20) 
    if(legends[i] %in% F) {
      b<-b + theme(legend.position = "none")
    } else {
      b<-b + theme(legend.title = element_blank())
    }
    if(titles[i] %in% F) {
      b<-b + ggtitle("")
    }
    b0<-c(b0, list(b))
    }


  b<-plotlist
  layout <- (b0[1][[1]] + b0[2][[1]]) / (b0[3][[1]] + b0[4][[1]])

  title0<-strsplit(split = "_", x = names(b)[1])[[1]][-c(1,2)]
  title0<-title0[-length(title0)]
  title0<-gsub(pattern = "byr", replacement = "base yr = ", x = title0)
  title0<-paste(title0, collapse = " ")
  title0<-gsub(pattern = " P ", replacement = " Price Method ", x = title0)
  title0<-gsub(pattern = " Q ", replacement = " Quantity Method ", x = title0)

  layout +
    plot_annotation(
    title = title00, 
    subtitle = "",
    caption = paste0(title0, "\n Data downloaded from FOSS ", datadl, ". "),
    theme = theme(plot.title = element_text(size = 20, hjust = 0.5, face = "bold"))
    )
}

#########xunits#############

xunits<-function(temp00, combine=T) {

  temp00<-sum(as.numeric(temp00))
  sigfig<-format(temp00, digits = 3, scientific = TRUE)
  sigfig0<-as.numeric(substr(x = sigfig, start = (nchar(sigfig)-1), stop = nchar(sigfig)))

  if (sigfig0<=5) {
    # if (sigfig0<4) {
    unit<-""
    x<-format(x = temp00, big.mark = ",", digits = 0, scientific = F)
    # } else if (sigfig0>=4 & sigfig0<6) {
    #   unit<-" thousand"
    # x<-round(temp00/1e3, digits = 1)
    # } else if (sigfig0==5) {
    #   unit<-" thousand"
    #   x<-round(temp00/1e3, digits = 0)
  } else if (sigfig0>=6 & sigfig0<9) {
    unit<-" million"
    x<-round(temp00/1e6, digits = 1)
  } else if (sigfig0>=9 & sigfig0<12) {
    unit<-" billion"
    x<-round(temp00/1e9, digits = 1)
  } else if (sigfig0>=12) {
    unit<-" trillion"
    x<-round(temp00/1e12, digits = 1)
  }

  out<-ifelse(combine==T, paste0(x, unit), list(x, unit))

  return(out)
}


#########plotkeyfig#############
plotkeyfig<-function(yr, place, folderpattern, 
                      plotpartnames) { #}, plotyaxis = c(NA, NA, NA, NA)){

  outputrun<-list.files(path = dir.output, pattern = as.character(Date0), full.names = TRUE)
  a<-list.files(path = (list.files(path = paste0(outputrun, "/analyses/"), full.names = TRUE, pattern = folderpattern)), 
                     pattern = "figures", full.names = TRUE, ignore.case = TRUE)
  b<-a[(grepl(pattern =paste0(yr, "To"), x = a))]
  # b<-load(paste0(a, "/AllFigures.rdata"))

  load(file = paste0(b, "/AllFigures.rdata"))

  # if (sum(grepl(pattern = place, x = names(figures.list), ignore.case = T)) != 0) {

    for (i in 1:length(plotpartnames)){
  c0<-figures.list[grepl(pattern = plotpartnames[i], x = names(figures.list), ignore.case = T) & 
                    grepl(pattern = place, x = names(figures.list), ignore.case = T)]

  if (plotpartnames[i] %in% c("QI-Line", "PI-Line")){
    nam<-names(c0)
  aa<-c0[[1]]$data
    for ( ii in 1:length(unique(aa$cat))){
      aaa<-aa[aa$cat %in% unique(aa$cat)[ii],]
      aaa$val<-aaa$val/aaa$val[aaa$Year %in% min(aaa$Year)]
      aa[aa$cat %in% unique(aa$cat)[ii],]<-aaa
    }
    c0[[1]]$data<-aa
    # c0<-list(c0)
    # names(c0)<-nam
  }
  # if (!(is.na(plotyaxis[i]))) {
  #     c0[[1]]$labels$y<-plotyaxis[i]
  # }
  assign(value = c0, x = paste0("c", i))
}


  p<-print4plots(plotlist = c(c1, c2, c3, c4), 
                     datadl = datadl, title00 = "", 
                     legends = c(F,T,F,F))
  # } else {
  #   
  #   title0<-strsplit(split = "/", x = b)
  #   title0<-title0[[1]][length(title0[[1]])-1]
  #   
  #   title0<-strsplit(split = "_", x = title0)[[1]]
  #   # title0<-title0[-length(title0)]
  #   title0<-gsub(pattern = "To", replacement = "-", x = title0)
  #   title0<-gsub(pattern = "byr", replacement = "base yr = ", x = title0)
  #   title0<-paste(title0, collapse = " ")
  #   title0<-gsub(pattern = " P ", replacement = " Price Method ", x = title0)
  #   title0<-gsub(pattern = " Q ", replacement = " Quantity Method ", x = title0)
  #   
  #   p <- ggplot(dat = data.frame(x=1,y=1), aes(x, y, 
  #                                              label = "We could not perform this analysis on this region")) + 
  #     geom_text() + 
  #     theme(panel.background = element_blank(), 
  #           axis.title = element_blank(), 
  #           axis.text = element_blank(), 
  #           axis.ticks = element_blank()) + 
  #     ggtitle(title0)
  #   
  # }


  return(p)
}

#########speciestable#############

speciestable<-function(Date0, yr, maxyr, folderpattern, dir.output){
  outputrun<-list.files(path = dir.output, pattern = as.character(Date0), full.names = TRUE)
  a<-list.files(path = (list.files(path = paste0(outputrun, "/analyses/"), full.names = TRUE, pattern = paste0(yr, "To", maxyr, "_", folderpattern))), 
                     pattern = "outputtables", full.names = TRUE, ignore.case = TRUE)
  b<-a[(grepl(pattern =paste0(yr, "To"), x = a))]
  b<-list.files(path = b, pattern = "_All_", full.names = TRUE)
  b<-b[grep(pattern = "_Species", x = b)]

  # b<-load(paste0(a, "/AllFigures.rdata"))
  a<-read.csv(file = b)

  a<-a[,-c(1,2)]
  a$pct.used<-paste0(a$pct.used, "%")
  names(a)<-c("Region", "Category", "Total Species", "Removed Species", "Used Species", "Percent Used")

library(magrittr)
library(flextable)
a %>% 
  tibble::rownames_to_column() %>% 
  flextable() %>%
  set_header_labels(rowname = "") %>% 
  # add_header_row(values = c("", "Group 1", "Group 2"), 
  #                colwidths = c(1, 2, 2)) %>% 
  theme_zebra() %>% autofit()
}


#########keydatatable#############

keydatatable<-function(Date0, yr, maxyr, folderpattern, dir.output){
  outputrun<-list.files(path = dir.output, pattern = as.character(Date0), full.names = TRUE)
  a<-list.files(path = (list.files(path = paste0(outputrun, "/analyses/"), full.names = TRUE, pattern = paste0(yr, "To", maxyr, "_", folderpattern))), 
                     pattern = "outputtables", full.names = TRUE, ignore.case = TRUE)
  b<-a[(grepl(pattern =paste0(yr, "To"), x = a))]
  b<-list.files(path = b, pattern = "_US_", full.names = TRUE)
  b<-b[grep(pattern = "_Review", x = b)]

  # b<-load(paste0(a, "/AllFigures.rdata"))
  a<-read.csv(file = b)
  rownames(a)<-a$X
  a$X<-NULL

  a<-a[,(grepl(pattern = "PI",x = names(a)) | 
          grepl(pattern = "Q[0-9]+_",x = names(a)) | 
          grepl(pattern = "Q_Total",x = names(a)) | 
          grepl(pattern = "QI",x = names(a)))]

  aa<-cbind.data.frame(data.frame(a[,grepl(pattern = "Total", x = names(a))]), 
                      data.frame(a[,!(grepl(pattern = "Total", x = names(a)))]))
  names(aa)<-c(names(a)[grepl(pattern = "Total", x = names(a))], 
              names(a)[!(grepl(pattern = "Total", x = names(a)))])
  a<-aa
  groups<-unlist(lapply(strsplit(split = "_", x = names(a)), `[[`, 2))
  names(a)<-gsub(pattern = "[0-9]+", replacement = "", x = names(a))
  names(a)<-gsub(pattern = "_[a-zA-Z]+", replacement = "", x = names(a))

  a[,(names(a) == "Q")]<-a[,(names(a) == "Q")]/1e6

  if (sum(names(a) == "PI")>0) {
  for (i in 1:length(which(names(a) == "PI"))){
    a[,which(names(a) == "PI")[i]]<-a[,which(names(a) == "PI")[i]]/a[1,which(names(a) == "PI")[i]]
  }
  }
    for (i in 1:length(which(names(a) == "QI"))){
    a[,which(names(a) == "QI")[i]]<-a[,which(names(a) == "QI")[i]]/a[1,which(names(a) == "QI")[i]]
  }

  g<-t(data.frame(table(groups)))
  g<-g[,match(table = g[1,], x = unique(groups))]

library(magrittr)
library(flextable)
a %>% 
  tibble::rownames_to_column() %>% 
  flextable() %>%
  set_header_labels(rowname = "") %>% 
  add_header_row(values = c("", g[1,]),
                 colwidths = c(1, as.numeric(g[2,]))) %>%
   theme_box() %>% autofit()
}

fold<-list.files(path = list.files(path = list.files(paste0(dir.out, "/analyses/"), 
                                                     pattern = folderpattern, 
                                                     full.names = TRUE), 
                                   pattern = "outputtables", 
                                   full.names = TRUE)[3], 
                 full.names = FALSE)[1]

#Find base yr
fold0<-strsplit(x = fold, split = "_")
a<-unique(paste(lapply(fold0, `[[`, 3)))
baseyr<-(gsub(pattern = "byr", replacement = "", x = a))

#Method
fold<-list.files(paste0(dir.out, "/analyses/"), pattern = folderpattern, full.names = FALSE)

fold0<-strsplit(x = fold, split = "_")
a<-unique(paste(lapply(fold0, `[[`, 3)))



if (a == "P") {
  method0<-"Price Method"
  plotpartnames <- c("Q-Category", "QE-Category", "QI-Line", "PI-Line")
  # plotyaxis<-c("Q ")
} else if (a == "Q") {
  method0<-"Quantity Method"
  plotpartnames <- c("_V-Line", "QI-Category", "QE-Category", "VE-Line")
}
yr<-1950

National r yr-r maxyr (r method0)

plotkeyfig(yr, 
            place = "_US_", 
            folderpattern, plotpartnames)#, plot, plotyaxis)

Species Percents r yr-r maxyr (r method0)

speciestable(Date0, yr, maxyr, folderpattern, dir.output)
yr<-1997

National r yr-r maxyr (r method0)

plotkeyfig(yr, 
            place = "_US_", 
            folderpattern, plotpartnames)#, plotyaxis)

Species Percents r yr-r maxyr (r method0)

speciestable(Date0, yr, maxyr, folderpattern, dir.output)
yr<-2007

National r yr-r maxyr (r method0)

plotkeyfig(yr, 
            place = "_US_", 
            folderpattern, plotpartnames)#, plotyaxis)

Species Percents r yr-r maxyr (r method0)

speciestable(Date0, yr, maxyr, folderpattern, dir.output)

Data r yr-r maxyr (r method0)

Price Index (2007 = 1)

Implicit Quantity (in 2007 $Million)

keydatatable(Date0, yr, maxyr, folderpattern, dir.output)

North Pacific r yr-r maxyr (r method0)

plotkeyfig(yr, 
            place = "_NP_", 
            folderpattern, plotpartnames)#, plotyaxis)

Pacific r yr-r maxyr (r method0)

plotkeyfig(yr, 
            place = "_Pac_", 
            folderpattern, plotpartnames)#, plotyaxis)

Western Pacific r yr-r maxyr (r method0)

plotkeyfig(yr, 
            place = "_WP_", 
            folderpattern, plotpartnames)#, plotyaxis)

New England r yr-r maxyr (r method0)

plotkeyfig(yr, 
            place = "_NE_", 
            folderpattern, plotpartnames)#, plotyaxis)

Mid-Atlantic r yr-r maxyr (r method0)

plotkeyfig(yr, 
            place = "_MA_", 
            folderpattern, plotpartnames)#, plotyaxis)

Northeast r yr-r maxyr (r method0)

plotkeyfig(yr, 
            place = "_NorE_", 
            folderpattern, plotpartnames)#, plotyaxis)

South Atlantic r yr-r maxyr (r method0)

plotkeyfig(yr, 
            place = "_SA_", 
            folderpattern, plotpartnames)#, plotyaxis)

Gulf of Mexico r yr-r maxyr (r method0)

plotkeyfig(yr, 
            place = "_GOM_", 
            folderpattern, plotpartnames)#, plotyaxis)


emilyhmarkowitz/FisheriesEconomicProductivityIndex documentation built on June 13, 2025, 10:31 a.m.