plot_lmrd.R 2.27 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

#' plot_lmrd
# Function adapted from the package lmom
# Draws an L-moment ratio diagram
#' plot_lmrd
#' @param x tbl_lmom data.frame
#' @param path path
#' @param name name
#' @param DESKTOP boolean
#' @param width_mm width_mm
#' @param height_mm height_mm
#' @keywords basicr
#' @export
#' @examples
#' \dontrun{
#' plot_lmrd()
#' }
plot_lmrd<-function(x, path=tempdir(),name=NULL,DESKTOP=TRUE,width_mm=200,height_mm=200) {

  if (inherits(x,"tbl_lmom")) {
    #start plot
    if (!(DESKTOP)) {

      if(is.null(name)) {
        filename <- normalizePath(file.path(path,paste0("lmrd.tiff")),mustWork = FALSE)
      } else filename <-normalizePath(file.path(path,paste0("lmrd_",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()


    path <- try(normalizePath(file.path(.libPaths()[1],"basicr","data","lmrd_n_param_distrib.rda"),mustWork=TRUE))
    load(path)
    lmrd_distrib <- get("lmrd_n_param_distrib",envir=environment())

    lmrd_3 <- lmrd_distrib %>% dplyr::filter(nb_par==3)
    lmrd_2 <- lmrd_distrib %>% dplyr::filter(nb_par==2)

    x_min <- min(0,x$t_3,na.rm=TRUE)
    x_max <- max(0.6,x$t_3,na.rm=TRUE)
    y_min <- min(0,x$t_4,na.rm=TRUE)
    y_max <- max(0.4,x$t_4,na.rm=TRUE)

    # plot
    p <- ggplot2::ggplot() +

         ggplot2::geom_line(  data=lmrd_3, ggplot2::aes(x=t_3,y=t_4,group=distribution,linetype=distribution)) +
         ggplot2::geom_point( data=lmrd_2, ggplot2::aes(x=t_3,y=t_4,group=distribution,shape=distribution),size=2) +
      #   ggplot2::scale_size_continuous(guide = FALSE) +
         ggplot2::scale_shape_discrete(name  ="2-par distribution") +
         ggplot2::scale_linetype_discrete(name  ="3-par distribution") +
         ggplot2::geom_point( data=x, ggplot2::aes(x=t_3,y=t_4,colour=ID),size=2,shape=8) +
         ggplot2::labs(x = expression(italic(L) * "-skewness"),y = expression(italic(L) * "-kurtosis")) +
         ggplot2::xlim(x_min,x_max) +
         ggplot2::ylim(y_min,y_max)

    print(p)

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

  } else stop("x as 'tbl_lmom' object")


}