R/pam.plot.R

Defines functions pam.plot

pam.plot <- function(p,
                     x.lab = NA,
                     y.lab = NA,
                     plot.title = NA,
                     plot.subtitle = NA,
                     caption = TRUE,
                     axis.titles = FALSE,
                     y.format = "comma",
                     show.legend = TRUE,
                     faceted = NA){

   # Plot title ------------------------------------------------------------------------

   if(is.na(plot.title)){
      p <- p + ggplot2::theme(plot.title = ggplot2::element_blank())
   } else {
      p <- p +
         ggplot2::labs(title = plot.title) +
         ggplot2::theme(
            plot.title = ggplot2::element_text(
               size    = ggplot2::rel(3.25),
               face    = 'bold'
            )
         )
   }

   # Plot subtitle ---------------------------------------------------------------------

   if(is.na(plot.subtitle)){
      p <- p + ggplot2::theme(plot.subtitle = ggplot2::element_blank())
   } else {
      p <- p +
         ggplot2::labs(subtitle  = plot.subtitle) +
         ggplot2::theme(plot.subtitle = ggplot2::element_text(size = ggplot2::rel(2)))
   }


# Caption ---------------------------------------------------------------------------

if(caption){
   p <- p + ggplot2::labs(
         caption = paste('Created', format(Sys.Date(), '%B %d, %Y'))
      )
}

# Legend ----------------------------------------------------------------------------

   if(show.legend){
      p <- p +
         ggplot2::theme(
            legend.position = 'bottom',
            legend.title    = ggplot2::element_blank(),
            legend.text     = ggplot2::element_text(size = ggplot2::rel(1.5))
         )
   } else {
      p <- p + ggplot2::theme(legend.position = 'none')
   }


# Axis titles -----------------------------------------------------------------------

   if(isFALSE(axis.titles)){
      p <- p + ggplot2::theme(axis.title = ggplot2::element_blank())
   } else {
      p <- p +
         ggplot2::labs(x = x.lab, y = y.lab) +
         ggplot2::theme(
            axis.title = ggplot2::element_text(size = ggplot2::rel(1.5))
         )
   }


# Axis text -------------------------------------------------------------------------
   if(y.format == "comma"){
      p <- p + ggplot2::scale_y_continuous(labels = scales::comma)
   }

   p <- p +
      ggplot2::theme(
         axis.text = ggplot2::element_text(size = ggplot2::rel(1.5))
      )

# Facetting -------------------------------------------------------------------------

   p <- p +
      ggplot2::theme(
         strip.text = ggplot2::element_text(size = ggplot2::rel(1.5))
      )

   if(!is.na(faceted)){
      if(faceted == 'row'){
         p <- p + ggplot2::facet_grid(row = ggplot2::vars(variable))
      } else {
         if(faceted  == 'col'){
            p <- p + ggplot2::facet_grid(col = ggplot2::vars(variable))
         }
      }

   }


   return(p)


}
davidallen02/pamr documentation built on July 25, 2020, 3:01 p.m.