R/draw_scale_manual.R

#' Change color from categorical colors to diverging/sequential colors
#' 
#' @param method Which method was used to differ variables in a plot? Two possible value: color or fill.
#' @param color Which color group we want to implement? Four possible group: categorical colors,diverging_colors, sequential colors or other color vector. Default value: categorical_colors
#' @param legend_title Title of legend. Default value: "Legend"
#' @examples 
#' \dontrun{
#' ggplot(mtcars, aes(factor(cyl), wt))+
#'      geom_bar(stat = "identity", fill = categorical_colors[1])+
#'      theme_peru()+
#'      draw_scale_numeric("y",0,60)
#'      
#' ggplot(diamonds[1:100,], aes(depth, carat, color = factor(cut)))+
#'      geom_point(size = 7)+
#'      theme_peru("color")+
#'      draw_scale_numeric("y",0,1,0.1,labels_percent = T)
#'      }

draw_scale_manual <- function(method = "", color = categorical_colors, legend_title = "Legend"){
  suppressWarnings(if (method == "fill") {
    if (color == diverging_colors) scale_fill_brewer(name = legend_title, palette = "RdBu")
    else if (color == sequential_colors) scale_fill_brewer(name = legend_title, palette = "Blues")
    else scale_fill_manual(name = legend_title, values = color)
  }
  else if (method == "color"){
    if (color == diverging_colors) scale_color_brewer(name = legend_title, palette = "RdBu")
    else if (color == sequential_colors) scale_color_brewer(name = legend_title, palette = "Blues")
    else scale_color_manual(name = legend_title, values = color)
  }
  )
}
AMUFacultyOfEnglish/PERUanalytics documentation built on May 5, 2019, 11:36 a.m.