R/plotGraph.R

Defines functions plotGraph

Documented in plotGraph

plotGraph = function(plot_data, name, directory, date.from, date.to) {
  #' @title Plot graph from data
  #' @name plotGraph
  #'
  #' @description
  #' \code{plotGraph} plots graph - fans by day
  #'
  #' @param plot_data data.frame with values and dates
  #' @param date.from start date of research
  #' @param date.to end date of research
  #' @param name name of the file
  #' @param directory directory to save the file
  #'
  #' @usage plotGraph(plot_data, name, directory, date.from, date.to)
  #'
  #' @return png with graph
  #' @examples
  #' plot_data = getData(450100695180891,224371541,
  #' 'EAADd6gCguI8BABCa5jew3mZBSV4qjEZAdrYHeDDf8zfwCZAXZCgLr9zXtrU6FsEGdfh44gZB1CMMxo7k7ZAXtal615Pb2r9H6CLfqxhK2JrvVWF3LDykayAToJ34eYlvQyYXbXygTDVrC8c5OoYlXWp4ZAT389vgk0ZD',
  #' '2017-05-01', '2017-05-20')
  #' plotGraph(plot_data, 'fanpage2', 'files', '2017-01-01', '2017-05-01')
  #' @export plotGraph
  #' @import ggplot2
  #' @import grDevices
  #' @importFrom assertthat is.writeable

  if (!(dir.exists(directory))) {
    dir.create(directory)
  }
  if (substr(directory, nchar(directory), nchar(directory)) == '/') {
    png.directory = paste0(directory, name, '.png')
  } else {
    png.directory = paste0(directory, '/', name, '.png')
  }
  try(if(!(is.writeable(png.directory))) stop("Wrong PNG path"))

  graph.temp = ggplot()+
    geom_line(data = plot_data, aes(x = plot_data$date, y = plot_data$value), size = 1.2)+
    #titles
    labs(x = 'Date',
         y = 'Number of fans',
         title = 'Fans on fanpage',
         subtitle = paste0(date.from, ' - ', date.to))+
    #to make sure chart is pretty
    theme(axis.text.x = element_text(family = 'Arial', face = 'bold', colour = 'black', size = 16),
          axis.text.y = element_text(family = 'Arial', face = 'bold', colour = 'black', size = 16),
          axis.title.x = element_text(family = 'Arial', face = 'bold', colour = 'black', size = 24),
          axis.title.y = element_text(family = 'Arial', face = 'bold', colour = 'black', size = 24),
          plot.title = element_text(family = 'Arial', face = 'bold', colour = 'black', size = 30, hjust = .5),
          plot.subtitle = element_text(family = 'Arial', face = 'bold', colour = 'black', size = 24, hjust = .5),
          legend.position = "none",
          panel.grid.major = element_line(color = 'grey'),
          panel.grid.minor = element_blank(),
          panel.border = element_blank(),
          panel.background = element_blank())
  #save to png (resolution 1200x800)
  png(png.directory, 1200, 800)
  print(graph.temp)
  dev.off()

}
karolgileta/FanpageData documentation built on June 24, 2017, 8:41 a.m.