From 0c2bf2d851f431ee9e19c669a7290ff363f236d1 Mon Sep 17 00:00:00 2001
From: "louis.heraut" <louis.heraut@inrae.fr>
Date: Wed, 24 Nov 2021 13:01:29 +0100
Subject: [PATCH] Plot aes

---
 plotting/panel.R | 179 ++++++++++++++++++++++++++++++-----------------
 script.R         |   3 +-
 script_install.R |   4 ++
 3 files changed, 119 insertions(+), 67 deletions(-)

diff --git a/plotting/panel.R b/plotting/panel.R
index db58423..062625c 100644
--- a/plotting/panel.R
+++ b/plotting/panel.R
@@ -6,10 +6,11 @@ library(gridExtra)
 library(gridtext)
 library(dplyr)
 library(grid)
+library(ggh4x)
 
 
 # Time panel
-panel = function (df_data, df_meta, layout_matrix, figdir='', filedir_opt='', filename_opt='', variable='', df_trend=NULL, p_threshold=0.1, unit2day=365.25, type='', missRect=FALSE, time_header=NULL, info_header=TRUE, header_ratio=2) {
+panel = function (df_data, df_meta, layout_matrix, figdir='', filedir_opt='', filename_opt='', variable='', df_trend=NULL, p_threshold=0.1, unit2day=365.25, type='', period=NULL, missRect=FALSE, time_header=NULL, info_header=TRUE, header_ratio=2) {
     
     if (all(class(df_data) != 'list')) {
         df_data = list(df_data)
@@ -99,8 +100,8 @@ panel = function (df_data, df_meta, layout_matrix, figdir='', filedir_opt='', fi
 
         if (!is.null(time_header)) {
             Htime = time_panel(code, time_header, df_trend=NULL,
-                               missRect=TRUE, unit2day=365.25,
-                               type='time')
+                               period=period, missRect=TRUE,
+                               unit2day=365.25, type='Q')
             P[[2]] = Htime
         }
 
@@ -173,11 +174,11 @@ panel = function (df_data, df_meta, layout_matrix, figdir='', filedir_opt='', fi
 
 
 
-time_panel = function (code, df_data, df_trend, missRect, p_threshold, unit2day, type, norm=TRUE, last=FALSE) {
+time_panel = function (code, df_data, df_trend, missRect, p_threshold, unit2day, type, period=NULL, norm=TRUE, last=FALSE) {
    
     df_data_code = df_data[df_data$code == code,] 
 
-    if (type == 'sqrt') {
+    if (type == 'sqrt(Q)') {
         df_data_code$Qm3s = sqrt(df_data_code$Qm3s)
     }
 
@@ -190,23 +191,86 @@ time_panel = function (code, df_data, df_trend, missRect, p_threshold, unit2day,
     if (norm) {
         df_data_code$Qm3s = df_data_code$Qm3s / dbrk
         maxQ = max(df_data_code$Qm3s, na.rm=TRUE)
-        dbrk = 1
+        if (maxQ >= 5) {
+            dbrk = 1.0
+        } else if (maxQ < 5 & maxQ >= 3) {
+            dbrk = 0.5
+        } else if (maxQ < 3 & maxQ >= 2) {
+            dbrk = 0.4
+        } else if (maxQ < 2 & maxQ >= 1) {
+            dbrk = 0.3
+        } else if (maxQ < 1) {
+            dbrk = 0.2
+        }
     }
 
-    dDate = df_data_code$Date[length(df_data_code$Date)] -
-        df_data_code$Date[1]
-    datebreak = round(as.numeric(dDate) / unit2day / 11 , 0)
+    dDate = as.numeric(df_data_code$Date[length(df_data_code$Date)] -
+        df_data_code$Date[1]) / unit2day
+
+    # datebreak = round(as.numeric(dDate) / unit2day / 11 , 0)
     
-    p = ggplot() + theme_bw() +
+    if (dDate >= 100) {
+        datebreak = 25
+        dateminbreak = 5
+    } else if (dDate < 100 & dDate >= 50) {
+        datebreak = 10
+        dateminbreak = 1
+    } else if (dDate < 50) {
+        datebreak = 5
+        dateminbreak = 1
+    }
+    
+    p = ggplot() + 
         
-        ggtitle(type) +
+        # theme_bw() +
         
-        theme(plot.title=element_text(size=10, vjust=-1.5, hjust=1), 
-              axis.title.x=element_blank(),
-              axis.title.y=element_text(size=9)) +
+        ggtitle(bquote(.(type)~'['*m^{3}*'.'*s^{-1}*']  x'~10^{.(as.character(power))})) +
+
+    theme(panel.background=element_rect(fill='white'),
+          text=element_text(family='sans'),
+          panel.border=element_blank(),
+
+          panel.grid.major.y=element_line(color='grey85', size=0.3),
+          panel.grid.major.x=element_blank(),
+          
+          axis.ticks.y=element_blank(),
+          axis.ticks.x=element_line(color='grey75', size=0.3),
+          
+          axis.text.x=element_text(color='grey40'),
+          axis.text.y=element_text(color='grey40'),
+
+          ggh4x.axis.ticks.length.minor=rel(0.5),
+          axis.ticks.length=unit(1.5, 'mm'),
+
+          plot.title=element_text(size=10, vjust=-4, 
+                                  hjust=0, color='grey20'), 
+          axis.title.x=element_blank(),
+          axis.title.y=element_blank(),
+          # axis.title.y=element_text(size=8, color='grey20'),
+          axis.line.x=element_blank(),
+          axis.line.y=element_blank(),
+          )
+
+    if (last) {
+        p = p +
+            theme(plot.margin=margin(1, 5, 5, 5, unit="mm"))
+    } else {
+        p = p +
+            theme(plot.margin=margin(1, 5, 1, 5, unit="mm"))
+    }
         
-        geom_line(aes(x=df_data_code$Date, y=df_data_code$Qm3s),
-                  color='black')
+
+    if (type == 'sqrt(Q)' | type == 'Q') {
+        p = p +
+            geom_line(aes(x=df_data_code$Date, y=df_data_code$Qm3s),
+                      color='grey20')
+    } else {
+        p = p +
+            geom_line(aes(x=df_data_code$Date, y=df_data_code$Qm3s),
+                      color='grey65') +
+            geom_point(aes(x=df_data_code$Date, y=df_data_code$Qm3s),
+                       shape=1, color='grey20', size=1)
+    }
 
     if (missRect) {
         NAdate = df_data_code$Date[is.na(df_data_code$Qm3s)]
@@ -218,10 +282,26 @@ time_panel = function (code, df_data, df_trend, missRect, p_threshold, unit2day,
             geom_rect(aes(xmin=NAdate_Down, 
                           ymin=0, 
                           xmax=NAdate_Up, 
-                          ymax=max(df_data_code$Qm3s, na.rm=TRUE)*1.1),
+                          ymax=maxQ*1.1),
                       linetype=0, fill='Wheat', alpha=0.3)
     }
 
+    if ((type == 'sqrt(Q)' | type == 'Q') & !is.null(period)) {
+            period = as.Date(period)
+            p = p + 
+                geom_rect(aes(xmin=min(df_data_code$Date),
+                              ymin=0, 
+                              xmax=period[1], 
+                              ymax= maxQ*1.1),
+                          linetype=0, fill='grey85', alpha=0.3) +
+                
+                geom_rect(aes(xmin=period[2],
+                              ymin=0, 
+                              xmax=max(df_data_code$Date), 
+                              ymax= maxQ*1.1),
+                          linetype=0, fill='grey85', alpha=0.3) 
+        }
+
     if (!is.null(df_trend)) {
         if (df_trend[df_trend$code == code,]$p < p_threshold) {
 
@@ -239,63 +319,30 @@ time_panel = function (code, df_data, df_trend, missRect, p_threshold, unit2day,
         }}
     
 
-
-
-    if (norm) {
-        p = p +
-            ylab(bquote('débit ['*m^{3}*'.'*s^{-1}*']  x'~10^{.(as.character(power))}))
-    } else {
-        p = p +
-            ylab(expression(paste('débit [', m^{3}, '.', 
-                                  s^{-1}, ']', sep='')))
-    }
+    # if (norm) {
+    #     p = p +
+    #         ylab(bquote('débit ['*m^{3}*'.'*s^{-1}*']  x'~10^{.(as.character(power))}))
+    # } else {
+    #     p = p +
+    #         ylab(expression(paste('débit [', m^{3}, '.', 
+    #                               s^{-1}, ']', sep='')))
+    # }
 
     p = p + 
         # xlab('date') + 
-        scale_x_date(date_breaks=paste(as.character(datebreak),
+        scale_x_date(date_breaks=paste(as.character(datebreak), 
                                        'year', sep=' '),
+                     date_minor_breaks=paste(as.character(dateminbreak), 
+                                             'year', sep=' '),
+                     guide='axis_minor',
                      date_labels="%Y",
                      limits=c(min(df_data_code$Date), 
                               max(df_data_code$Date)),
-                     expand=c(0, 0))
-
-    # geom_text(aes(x=max(df_data_code$Date),
-    #               y=maxQ*1.1, 
-    #               label=type), 
-    #           parse=TRUE,
-    #           hjust='right',
-    #           vjust='top')
-
-    if (type == 'sqrt') {
-        p = p +
-            scale_y_continuous(breaks=seq(0, maxQ*10, dbrk),
-                               # minor_breaks=seq(0, maxQ*10, dbrk/2),
-                               limits=c(0, maxQ*1.1),
-                               expand=c(0, 0))
-    } else if (type == 'time') {
-        p = p +
-            scale_y_continuous(breaks=seq(0, maxQ*10, dbrk),
-                               # minor_breaks=seq(0, maxQ*10, dbrk/2),
-                               limits=c(0, maxQ*1.1),
-                               expand=c(0, 0))
-    } else {
-         p = p +
-             scale_y_continuous(limits=c(0, maxQ*1.1),
-                                expand=c(0, 0))
-    }
-
-    if (last) {
-        p = p +
-            theme(
-                panel.background=element_rect(fill="white"),
-                plot.margin=margin(1, 5, 5, 5, unit="mm"))
-    } else {
-        p = p +
-            theme(
-                panel.background=element_rect(fill="white"),
-                plot.margin=margin(1, 5, 1, 5, unit="mm"))
+                     expand=c(0, 0)) +
         
-    }
+        scale_y_continuous(breaks=seq(0, maxQ*10, dbrk),
+                           limits=c(0, maxQ*1.1),
+                           expand=c(0, 0))
 
     return(p)
 }
diff --git a/script.R b/script.R
index 7af8120..5ad4206 100644
--- a/script.R
+++ b/script.R
@@ -159,7 +159,7 @@ res_VCN10trend = get_VCN10trend(df_data, df_meta, period)
 #       layout_matrix=c(1, 2),
 #       df_meta=df_meta,
 #       missRect=list(TRUE, TRUE), 
-#       type=list('time', 'sqrt'), 
+#       type=list('Q', 'sqrt(Q)'), 
 #       info_header=TRUE,
 #       time_header=NULL,
 #       header_ratio=3,
@@ -174,6 +174,7 @@ panel(list(res_QAtrend$data, res_QMNAtrend$data,
                     res_VCN10trend$trend), 
       type=list(bquote(Q[A]), bquote(Q[MNA]), bquote(V[CN10])),
       missRect=list(TRUE, TRUE, TRUE),
+      period=period,
       info_header=TRUE,
       time_header=df_data,
       header_ratio=2,
diff --git a/script_install.R b/script_install.R
index 8c5527b..ee6c2be 100644
--- a/script_install.R
+++ b/script_install.R
@@ -10,6 +10,10 @@ install.packages('zoo')
 install.packages("qpdf")
 install.packages("gridtext")
 install.packages("lubridate")
+install.packages('ggh4x')
+install.packages('extrafont')
+
+
 
 
 library(devtools)
-- 
GitLab