"makePeakLegend" <- function(datfile=NA) {
  if(! file.exists(datfile)) {
     warning("Must specify path to an example file for the legend, ",
             "suggest 'MGBT/inst/legend/peaks_for_legend.txt'")
  }
  LG <- read.table(datfile, header=TRUE, colClasses="character")
  LG <- MGBT::makeWaterYear(LG)
  LG <- MGBT::splitPeakCodes(LG)
  LG$peak_va <- as.numeric(LG$peak_va)
  MGBT::plotPeaks(LG, lot=3430, showDubNA=TRUE,
                      ylab="Peak streamflow, in cubic feet per second")
}

pdf("peaks_for_legend.pdf", useDingbats=FALSE)
  makePeakLegend(datfile="./peaks_for_legend.txt")

  legend(1890, 30000,
    c("Streamflow is zero for the year (tagged below bottom axis)",
      "Streamflow is an inequality and line direction indicates less than or greater than",
      "Gap in the annual (by water year) records",
      "Gage height only is available",
      "Double NA entry for discharge and gage height"),  bty="n", cex=0.8,
    pch=c(NA, 16, NA, NA, NA), pt.cex=c(0.8),
    col=c("#006F41", "#FF3C00", "#FFAA00", "#42B3F4", "#EDF8B1"),
    lwd=c(2.1, 0.81, 0.76, 2.1, 3), lty=c(1,1,5,1,1))

  legend(1890, 9000,
    c("Streamflow inclusive of estimated streamflow otherwise lacking discharge qualification codes",
      "Streamflow is a maximum daily average",
      "Streamflow is affected by snowmelt, hurricane, ice-jam, or \n debris-dam breakup"),
    bty="n", cex=0.8, col=c(grey(0.1), 1, 1),
    pt.lwd=c(0.8, 0.8, 0.8), pt.bg=c(grey(0.9), NA, NA),
    pch=c(21, 10, 7), pt.cex=c(0.7, 1.1, 1.9))

  legend(1890, 2300,
    c(paste0("All or part of the record is affected by urbanization, mining, \n",
             " agricultural changes, channelization, or other anthropogenic activity"),
      "Streamflow affected by dam failure",
      "Opportunistic value not from systematic data collection",
      "Streamflow affected to an unknown degree by regulation or diversion",
      "Streamflow is affected by regulation or diversion",
      "Streamflow is a historical peak"), bty="n", cex=0.8,
    pch=c("C", "D", "O", "5", "6", "7"), pt.cex=c(1.3, 2.5, 1.9, 0.8, 0.8, 1.0),
    col=c(4, "#377EB8", "#FF8C00", "#4B284B","#7D287D",6))

  legend(1890, 180,
    c("Low-outlier threshold, if present, from Multiple Grubbs-Beck test or user provided"),
    bty="n", cex=0.8, lwd=0.5, lty=2, col=1)
dev.off()
