########################################
# Lineage barplot
# Lineage and nsSNP count barplot
# Lineage Diversity barplot
########################################

lin_count_bp_diversity <- function( lf_data = lin_wf
                          , all_lineages = F
                          , x_categ = "sel_lineages"
                          , y_count = "snp_diversity"
                          #, all_lineages = F
                          , use_lineages = c("L1", "L2", "L3", "L4")
                          #, bar_fill_categ = "count_categ"
                          , display_label_col = "snp_diversity_f"
                          , bar_stat_stype = "identity"
                          , x_lab_angle = 90
                          , d_lab_size = 5
                          , d_lab_hjust = 0.5
                          , d_lab_vjust = 0.5
                          , d_lab_col = "black"
                          , my_xats = 20 # x axis text size
                          , my_yats = 20 # y axis text size
                          , my_xals = 22 # x axis label size
                          , my_yals = 22 # y axis label size
                          , my_lls   = 22 # legend label size
                          , bar_col_labels =  c("Mutations", "Total Samples")
                          , bar_col_values = c("grey50", "gray75")
                          , bar_leg_name = ""
                          , leg_location = "top"
                          , y_log10 = FALSE
                          , y_scale_percent = FALSE
                          #, y_label = c("Count", "SNP diversity")
                          , y_label = c("SNP diversity")
                          ) {
  if(!all_lineages){
    lf_data = lf_data[lf_data[[x_categ]]%in%use_lineages,]
  }
  
  g = ggplot(lf_data
             , aes(  x    = factor( eval(parse(text = x_categ)), ordered = T )
                     , y    = eval(parse(text = y_count))
                     #, fill = eval(parse(text = bar_fill_categ)) 
                     ) )
  
  OutPlot = g + geom_bar( stat          = bar_stat_stype
                          , position    = position_stack(reverse = TRUE)
                          #, alpha    = 1
                          #, colour   = "grey75"
  ) + 
    theme(axis.text.x     = element_text(size = my_xats
                                         , angle = x_lab_angle)
          , axis.text.y   = element_text(size = my_yats
                                         , angle = 90
                                         , hjust = 1
                                         , vjust = 0)
          , axis.title.x = element_text(size     = my_xals
                                        , colour = "black")
          , axis.title.y = element_text(size     = my_yals
                                        , colour = "black")
          , legend.position = leg_location
          , legend.text = element_text(size = my_lls)) + 
    
    geom_label(aes(label = eval(parse(text = display_label_col)))
               , size    = d_lab_size
               , hjust   = d_lab_hjust
               , vjust   = d_lab_vjust
               , colour  = d_lab_col
               , show.legend = FALSE
               #, check_overlap = TRUE
               , position = position_stack(reverse = T)) + 

    scale_fill_manual(values   = bar_col_values
                      , name   = bar_leg_name
                      , labels = bar_col_labels) +
    labs(title    = ""
         , x      = ""
         , y      = y_label
         , colour = "black")
    
  if (y_log10){
    
   OutPlot = OutPlot + 
     scale_y_continuous(trans = "log10"
                        , labels = trans_format("log10", math_format(10^.x) ) )
   }
  
  if (y_scale_percent){
    
    OutPlot = OutPlot +
      scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
      #scale_y_continuous(labels = scales::percent) +
      
      labs(title    = ""
           , x      = ""
           , y      = y_label
           , colour = "black")
  }
  
  return(OutPlot)
}
