plot.R 1.68 KB
Newer Older
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

#' plot_tbl_basic

#' plot_tbl_basic
#' @param x tbl_basic object
#' @param dataset friendlyr data.frame
#' @param target target
#' @param group group character
#' @param path path
#' @param name name
#' @param width_mm width_mm
#' @param height_mm height_mm
#' @keywords diagnr
#' @export
#' @examples
#' \dontrun{
#' plot_tbl_basic()
#' }
plot_tbl_basic <- function(x,dataset,target,group,path=NULL,name=NULL,width_mm=200,height_mm=200){


  if (!is.null(path)) {
    diagnrdir <- normalizePath(file.path(path,"diagnr","figures"),mustWork = FALSE)
    dir.create(diagnrdir, showWarnings = FALSE, recursive = TRUE)

    if(is.null(name)) {
      filename <- normalizePath(tempfile(pattern = "file", tmpdir = diagnrdir, fileext = ".tiff"),mustWork = FALSE)
    } else filename <- normalizePath(file.path(diagnrdir,paste0(name,".tif")),mustWork = FALSE)

    grDevices::tiff(filename = filename, width = width_mm, height = height_mm,
          units = "mm", pointsize = 12,
          compression = "lzw",
          bg = "transparent", res = 300)

  } else grDevices::dev.new()


  # just to get class
  x <- x

  names(dataset)[names(dataset)==target] <- "targ"
  p <- switch(group,
    "HOUR"  = ggplot2::ggplot(dataset) + ggplot2::geom_boxplot(ggplot2::aes(x = HOUR, y = targ)) + ggplot2::labs(x = group,  y = target),
    "WDAY"  = ggplot2::ggplot(dataset) + ggplot2::geom_boxplot(ggplot2::aes(x = wday, y = targ)) + ggplot2::labs(x = group,  y = target),
    "MONTH" = ggplot2::ggplot(dataset) + ggplot2::geom_boxplot(ggplot2::aes(x = MONTH, y = targ)) + ggplot2::labs(x = group,  y = target),
    stop("group not recognized"))

  print(p)

  if (!is.null(path)) {
    grDevices::dev.off()
  } else {}

  return(TRUE)
}