R/extplot.R

#' extplot
#' 
#' extended plot
#'
#' @param title graph title.
#' @param id id of the graph.
#' @param project  number of the project
#' @param subtitle graph subtitle.
#' @param plot plot, default is last plot.
#' @param display display the plot after run the code, default set to TRUE.
#' @param w width, default is 15.
#' @param h height, default is 10.
#' @param caption caption text, default is Sys.time.
#' @param theme ggtheme, default set to theme_bw.
#' @param extensions vector with desirable extensions.
#'
#' @return 
#' @export
#'
#' @examples
# library(ggplot2)
# qplot(x=c(1:100), y=sample(1:100, 100))
# extplot("x", 1, 1)
extplot<-function (title, id, project, subtitle = "", plot = last_plot(), 
                   display = T, w = 15, h = 10, caption = Sys.time(), theme = theme_minimal(), 
                   extensions = c("pdf", "png")) 
{
  temp <- plot + theme + labs(title = title, subtitle = subtitle, 
                              caption = caption)
  for (ext in seq_along(extensions)) {
    if(extensions[ext]=="pdf"){
    dir.create(paste("./", extensions[ext], sep = ""))
    ggplot2::ggsave(file = paste("./", extensions[ext], "/", extensions[ext], 
                        "_", id, "_", project, ".", extensions[ext], sep = ""), 
           plot = temp, width = w, height = h,useDingbats=FALSE)}
    else{ dir.create(paste("./", extensions[ext], sep = ""))
      ggplot2::ggsave(file = paste("./", extensions[ext], "/", extensions[ext], 
                                   "_", id, "_", project, ".", extensions[ext], sep = ""), 
                      plot = temp, width = w, height = h)}
  }
  if (isTRUE(display)) {
    temp
  }
}
gabrielzanlorenssi/extfunctions documentation built on Dec. 30, 2019, 6:37 p.m.