
  # -------------------------------------------------------------------------------------------------------------------------  #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #           File Name              :  ggggFocusedGraph.r                                                                       #
  #           Last Updated Funclist  :  19 Feb 2015,  1:05 PM (Thursday)                                                       #
  #                                                                                                                            #
  #           Author Name            :  Rick Saporta                                                                           #
  #           Author Email           :  RickSaporta@gmail.com                                                                  #
  #           Author URL             :  www.github.com/rsaporta                                                                #
  #                                                                                                                            #
  #           Packages Called        :  NA                                                                                     #
  #           Packages Used via NS   :  NA                                                                                     #
  #                                                                                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #   ggFocusedGraph       ( DT.means, FocusOnMethod, GorL.string=c("<", "<=", ">", ">="), SmallLargeBreak=1e+05                 #
  #                        , ylab="Time", debug=FALSE )                                                                        #
  #                                                                                                                            #
  #                                                                                                                            #
  #                                                     <END FUNCS>                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  # -------------------------------------------------------------------------------------------------------------------------  #

## NOTES:  This was originally used in 
##         ~/git/DPLYR_vs_DT/src/dplyr vs data.table - 2 Groups.r
##         It is not generally expandable

## EXAMPLE:  Comparing dplyr to data.table
if (FALSE)  
{
  ## COLORS
  colorVals <- c(dplyr="#3CD7DD", data.table="#E56AE7")
  FocusOnMethod <- c("whichmin_seq")  ## Method to focus on.  Ideally, this would be `whichmin` once DPLYR accepts index numbers
  RepsPerBench <- 20L # The `times=` argument in 'microbenchmark()'

  ## plotmath label for Y Axis. Same for all plots. 
  ylab <- sprintf("atop(paste(bold('Time in Seconds'), '\n  (Median of %d Reps Each)'), scriptstyle(bold('Smaller is Better')))", RepsPerBench)


  # P2.LargerData  <- focusedGraph(DT.means=DT.means, FocusOnMethod=FocusOnMethod, GorL.string=">=", SmallLargeBreak=SmallLargeBreak, ylab=ylab, debug=FALSE)
  # P2.SmallerData <- focusedGraph(DT.means=DT.means, FocusOnMethod=FocusOnMethod, GorL.string="<",  SmallLargeBreak=SmallLargeBreak, ylab=ylab, debug=FALSE)

  ## EXAMPLE
  ggFocusedGraph(DT.means=DT.means, FocusOnMethod=FocusOnMethod, GorL.string=">=", SmallLargeBreak=SmallLargeBreak, debug=FALSE)

}


ggFocusedGraph <- function(DT.means, FocusOnMethod, GorL.string=c("<", "<=", ">", ">="), SmallLargeBreak=1e5, ylab="Time", debug=FALSE) {
  # GorL : greater or lesser than function to use

  ## If debugging
  browser(expr=isTRUE(debug), text="Browsing in ggFocusedGraph().")

  ## convert the GorL string to a function
  GorL.string <- match.arg(GorL.string)
  GorL <- match.fun(GorL.string)

  # Filter out to only those N_rows being used
  # Also filtering out any groups where there is only a single observation
  DT.plot <- DT.means[.(FocusOnMethod)][  GorL( N_rows, SmallLargeBreak)  ]

  ## Convert N_groups to factor, formatting the numbers to purtty syntax
  if (!is.factor(DT.plot[["N_groups"]]))
    DT.plot[, N_groups := factor(N_groups, labels=gsub("^\\s+|\\s$", "", formnm(unique(N_groups))))]

  setkeyv(DT.plot, c(key(DT.means), "N_groups"))
  DT.plot <- DT.plot[DT.plot[, .N, by=key(DT.plot)][N > 1]]

  ## Title
  Title <- sprintf("Pre-Sorted (Keyed or Grouped) vs Non-Pre-Sorted\n For methods %s with Rows %s %s"
                  , paste0("'", FocusOnMethod, "'", collapse=" & "),  GorL.string,  formnm(SmallLargeBreak)
                 )

  P <- 
    ggplot(DT.plot) + 
      aes(x=N_rows, y=Median,  color=Package) +
      geom_line(aes(linetype= {if(length(FocusOnMethod) > 1L) paste(ComputeMethod, PreGrouped) else PreGrouped} )) + 
      geom_point() +
      # scale_linetype_manual(values = c("longdash", "solid")) +
      labs( y=bquote(.(parse(text=ylab)))
          , x=bquote(.(parse(text=sprintf("atop('Number of Rows', scriptstyle(%s))", SessionTag))))
          , title=bquote(atop(.(Title)
                  , scriptstyle("Approximate Number of Groups")))
          , linetype= { if(length(FocusOnMethod) > 1L) "Method + PreGrouped" else "PreGrouped" }
          ) + 
      scale_x_continuous(labels=comma) +
      theme(axis.text = element_text(angle=36, size=7, vjust=0.5) )  +
      facet_grid(. ~ N_groups) 

  if (exists("colorVals"))
    P <- P + scale_color_manual(values=colorVals)

  return(P)
}
