plot_tbl_basic.R 2.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#' plot_tbl_basic

#' plot_tbl_basic
#' @param x tbl_friendlyts data.frame
#' @param target target
#' @param group "none","YEAR","MONTH","WDAY","HOUR","MINUTE","SECOND"
#' @param path path
#' @param name name
#' @param DESKTOP boolean
#' @param width_mm width_mm
#' @param height_mm height_mm
#' @param y_name y_name
#' @keywords basicr
#' @export
#' @examples
#' \dontrun{
#' plot_tbl_basic()
#' }
plot_tbl_basic <- function(x,target,group,path=tempdir(),name=NULL,DESKTOP=TRUE,width_mm=200,height_mm=200,y_name="Y-axis"){

22
  if ( (length(group)==1) && (prod(group %in% c("YEAR","MONTH","WDAY","DAY","HOUR","MINUTE","SECOND"))==1) ){
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

    if (!DESKTOP) {
      if(is.null(name)) {
        filename <- normalizePath(file.path(path,paste0("basic_",group,"_",target,".tiff")),mustWork = FALSE)
      } else filename <-normalizePath(file.path(path,paste0("basic_",group,"_",target,"_",name,".tiff")),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()

    names(x)[names(x)==target] <- "targ"

    p <- switch(group,
Jean-Marie Lepioufle's avatar
typo    
Jean-Marie Lepioufle committed
39
40
41
42
43
44
45
      "YEAR"    = ggplot2::ggplot(x) + ggplot2::geom_boxplot(ggplot2::aes(x = YEAR, y = targ, group = YEAR)) + ggplot2::labs(x = "YEAR",  y = y_name),
      "MONTH"   = ggplot2::ggplot(x) + ggplot2::geom_boxplot(ggplot2::aes(x = MONTH, y = targ, group = MONTH)) + ggplot2::labs(x = "MONTH",  y = y_name),
      "WDAY"    = ggplot2::ggplot(x) + ggplot2::geom_boxplot(ggplot2::aes(x = WDAY, y = targ, group = WDAY)) + ggplot2::labs(x = "WDAY",  y = y_name),
      "DAY"     = ggplot2::ggplot(x) + ggplot2::geom_boxplot(ggplot2::aes(x = DAY, y = targ, group = DAY)) + ggplot2::labs(x = "DAY",  y = y_name),
      "HOUR"    = ggplot2::ggplot(x) + ggplot2::geom_boxplot(ggplot2::aes(x = HOUR, y = targ, group = HOUR)) + ggplot2::labs(x = "HOUR",  y = y_name),
      "MINUTE"  = ggplot2::ggplot(x) + ggplot2::geom_boxplot(ggplot2::aes(x = MINUTE, y = targ, group = MINUTE)) + ggplot2::labs(x = "MINUTE",  y = y_name),
      "SECOND"  = ggplot2::ggplot(x) + ggplot2::geom_boxplot(ggplot2::aes(x = SECOND, y = targ, group = SECOND)) + ggplot2::labs(x = "SECOND",  y = y_name),
46
47
48
49
50
51
52
53
54
55
56
      stop("group not recognized"))

    print(p)

    if (!DESKTOP) {
      grDevices::dev.off()
      print(filename)
    } else {}

    return(TRUE)

57
  } else stop("group must be of length 1 and refer to: 'YEAR', 'MONTH', 'WDAY', 'DAY', 'HOUR','MINUTE','SECOND'")
58
59
60


}