From ff5d708f6b48ec84f22a9f1d524c9d0a78a52478 Mon Sep 17 00:00:00 2001
From: "louis.heraut" <louis.heraut@inrae.fr>
Date: Tue, 23 Nov 2021 17:15:10 +0100
Subject: [PATCH] Plot modularity

---
 plotting/panel.R | 57 ++++++++++++++++++++++++++++++------------------
 1 file changed, 36 insertions(+), 21 deletions(-)

diff --git a/plotting/panel.R b/plotting/panel.R
index e446296..53c0a03 100644
--- a/plotting/panel.R
+++ b/plotting/panel.R
@@ -173,13 +173,25 @@ 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, last=FALSE) {
-    
+time_panel = function (code, df_data, df_trend, missRect, p_threshold, unit2day, type, norm=TRUE, last=FALSE) {
+   
+    df_data_code = df_data[df_data$code == code,] 
+
     if (type == 'sqrt') {
-        df_data[, 'Qm3s'] = apply(df_data[, 'Qm3s'], 1, sqrt)
+        df_data_code$Qm3s = sqrt(df_data_code$Qm3s)
     }
 
-    df_data_code = df_data[df_data$code == code,] 
+    
+    maxQ = max(df_data_code$Qm3s, na.rm=TRUE)
+    power = nchar(as.character(as.integer(maxQ))) - 1
+    dbrk = 10^power
+
+
+    if (norm) {
+        df_data_code$Qm3s = df_data_code$Qm3s / dbrk
+        maxQ = max(df_data_code$Qm3s, na.rm=TRUE)
+        dbrk = 1
+    }
 
     dDate = df_data_code$Date[length(df_data_code$Date)] -
         df_data_code$Date[1]
@@ -219,11 +231,19 @@ time_panel = function (code, df_data, df_trend, missRect, p_threshold, unit2day,
                           color='cornflowerblue')
         }}
     
+
+    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 + 
         # ggtitle(paste(variable, 'station', 
                       # as.character(code), sep=' ')) +
-        ylab(expression(paste('débit [', m^{3}, '.', 
-                              s^{-1}, ']', sep=''))) +
         xlab('date') + 
         scale_x_date(date_breaks=paste(as.character(datebreak),
                                        'year', sep=' '),
@@ -235,28 +255,23 @@ time_panel = function (code, df_data, df_trend, missRect, p_threshold, unit2day,
                  x=as.Date(as.numeric(max(df_data_code$Date))*0.9),
                  y=max(df_data_code$Qm3s, na.rm=TRUE)*1.05, 
                  label=type)
-        
-    if (type == 'sqrt') {   
+
+
+    if (type == 'sqrt') {
         p = p +
-            scale_y_continuous(breaks=seq(0, 100, 10),
-                               minor_breaks=seq(0, 100, 5),
-                               limits=c(0,
-                                        max(df_data_code$Qm3s,
-                                            na.rm=TRUE)*1.1),
+            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, 10000, 1000),
-                               minor_breaks=seq(0, 10000, 500),
-                               limits=c(0, 
-                                        max(df_data_code$Qm3s,
-                                            na.rm=TRUE)*1.1),
+            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, 
-                                         max(df_data_code$Qm3s,
-                                             na.rm=TRUE)*1.1),
+             scale_y_continuous(limits=c(0, maxQ*1.1),
                                 expand=c(0, 0))
     }
 
-- 
GitLab