From a8db14e8b1967781a69abef7aa49de40cb7a3f0f Mon Sep 17 00:00:00 2001
From: "louis.heraut" <louis.heraut@inrae.fr>
Date: Tue, 11 Jan 2022 19:45:37 +0100
Subject: [PATCH] Cleaned and commented

---
 plotting/map.R    |   9 ++-
 plotting/matrix.R | 173 ++++++++++++++++++++++++++++++----------------
 2 files changed, 119 insertions(+), 63 deletions(-)

diff --git a/plotting/map.R b/plotting/map.R
index 5ef6db7..d622a30 100644
--- a/plotting/map.R
+++ b/plotting/map.R
@@ -207,12 +207,17 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp=''
             sizerv = 0.2
         }
 
+        # Stores the coordonate system 
+        cf = coord_fixed()
+        # Makes it the default one to remove useless warning
+        cf$default = TRUE
+
         # Open a new plot with the personalise theme
         map = ggplot() + theme_void() +
             # theme(plot.background=element_rect(fill=NA,
                                                # color="#EC4899")) +
-            # Fixed coordinate system
-            coord_fixed() +
+            # Fixed coordinate system (remove useless warning)
+            cf +
             # Plot the background of France
             geom_polygon(data=df_france,
                          aes(x=long, y=lat, group=group),
diff --git a/plotting/matrix.R b/plotting/matrix.R
index fefdde0..ebf7805 100644
--- a/plotting/matrix.R
+++ b/plotting/matrix.R
@@ -162,7 +162,7 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
 
                 # If the p value is under the threshold
                 if (df_trend_code_per$p <= p_threshold){
-                    # Stores the mean trend
+                    # Stores the averaged trend
                     TrendMean_code[j, i, k] = trendMean
                 # Otherwise
                 } else {
@@ -235,7 +235,7 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
 
                 # If the p value is under the threshold
                 if (df_trend_code_per$p <= p_threshold){
-                    # Gets the color associated to the mean trend
+                    # Gets the color associated to the averaged trend
                     color_res = get_color(trendMean, 
                                           minTrendMean[j, i],
                                           maxTrendMean[j, i],
@@ -269,7 +269,7 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
 
     # If there is a 'mean_period'
     if (!is.null(mean_period)) {
-        # Blank vectors to store info about mean analyses
+        # Blank vectors to store info about breaking analysis
         Periods_mean = c()
         NPeriod_mean = c()
         Type_mean = list()
@@ -290,7 +290,7 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
         dataMeantmp = array(rep(NA, nbp*nCode),
                             dim=c(nbp, nCode))
         
-        # For all mean period
+        # For all period of breaking analysis
         for (j in 1:nPeriod_mean) {
             # For all the code
             for (k in 1:nCode) {
@@ -354,7 +354,7 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
                 }
             }
         }
-        # Computes the min and the max of the mean trend for
+        # Computes the min and the max of the averaged trend for
         # all the station
         minBreakMean = apply(BreakMean_code, c(1, 2),
                              min, na.rm=TRUE)
@@ -372,7 +372,7 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
                 code = Code[k]
                 # For all variable
                 for (i in 1:nbp) {
-                    # Extracts break mean
+                    # Extracts averaged breaking
                     BreakMean = BreakMean_mean[ii]
                     # Gets the color associated
                     color_res = get_color(BreakMean, 
@@ -435,7 +435,7 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
             subFill_trend = Fill_trend[CodefL_trend]
             subColor_trend = Color_trend[CodefL_trend]
 
-            # Same for mean difference analysis
+            # Same for breaking analysis
             CodefL_mean = Code_mean %in% subCode
             # Extracts right info
             subPeriods_mean = Periods_mean[CodefL_mean]
@@ -506,12 +506,12 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
                 # Converts the vector of hydrological variable to
                 # a vector of integer associated to those variable
                 Xtmp = as.integer(factor(as.character(Type_trend_per)))
-                # Computes X position of the column of the period dates
+                # Computes X position of the column for the period dates
                 Xc = j + (j - 1)*nbp*2
-                # Computes X positions of columns of the mean of
+                # Computes X positions of columns for the mean of
                 # variables
                 Xm = Xtmp + (j - 1)*nbp*2 + j
-                # Computes X positions of columns of the mean trend
+                # Computes X positions of columns for the averaged trend
                 X = Xtmp + (j - 1)*nbp*2 + nbp + j
                 
                 # Computes Y positions of each line for each station
@@ -545,20 +545,20 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
                 # For all the variable
                 for (i in 1:length(X)) {
                     mat = mat +
-                        # Plots circles for mean trends
+                        # Plots circles for averaged trends
                         gg_circle(r=0.45, xc=X[i], yc=Y[i],
                                   fill=Fill_trend_per[i],
                                   color=Color_trend_per[i]) +
-                        # Plots circles for mean of variables
+                        # Plots circles for averaged of variables
                         gg_circle(r=0.45, xc=Xm[i], yc=Y[i],
                                   fill='white', color='grey40') +
                         # Plots circles for the column of period dates
                         gg_circle(r=0.45, xc=Xc, yc=Y[i],
                                   fill='white', color='grey40') 
                 }
-                # For all the mean trend on this period
+                # For all averaged trends on this periods
                 for (i in 1:length(TrendMean_trend_per)) {
-                    # Extracts the value of the mean trend
+                    # Extracts the value of the averaged trend
                     trendMean = TrendMean_trend_per[i]
                     # Converts it to the right format with two
                     # significant figures
@@ -574,7 +574,7 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
                         Tcolor = 'grey85'
                     }
                     
-                    # Same for the mean of a variable over
+                    # Same for averaged variables over
                     # the current period
                     dataMean = DataMean_trend_per[i]
                     dataMeanC = signif(dataMean, 2)
@@ -592,8 +592,8 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
                                  size=3, color='grey40')
                 }
                 
+                # Writes a name for the period dates column
                 mat = mat +
-                    # Writes a name for the period dates column
                     annotate('text', x=Xc, y=max(Y) + 0.85,
                              label=bquote(bold('Début')),
                              hjust=0.5, vjust=0.5, 
@@ -618,12 +618,12 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
                                  label=bquote('[%.'*ans^{-1}*']'),
                                  hjust=0.5, vjust=0.5, 
                                  size=2, color='grey40') +
-                        # Writes the type of the mean variable
+                        # Writes the type of the averaged variable
                         annotate('text', x=Xm[i], y=max(Y) + 0.82,
                                  label=bquote('µ'*.(type)),
                                  hjust=0.5, vjust=0.5, 
                                  size=3.25, color='grey20') +
-                        # Writes the unit of the mean variable
+                        # Writes the unit of the averaged variable
                         annotate('text', x=Xm[i], y=max(Y) + 0.6,
                                  label=bquote('['*m^3*'.'*s^{-1}*']'),
                                  hjust=0.5, vjust=0.5, 
@@ -634,18 +634,21 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
                 for (k in 1:nsubCode) {
                     # Gets the code
                     code = subCode[k]
-
+                    # Extracts label for the period dates
                     label = Periods_trend[subNPeriod_trend == j
                                           & subCode_trend == code][1]
+                    # Gets the start and end of the period
+                    # for the station
                     periodStart = substr(label, 1, 4)
                     periodEnd = substr(label, 14, 17)
                     
                     mat = mat +
+                        # Writes the starting value
                         annotate('text', x=Xc, y=k + 0.13,
                                  label=bquote(bold(.(periodStart))),
                                  hjust=0.5, vjust=0.5, 
                                  size=3, color='grey40') + 
-                        
+                        # Writes the ending value
                         annotate('text', x=Xc, y=k - 0.13,
                                  label=bquote(bold(.(periodEnd))),
                                  hjust=0.5, vjust=0.5, 
@@ -654,8 +657,10 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
             }
 
             ### Mean ###
+            # For all the trend period
             for (j in 1:nPeriod_mean) {
-
+                # Extracts the info to plot associated to the
+                # right period
                 Type_mean_per =
                     subType_mean[subNPeriod_mean == j]
                 Code_mean_per =
@@ -668,148 +673,182 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
                     subFill_mean[subNPeriod_mean == j]
                 Color_mean_per =
                     subColor_mean[subNPeriod_mean == j]
-                
-                Xtmp_mean = as.integer(factor(as.character(Type_mean_per)))
 
+                # Converts the vector of hydrological variable to
+                # a vector of integer associated to those variable
+                Xtmp_mean = as.integer(factor(as.character(Type_mean_per)))
+                # Computes X position of the column for the period dates
                 Xc_mean = j + (j - 1)*nbp + X[length(X)]
+                # Computes X positions of columns for the mean of
+                # variables
                 Xm_mean = Xtmp_mean + (j - 1)*nbp + j + X[length(X)]
+                # Computes X positions of columns for the difference of
+                # mean between periods (break)
                 Xr_mean = Xtmp_mean + (j - 1)*nbp*2 + j + X[length(X)]
-                
+
+                # Computes Y positions of each line for each station
                 Y_mean = as.integer(factor(Code_mean_per))
 
+                # Position of a line to delimite periods
                 x = Xc_mean - 0.4
                 xend = Xm_mean[length(Xm_mean)] + 0.25
                 y = height + 1
                 yend = height + 1
-
+                # Drawing of the line
                 mat = mat +
                     annotate("segment",
                              x=x, xend=xend,
                              y=y, yend=yend, 
                              color="grey40", size=0.35)
-                
+
+                # Position of the name of the current period
                 yt = y + 0.15
                 Start = mean_period[[j]][1]
                 End = mean_period[[j]][2]
-                
+                # Name of the period
                 periodName = bquote(bold('Période')~bold(.(as.character(j+nPeriod_trend))))
-
+                # Naming the period
                 mat = mat +
                     annotate("text", x=x, y=yt,
                              label=periodName,
                              hjust=0, vjust=0.5, 
                              size=3, color='grey40')
-                
+
+                # If this is not the first period
                 if (j > 1) {
+                    # Position of a line to delimite results of
+                    # difference of mean bewteen periods
                     x = Xr_mean[1] - 0.4
                     xend = Xr_mean[length(Xr_mean)] + 0.25
-
+                    # Drawing of the line
                     mat = mat +
                         annotate("segment",
                                  x=x, xend=xend,
                                  y=y, yend=yend, 
                                  color="grey40", size=0.35)
-                    
+                    # Naming the breaking columns
                     breakName =  bquote(bold('Écart')~bold(.(as.character(j-1+nPeriod_trend)))*bold('-')*bold(.(as.character(j+nPeriod_trend))))
-                    
+                    # Writes the name
                     mat = mat +
                         annotate("text", x=x, y=yt,
                                  label=breakName,
                                  hjust=0, vjust=0.5, 
                                  size=3, color='grey40')
                 }
-                
+
+                # For all the variable
                 for (i in 1:length(Xm_mean)) {
                     mat = mat +
-                        
+                        # Plots circles for averaged variables
                         gg_circle(r=0.45, xc=Xm_mean[i], yc=Y[i],
                                   fill='white', color='grey40') +
-                        
+                        # Plots circles for the column of period dates
                         gg_circle(r=0.45, xc=Xc_mean, yc=Y[i],
                                   fill='white', color='grey40')
                     
+                    # If this is not the first period
                     if (j > 1) {
                         mat = mat +
+                            # Plots circles for breaking results
                             gg_circle(r=0.45, xc=Xr_mean[i], yc=Y[i],
                                       fill=Fill_mean_per[i],
                                       color=Color_mean_per[i])
                     }
                 }
 
+                # For all averaged variables on this period
                 for (i in 1:length(DataMean_mean_per)) {
-                    dataMean = signif(DataMean_mean_per[i], 2)
-                    
+                    # Extracts values of averaged variables
+                    dataMean = DataMean_mean_per[i]
+                    # Converts it to the right format with two
+                    # significant figures
+                    dataMeanC = signif(dataMean, 2)
+                    # Writes averaged variables values
                     mat = mat +
                         annotate('text', x=Xm_mean[i], y=Y[i],
-                                 label=dataMean,
+                                 label=dataMeanC,
                                  hjust=0.5, vjust=0.5, 
                                  size=3, color='grey40')
-
+                    # If this is not the first period
                     if (j > 1) {
+                        # Extracts values of breaking between periods
                         BreakMean = BreakMean_mean_per[i]
-                        BreakC = signif(BreakMean*100, 2)
-                        
+                        # Converts it to the right format with two
+                        # significant figures
+                        BreakMeanC = signif(BreakMean*100, 2)
+                        # Writes breaking values
                         mat = mat +
                             annotate('text', x=Xr_mean[i], y=Y[i],
-                                     label=BreakC,
+                                     label=BreakMeanC,
                                      hjust=0.5, vjust=0.5, 
                                      size=3, color='white')   
                     }
                 }
-                
+
+                # Writes a name for the period dates column
                 mat = mat +
                     annotate('text', x=Xc_mean, y=max(Y) + 0.85,
                              label=bquote(bold('Début')),
                              hjust=0.5, vjust=0.5, 
                              size=3, color='grey20') + 
-                    
                     annotate('text', x=Xc_mean, y=max(Y) + 0.6,
                              label=bquote(bold('Fin')),
                              hjust=0.5, vjust=0.5, 
                              size=3, color='grey20')
-                # For all variable
+                
+                # For all variables
                 for (i in 1:nbp) {
                     # Extract the variable of the plot
                     type = list_df2plot[[i]]$type
                     mat = mat +
+                        # Writes the type of the averaged variable
                         annotate('text', x=Xm_mean[i], y=max(Y) + 0.82,
                                  label=bquote('µ'*.(type)),
                                  hjust=0.5, vjust=0.5, 
                                  size=3.25, color='grey20') +
-                        
+                        # Writes the unit of the averaged variable
                         annotate('text', x=Xm_mean[i], y=max(Y) + 0.6,
                                  label=bquote('['*m^3*'.'*s^{-1}*']'),
                                  hjust=0.5, vjust=0.5, 
                                  size=2, color='grey40')
-
+                    
+                    # If this is not the first period
                     if (j > 1) {
                         mat = mat +
-                            annotate('text', x=Xr_mean[i], y=max(Y) + 0.82,
+                            # Writes the type of the breaking variable
+                            annotate('text', x=Xr_mean[i],
+                                     y=max(Y) + 0.82,
                                      label=bquote('d'*.(type)),
                                      hjust=0.5, vjust=0.5, 
                                      size=3.25, color='grey20') +
-                            
-                            annotate('text', x=Xr_mean[i], y=max(Y) + 0.6,
+                            # Writes the unit of the breaking variable
+                            annotate('text', x=Xr_mean[i],
+                                     y=max(Y) + 0.6,
                                      label=bquote('[%]'),
                                      hjust=0.5, vjust=0.5, 
                                      size=2, color='grey40')
-                            
                     }
                 }
 
+                # For all the station on the page
                 for (k in 1:nsubCode) {
+                    # Gets the code
                     code = subCode[k]
+                    # Extracts label for the period dates
                     label = Periods_mean[subNPeriod_mean == j
                                          & subCode_mean == code][1]
+                    # Gets the start and end of the period
+                    # for the station
                     periodStart = substr(label, 1, 4)
                     periodEnd = substr(label, 14, 17)
 
                     mat = mat +
+                        # # Writes the starting value
                         annotate('text', x=Xc_mean, y=k + 0.13,
                                  label=bquote(bold(.(periodStart))),
                                  hjust=0.5, vjust=0.5, 
                                  size=3, color='grey40') + 
-                        
+                        # Writes the ending value
                         annotate('text', x=Xc_mean, y=k - 0.13,
                                  label=bquote(bold(.(periodEnd))),
                                  hjust=0.5, vjust=0.5, 
@@ -818,21 +857,30 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
             }
             
             ### Code ###
+            # For all the station
             for (k in 1:nsubCode) {
                 # Gets the code
                 code = subCode[k]
+                # Gets the name of the station
                 name = df_meta[df_meta$code == code,]$nom
+                # Fixes a limit for the max number
+                # of characters available
                 ncharMax = 38
+                # If the number of character of the name is greater
+                # than the limit
                 if (nchar(name) > ncharMax) {
-                    name = paste(substr(name, 1, ncharMax), '...', sep='')
+                    # Cuts the name and add '...'
+                    name = paste(substr(name, 1, ncharMax),
+                                 '...', sep='')
                 }
 
                 mat = mat +
+                    # Writes the code of the station
                     annotate('text', x=0.3, y=k + 0.14,
                              label=bquote(bold(.(code))),
                              hjust=1, vjust=0.5, 
                              size=3.5, color="#00A3A8") +
-                    
+                    # Writes the name of the station
                     annotate('text', x=0.3, y=k - 0.14,
                              label=name,
                              hjust=1, vjust=0.5, 
@@ -843,31 +891,34 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
             mat = mat +
                 # Fixed coordinate system
                 coord_fixed() +
-                
+                # X axis
                 scale_x_continuous(limits=c(1 - rel(6), 
                                             width + rel(0.5)),
                                    expand=c(0, 0)) + 
-                
+                # Y axis
                 scale_y_continuous(limits=c(1 - rel(0.5), 
                                             height + rel(2)),
                                    expand=c(0, 0))
             
-            # Saving matrix plot
+            # Paper format in A3 if needed
             if (A3) {
                 width = 42
                 height = 29.7
                 dpi = 300
+            # Otherwise in A4
             } else {
                 width = 29.7
                 height = 21
                 dpi = 100
             }
-            
+
+            # Saving
             ggsave(plot=mat, 
                    path=outdirTmp,
-                   filename=paste(outnameTmp, '_', fL, imat, '.pdf',
-                                  sep=''),
-                   width=width, height=height, units='cm', dpi=dpi)            
+                   filename=paste(outnameTmp, '_', fL,
+                                  imat, '.pdf',  sep=''),
+                   width=width, height=height,
+                   units='cm', dpi=dpi)            
         }
     }           
 }
-- 
GitLab