R/ggsavejj.r

Defines functions ggsavejj

Documented in ggsavejj

#' A redefinition of the ggsave function to save all sorts of graphics for different journals
#'
#' This function applies a simple publication-ready theme
#' @param filename File name, string without ending, e.g. 'myprettyplot'
#' @param p plot object; defaults to the currently opened plot
#' @param device Format (jpg, pdf, eps, etc); defaults to jpg
#' @param dpi dpi of the plot; defaults to 300
#' @param w defined the width of the figure in inch, either a number or a string equal to either "onecol" or "twocol"; defaults to "onecol"
#' @param h defined the height of the figure in inch
#' @param journal target journal; defaults to NA
#' @param s scale of the plot; defaults to 0.8 if the journal is NA and 1 if the journal is specified
#' @param idir directory to save the image in; defaults to "../8-Img"
#' @param bg switch for transparent background, string either "solid" or "transparent"; defaults to "solid"
#' @param secondfile saves a copy of the plot in another file in the same directory with the name "_myprettyplot", while the first file will add the current date at the end fo the filename ("myprettyplot_28Jan2016")
#' @param ... other parameters to be passed to ggsave()
#' @keywords ggplot2, themes, graphics
#' @export
#' @examples
#' graphicsjj


ggsavejj <- function(filename = default_name(plot), p = last_plot(), device = "jpg", dpi = 300, w = "onecol", h = par("din")[2], journal = NA, s = if(is.na(journal)) {.8} else 1, idir = "../8-Img", bg = "solid", secondfile = T, ...){
  require(extrafont)

  today = format(Sys.time(), "%d%b%Y")
  #windowsFonts(font_jj = windowsFont('Lato light'))

  # Let a transparent background automatically lead to png format
  if (bg == "transparent") device <- "png"

  # Format: Powerpoint presentation
  if (is.na(journal)){
    if (w == "onecol") { w = par("din")[1] } else # standard width
                     { w = w }
  } else {
  # Format: BBS (Nov 2014)
    if(journal == "BBS")
      { #tiff or eps
      cat("Saving for BBS\n")
      device <- "png"
      if (w == "onecol") { w = 3.32*3 } else # onecol width:  3.32 in
      if (w == "twocol") { w = 6.91*3 } else # twocol width:  6.91 in
                       { w = w      }
      s = .7
    }
    if (journal == "CognitiveScience")
    {
      cat("Saving for Cognitive Science\n")
      device = "pdf"
      w = 5.5
      s = 1
    }
  }
  # Generate the filename with the date
  fn  = paste0(filename, "_", today, ".", device)
  fn2 = paste0("_", filename, ".", device)
  
  ## Change ggsave to handle multiple plots
  # ggsave <- ggplot2::ggsave; body(ggsave) <- body(ggplot2::ggsave)[-2]
  
  ## Change the font of geom_text
  #print(p)
  #grid.gedit("GRID.text",gp=gpar(fontfamily="fontjj")) #http://stackoverflow.com/questions/4094094
  #p = last_plot()

  # Eps settings
  if (device == "eps")
  {
    Sys.setenv(R_GSCMD = "C:/Program Files (x86)/gs/gs9.05/bin/gswin32.exe")  
  }
  # Embed the fonts if eps or pdf
  if (device == "eps" | device == "pdf")
  {
    #font_import(pattern="Roboto")
    #loadfonts()
    ggsave(fn, plot = p, dpi = dpi, path = idir, width = w, height = h, units = "in", scale = s, device = cairo_pdf, pointsize = 6, ...)
    if (secondfile)
    {
      ggsave(fn2, plot = p, dpi = dpi, path = idir, width = w, height = h, units = "in", scale = s, device = cairo_pdf, pointsize = 6, ...)
    }

  } else {
    ggsave(fn, plot = p, dpi = dpi, path = idir, width = w, height = h, units = "in", scale = s, ...)
    if (secondfile)
    {
      ggsave(fn2, plot = p, dpi = dpi, path = idir, width = w, height = h, units = "in", scale = s, ...)
    }
  }
}
JanaJarecki/ggsavejj documentation built on May 7, 2019, 10:21 a.m.