diff --git a/plotting/matrix.R b/plotting/matrix.R index b4e37f050b06d48523e5b275f964d11c7f9aeb4c..c043b1882b83049c4ad1ee0f324a19eccc9bd9e2 100644 --- a/plotting/matrix.R +++ b/plotting/matrix.R @@ -407,562 +407,595 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice slice = nCode } - - Type = levels(factor(Type_trend)) #### - - - # Extracts each possibilities of first letter of station code - firstLetter = levels(factor(substr(Code, 1, 1))) - # Number of different first letters - nfL = length(firstLetter) - # For all the available first letter - for (ifL in 1:nfL) { - # Gets the first letter - fL = firstLetter[ifL] - # Print the matrix name - print(paste('Matrix for region : ', fL, - " (", round(ifL/nfL*100, 1), " %)", - sep='')) - - # Get only station code with the same first letter - subCodefL = Code[substr(Code, 1, 1) == fL] - # Counts the number of station in it - nsubCodefL = length(subCodefL) - # Computes the number of pages needed to plot all stations - nMat = as.integer(nsubCodefL/slice) + 1 - # For all the pages - for (imat in 1:nMat) { - # Extracts the station for the current page - subCode = subCodefL[(slice*(imat-1)+1):(slice*imat)] - # Removes NA stations - subCode = subCode[!is.na(subCode)] - # Reverses verticale order of stations - subCode = rev(subCode) - # Gets the number of station for the page - nsubCode = length(subCode) - - # Creates logical vector to select only info about - # stations that will be plot on the page - CodefL_trend = Code_trend %in% subCode - # Extracts those info - subPeriods_trend = Periods_trend[CodefL_trend] - subNPeriod_trend = NPeriod_trend[CodefL_trend] - subVar_trend = Var_trend[CodefL_trend] - subType_trend = Type_trend[CodefL_trend] - subCode_trend = Code_trend[CodefL_trend] - subPthresold_trend = Pthresold_trend[CodefL_trend] - subTrendMean_trend = TrendMean_trend[CodefL_trend] - subDataMean_trend = DataMean_trend[CodefL_trend] - subFill_trend = Fill_trend[CodefL_trend] - subColor_trend = Color_trend[CodefL_trend] - - # Same for breaking analysis - CodefL_mean = Code_mean %in% subCode - # Extracts right info - subPeriods_mean = Periods_mean[CodefL_mean] - subNPeriod_mean = NPeriod_mean[CodefL_mean] - subVar_mean = Var_mean[CodefL_mean] - subType_mean = Type_mean[CodefL_mean] - subCode_mean = Code_mean[CodefL_mean] - subDataMean_mean = DataMean_mean[CodefL_mean] - subBreakMean_mean = BreakMean_mean[CodefL_mean] - subFill_mean = Fill_mean[CodefL_mean] - subColor_mean = Color_mean[CodefL_mean] - - # Extracts the name of the currently hydrological - # region plotted - title = df_meta[df_meta$code == subCode[1],]$region_hydro - - ### Plot ### - # Fixes the height and width of the table according to - # the number of station and the number of column to draw - height = nsubCode - width = nbp * 2 * nPeriod_trend + nPeriod_trend + nPeriod_mean * nbp + nPeriod_mean + nbp - - # Fixes the size of the plot area to keep proportion right - options(repr.plot.width=width, repr.plot.height=height) - - # Open a new plot with a personalise theme - mat = ggplot() + theme_ash + - # Modification of theme in order to remove axis - theme( - panel.border=element_blank(), - axis.text.x=element_blank(), - axis.text.y=element_blank(), - axis.ticks.y=element_blank(), - axis.ticks.x=element_blank(), - axis.title.y=element_blank(), - plot.margin=margin(t=5, r=5, b=5, l=5, unit="mm") - ) - # Postion and name of the title - xt = 1 - 6 - yt = height + 2 - Title = bquote(bold(.(title))) - # Writes the title - mat = mat + - annotate("text", x=xt, y=yt, - label=Title, - hjust=0, vjust=1, - size=6, color="#00A3A8") - - ### Trend ### - # For all the trend period - for (j in 1:nPeriod_trend) { - # Extracts the info to plot associated to the - # right period - Periods_trend_per = - subPeriods_trend[subNPeriod_trend == j] - NPeriods_trend_per = - subNPeriod_trend[subNPeriod_trend == j] - Var_trend_per = - subVar_trend[subNPeriod_trend == j] - Type_trend_per = - subType_trend[subNPeriod_trend == j] - Code_trend_per = - subCode_trend[subNPeriod_trend == j] - Pthresold_trend_per = - subPthresold_trend[subNPeriod_trend == j] - TrendMean_trend_per = - subTrendMean_trend[subNPeriod_trend == j] - DataMean_trend_per = - subDataMean_trend[subNPeriod_trend == j] - Fill_trend_per = - subFill_trend[subNPeriod_trend == j] - Color_trend_per = - subColor_trend[subNPeriod_trend == j] - - # Converts the variable list into levels for factor - levels = unlist(Var_trend_per) - # Converts the vector of hydrological variable to - # a vector of integer associated to those variable - Xtmp = as.integer(factor(as.character(Var_trend_per), - levels=levels)) - - # Computes X position of the column for the period dates - Xc = j + (j - 1)*nbp*2 - # Computes X positions of columns for the mean of - # variables - Xm = Xtmp + (j - 1)*nbp*2 + j - # Computes X positions of columns for the averaged trend - X = Xtmp + (j - 1)*nbp*2 + nbp + j + # Gets all the different type of plots + Type = levels(factor(Type_trend)) + nbType = length(Type) + # For all the type of plots + for (itype in 1:nbType) { + # Gets the type + type = Type[itype] + + # Extracts each possibilities of first letter of station code + firstLetter = levels(factor(substr(Code, 1, 1))) + # Number of different first letters + nfL = length(firstLetter) + # For all the available first letter + for (ifL in 1:nfL) { + # Gets the first letter + fL = firstLetter[ifL] + + # Get only station code with the same first letter + subCodefL = Code[substr(Code, 1, 1) == fL] + # Counts the number of station in it + nsubCodefL = length(subCodefL) + # Computes the number of pages needed to plot all stations + nMat = as.integer(nsubCodefL/slice) + 1 + # For all the pages + for (iMat in 1:nMat) { + # Print the matrix name + print(paste('Matrix ', iMat, '/', nMat, + ' of ', type, + ' for region : ', fL, + " (", + round((ifL + nfL*(itype-1)) / (nfL*2) * 100, + 1), + " %)", + sep='')) - # Computes Y positions of each line for each station - Y = as.integer(factor(Code_trend_per)) - # Reverses vertical order of stations - Y = rev(Y) - - # Position of a line to delimite periods - x = Xc - 0.4 - xend = X[length(X)] + 0.25 - y = height + 1.1 - yend = height + 1.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 = trend_period[[j]][1] - End = trend_period[[j]][2] - # Name of the period - periodName = bquote(bold('Période')~bold(.(as.character(j)))) - # Naming the period + # Extracts the station for the current page + subCode = subCodefL[(slice*(iMat-1)+1):(slice*iMat)] + # Removes NA stations + subCode = subCode[!is.na(subCode)] + # Reverses verticale order of stations + subCode = rev(subCode) + # Gets the number of station for the page + nsubCode = length(subCode) + + # Creates logical vector to select only info about + # stations that will be plot on the page + CodefL_trend = + Code_trend %in% subCode & Type_trend == type + # Extracts those info + subPeriods_trend = Periods_trend[CodefL_trend] + subNPeriod_trend = NPeriod_trend[CodefL_trend] + subVar_trend = Var_trend[CodefL_trend] + subType_trend = Type_trend[CodefL_trend] + subCode_trend = Code_trend[CodefL_trend] + subPthresold_trend = Pthresold_trend[CodefL_trend] + subTrendMean_trend = TrendMean_trend[CodefL_trend] + subDataMean_trend = DataMean_trend[CodefL_trend] + subFill_trend = Fill_trend[CodefL_trend] + subColor_trend = Color_trend[CodefL_trend] + + # Same for breaking analysis + CodefL_mean = + Code_mean %in% subCode & Type_mean == type + # Extracts right info + subPeriods_mean = Periods_mean[CodefL_mean] + subNPeriod_mean = NPeriod_mean[CodefL_mean] + subVar_mean = Var_mean[CodefL_mean] + subType_mean = Type_mean[CodefL_mean] + subCode_mean = Code_mean[CodefL_mean] + subDataMean_mean = DataMean_mean[CodefL_mean] + subBreakMean_mean = BreakMean_mean[CodefL_mean] + subFill_mean = Fill_mean[CodefL_mean] + subColor_mean = Color_mean[CodefL_mean] + + # Gets the number of variable to plot in + # function of the current type + nbpMod = + length(levels(factor(subVar_trend))) + + ### Plot ### + # Fixes the height and width of the table according to + # the number of station and the number of column to draw + height = nsubCode + width = nbpMod * 2 * nPeriod_trend + nPeriod_trend + nPeriod_mean * nbpMod + nPeriod_mean + nbpMod + + # Fixes the size of the plot area to keep proportion right + options(repr.plot.width=width, repr.plot.height=height) + + # Open a new plot with a personalise theme + mat = ggplot() + theme_ash + + # Modification of theme in order to remove axis + theme( + panel.border=element_blank(), + axis.text.x=element_blank(), + axis.text.y=element_blank(), + axis.ticks.y=element_blank(), + axis.ticks.x=element_blank(), + axis.title.y=element_blank(), + plot.margin=margin(t=5, r=5, b=5, l=5, unit="mm") + ) + + # Extracts the name of the currently hydrological + # region plotted + title = df_meta[df_meta$code == subCode[1],]$region_hydro + + subtitle = paste(' ', iMat, '/', nMat,' ', type, + sep='') + # Postion and name of the title + xt = 1 - 6 + yt = height + 2 + Title = bquote(bold(.(title))[.(subtitle)]) + # Writes the title mat = mat + - annotate("text", x=x, y=yt, - label=periodName, - hjust=0, vjust=0.5, - size=3, color='grey40') - - # For all the variable - for (i in 1:length(X)) { - mat = mat + - # 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 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 averaged trends on this periods - for (i in 1:length(TrendMean_trend_per)) { - # Extracts the value of the averaged trend - trendMean = TrendMean_trend_per[i] - # Converts it to the right format with two - # significant figures - trendMeanC = signif(trendMean*100, 2) - - # If it is significative - if (!is.na(Pthresold_trend_per[i])) { - # The text color is white - Tcolor = 'white' - # Otherwise - } else { - # The text is grey - Tcolor = 'grey85' - } + annotate("text", x=xt, y=yt, + label=Title, + hjust=0, vjust=1, + size=6, color="#00A3A8") + + ### Trend ### + # For all the trend period + for (j in 1:nPeriod_trend) { + # Extracts the info to plot associated to the + # right period + Periods_trend_per = + subPeriods_trend[subNPeriod_trend == j] + NPeriods_trend_per = + subNPeriod_trend[subNPeriod_trend == j] + Var_trend_per = + subVar_trend[subNPeriod_trend == j] + Type_trend_per = + subType_trend[subNPeriod_trend == j] + Code_trend_per = + subCode_trend[subNPeriod_trend == j] + Pthresold_trend_per = + subPthresold_trend[subNPeriod_trend == j] + TrendMean_trend_per = + subTrendMean_trend[subNPeriod_trend == j] + DataMean_trend_per = + subDataMean_trend[subNPeriod_trend == j] + Fill_trend_per = + subFill_trend[subNPeriod_trend == j] + Color_trend_per = + subColor_trend[subNPeriod_trend == j] + + # Converts the variable list into levels for factor + levels = unlist(Var_trend_per) + # Converts the vector of hydrological variable to + # a vector of integer associated to those variable + Xtmp = as.integer(factor(as.character(Var_trend_per), + levels=levels)) + + # Computes X position of the column for + # the period dates + Xc = j + (j - 1)*nbpMod*2 + # Computes X positions of columns for + # the mean of variables + Xm = Xtmp + (j - 1)*nbpMod*2 + j + # Computes X positions of columns for + # the averaged trend + X = Xtmp + (j - 1)*nbpMod*2 + nbpMod + j - # Same for averaged variables over - # the current period - dataMean = DataMean_trend_per[i] - dataMeanC = signif(dataMean, 2) + # Computes Y positions of each line for each station + Y = as.integer(factor(Code_trend_per)) + # Reverses vertical order of stations + Y = rev(Y) + + # Position of a line to delimite periods + x = Xc - 0.4 + xend = X[length(X)] + 0.4 + y = height + 1.1 + yend = height + 1.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 = trend_period[[j]][1] + End = trend_period[[j]][2] + # Name of the period + periodName = + bquote(bold('Période')~bold(.(as.character(j)))) + # Naming the period mat = mat + - # Writes the mean trend - annotate('text', x=X[i], y=Y[i], - label=trendMeanC, - hjust=0.5, vjust=0.5, - size=3, color=Tcolor) + - # Writes the mean of the associated variable - annotate('text', x=Xm[i], y=Y[i], - label=dataMeanC, - hjust=0.5, vjust=0.5, + annotate("text", x=x, y=yt, + label=periodName, + hjust=0, vjust=0.5, size=3, color='grey40') - } - - # Writes a name for the period dates column - mat = mat + - annotate('text', x=Xc, y=max(Y) + 0.9, - label=bquote(bold('Début')), - hjust=0.5, vjust=0.5, - size=3, color='grey20') + - annotate('text', x=Xc, y=max(Y) + 0.63, - label=bquote(bold('Fin')), - hjust=0.5, vjust=0.5, - size=3, color='grey20') - - # For all variable - for (i in 1:nbp) { - # Extract the variable of the plot - var = list_df2plot[[i]]$var - mat = mat + - # Writes the unit of the variable - annotate('text', x=X[i], y=max(Y) + 0.63, - label=bquote('[%.'*ans^{-1}*']'), - hjust=0.5, vjust=0.5, - size=2, color='grey40') + - # Writes the type of the variable - annotate('text', x=X[i], y=max(Y) + 0.9, - label=bquote(.(var)), - hjust=0.5, vjust=0.5, - size=3.25, color='grey20') + - # Writes the unit of the averaged variable - annotate('text', x=Xm[i], y=max(Y) + 0.63, - label=bquote('['*m^3*'.'*s^{-1}*']'), - hjust=0.5, vjust=0.5, - size=2, color='grey40') + - # Writes the type of the averaged variable - annotate('text', x=Xm[i], y=max(Y) + 0.9, - label=expr(bar(!!var)), - hjust=0.5, vjust=0.5, - size=3.25, color='grey20') - } - # 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_trend_per[Code_trend_per == code][1] - # Gets the start and end of the period - # for the station - periodStart = substr(label, 1, 4) - periodEnd = substr(label, 14, 17) + # For all the variable + for (i in 1:length(X)) { + mat = mat + + # 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 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 averaged trends on this periods + for (i in 1:length(TrendMean_trend_per)) { + # Extracts the value of the averaged trend + trendMean = TrendMean_trend_per[i] + # Converts it to the right format with two + # significant figures + trendMeanC = signif(trendMean*100, 2) + + # If it is significative + if (!is.na(Pthresold_trend_per[i])) { + # The text color is white + Tcolor = 'white' + # Otherwise + } else { + # The text is grey + Tcolor = 'grey85' + } + + # Same for averaged variables over + # the current period + dataMean = DataMean_trend_per[i] + dataMeanC = signif(dataMean, 2) + + mat = mat + + # Writes the mean trend + annotate('text', x=X[i], y=Y[i], + label=trendMeanC, + hjust=0.5, vjust=0.5, + size=3, color=Tcolor) + + # Writes the mean of the associated variable + annotate('text', x=Xm[i], y=Y[i], + label=dataMeanC, + hjust=0.5, vjust=0.5, + size=3, color='grey40') + } + # Writes a name for the period dates column mat = mat + - # Writes the starting value - annotate('text', x=Xc, y=k + 0.13, - label=bquote(bold(.(periodStart))), + annotate('text', x=Xc, y=max(Y) + 0.9, + label=bquote(bold('Début')), 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))), + size=3, color='grey20') + + annotate('text', x=Xc, y=max(Y) + 0.63, + label=bquote(bold('Fin')), hjust=0.5, vjust=0.5, - size=3, color='grey40') + size=3, color='grey20') + + # For all variable + for (i in 1:nbpMod) { + # Extract the variable of the plot + var = subVar_trend[i] + mat = mat + + # Writes the unit of the variable + annotate('text', x=X[i], y=max(Y) + 0.63, + label=bquote('[%.'*ans^{-1}*']'), + hjust=0.5, vjust=0.5, + size=2, color='grey40') + + # Writes the type of the variable + annotate('text', x=X[i], y=max(Y) + 0.9, + label=bquote(.(var)), + hjust=0.5, vjust=0.5, + size=3.25, color='grey20') + + # Writes the unit of the averaged variable + annotate('text', x=Xm[i], y=max(Y) + 0.63, + label=bquote('['*m^3*'.'*s^{-1}*']'), + hjust=0.5, vjust=0.5, + size=2, color='grey40') + + # Writes the type of the averaged variable + annotate('text', x=Xm[i], y=max(Y) + 0.9, + label=expr(bar(!!var)), + hjust=0.5, vjust=0.5, + size=3.25, color='grey20') + } + + # 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_trend_per[Code_trend_per == 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, + size=3, color='grey40') + } } - } - ### Mean ### - # For all the trend period - for (j in 1:nPeriod_mean) { - # Extracts the info to plot associated to the - # right period - Periods_mean_per = - subPeriods_mean[subNPeriod_mean == j] - NPeriods_mean_per = - subNPeriod_mean[subNPeriod_mean == j] - Var_mean_per = - subVar_mean[subNPeriod_mean == j] - Type_mean_per = - subType_mean[subNPeriod_mean == j] - Code_mean_per = - subCode_mean[subNPeriod_mean == j] - DataMean_mean_per = - subDataMean_mean[subNPeriod_mean == j] - BreakMean_mean_per = - subBreakMean_mean[subNPeriod_mean == j] - Fill_mean_per = - subFill_mean[subNPeriod_mean == j] - Color_mean_per = - subColor_mean[subNPeriod_mean == j] - - # Converts the variable list into levels for factor - levels = unlist(Var_mean_per) - # Converts the vector of hydrological variable to - # a vector of integer associated to those variable - Xtmp_mean = as.integer(factor(as.character(Var_mean_per), - levels=levels)) - # 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)) - # Reverses vertical order of stations - Y_mean = rev(Y_mean) - - # Position of a line to delimite periods - x = Xc_mean - 0.4 - xend = Xm_mean[length(Xm_mean)] + 0.25 - y = height + 1.1 - yend = height + 1.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 + ### Mean ### + # For all the trend period + for (j in 1:nPeriod_mean) { + # Extracts the info to plot associated to the + # right period + Periods_mean_per = + subPeriods_mean[subNPeriod_mean == j] + NPeriods_mean_per = + subNPeriod_mean[subNPeriod_mean == j] + Var_mean_per = + subVar_mean[subNPeriod_mean == j] + Type_mean_per = + subType_mean[subNPeriod_mean == j] + Code_mean_per = + subCode_mean[subNPeriod_mean == j] + DataMean_mean_per = + subDataMean_mean[subNPeriod_mean == j] + BreakMean_mean_per = + subBreakMean_mean[subNPeriod_mean == j] + Fill_mean_per = + subFill_mean[subNPeriod_mean == j] + Color_mean_per = + subColor_mean[subNPeriod_mean == j] + + # Converts the variable list into levels for factor + levels = unlist(Var_mean_per) + # Converts the vector of hydrological variable to + # a vector of integer associated to those variable + Xtmp_mean = + as.integer(factor(as.character(Var_mean_per), + levels=levels)) + # Computes X position of the column for + # the period dates + Xc_mean = j + (j - 1)*nbpMod + X[length(X)] + # Computes X positions of columns for + # the mean of variables + Xm_mean = + Xtmp_mean + (j - 1)*nbpMod + j + X[length(X)] + # Computes X positions of columns for + # the difference of mean between periods (break) + Xr_mean = + Xtmp_mean + (j - 1)*nbpMod*2 + j + X[length(X)] + + # Computes Y positions of each line for each station + Y_mean = as.integer(factor(Code_mean_per)) + # Reverses vertical order of stations + Y_mean = rev(Y_mean) + + # Position of a line to delimite periods + x = Xc_mean - 0.4 + xend = Xm_mean[length(Xm_mean)] + 0.25 + y = height + 1.1 + yend = height + 1.1 # 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 + + # 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=breakName, + label=periodName, 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) { + # 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 + - # 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]) + 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 averaged variables on this period - for (i in 1:length(DataMean_mean_per)) { - # 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=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] + # 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)) { + # Extracts values of averaged variables + dataMean = DataMean_mean_per[i] # Converts it to the right format with two # significant figures - BreakMeanC = signif(BreakMean*100, 2) - # Writes breaking values + dataMeanC = signif(dataMean, 2) + # Writes averaged variables values mat = mat + - annotate('text', x=Xr_mean[i], y=Y[i], - label=BreakMeanC, + annotate('text', x=Xm_mean[i], y=Y[i], + label=dataMeanC, hjust=0.5, vjust=0.5, - size=3, color='white') + 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] + # 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=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.9, - label=bquote(bold('Début')), - hjust=0.5, vjust=0.5, - size=3, color='grey20') + - annotate('text', x=Xc_mean, y=max(Y) + 0.63, - label=bquote(bold('Fin')), - hjust=0.5, vjust=0.5, - size=3, color='grey20') - - # For all variables - for (i in 1:nbp) { - # Extract the variable of the plot - var = list_df2plot[[i]]$var + # Writes a name for the period dates column mat = mat + - # Writes the unit of the averaged variable - annotate('text', x=Xm_mean[i], y=max(Y) + 0.63, - label=bquote('['*m^3*'.'*s^{-1}*']'), + annotate('text', x=Xc_mean, y=max(Y) + 0.9, + label=bquote(bold('Début')), hjust=0.5, vjust=0.5, - size=2, color='grey40') + - # Writes the type of the averaged variable - annotate('text', x=Xm_mean[i], y=max(Y) + 0.9, - label=expr(bar(!!var)), + size=3, color='grey20') + + annotate('text', x=Xc_mean, y=max(Y) + 0.63, + label=bquote(bold('Fin')), hjust=0.5, vjust=0.5, - size=3.25, color='grey20') - - # If this is not the first period - if (j > 1) { + size=3, color='grey20') + + # For all variables + for (i in 1:nbpMod) { + # Extract the variable of the plot + var = subVar_mean[i] mat = mat + - # Writes the unit of the breaking variable - annotate('text', x=Xr_mean[i], - y=max(Y) + 0.63, - label=bquote('[%]'), + # Writes the unit of the averaged variable + annotate('text', + x=Xm_mean[i], y=max(Y) + 0.63, + label=bquote('['*m^3*'.'*s^{-1}*']'), hjust=0.5, vjust=0.5, size=2, color='grey40') + - # Writes the type of the breaking variable - annotate('text', x=Xr_mean[i], - y=max(Y) + 0.9, - label=paste("d", var, sep=''), - hjust=0.5, vjust=0.5, + # Writes the type of the averaged variable + annotate('text', + x=Xm_mean[i], y=max(Y) + 0.9, + label=expr(bar(!!var)), + hjust=0.5, vjust=0.5, size=3.25, color='grey20') + + # If this is not the first period + if (j > 1) { + mat = mat + + # Writes the unit of the breaking variable + annotate('text', x=Xr_mean[i], + y=max(Y) + 0.63, + label=bquote('[%]'), + hjust=0.5, vjust=0.5, + size=2, color='grey40') + + # Writes the type of the breaking variable + annotate('text', x=Xr_mean[i], + y=max(Y) + 0.9, + label=paste("d", var, sep=''), + hjust=0.5, vjust=0.5, + size=3.25, color='grey20') + } } - } - # For all the station on the page + # 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_per[Code_mean_per == 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, + size=3, color='grey40') + } + } + + ### Code ### + # For all the station for (k in 1:nsubCode) { # Gets the code code = subCode[k] - # Extracts label for the period dates - label = Periods_mean_per[Code_mean_per == code][1] - # Gets the start and end of the period - # for the station - periodStart = substr(label, 1, 4) - periodEnd = substr(label, 14, 17) + # 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) { + # Cuts the name and add '...' + name = paste(substr(name, 1, ncharMax), + '...', sep='') + } 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, - size=3, color='grey40') - } - } - - ### 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) { - # Cuts the name and add '...' - name = paste(substr(name, 1, ncharMax), - '...', sep='') + # 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, + size=3.5, color="#00A3A8") } + ### Environment ### 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, - size=3.5, color="#00A3A8") - } + # 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)) + + # 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 + } - ### Environment ### - 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)) - - # 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, + '_', type, + '_', fL, + iMat, sep=''), + device='pdf', + width=width, height=height, + units='cm', dpi=dpi) } - - # Saving - ggsave(plot=mat, - path=outdirTmp, - filename=paste(outnameTmp, '_', fL, - imat, sep=''), - device='pdf', - width=width, height=height, - units='cm', dpi=dpi) - } - } + } + } } diff --git a/processing/format.R b/processing/format.R index 97cba2bf3edd5ab279218e342638109860d202b3..d6398ece9eb8c83e86e5be12f696fac96b493e46 100644 --- a/processing/format.R +++ b/processing/format.R @@ -180,11 +180,15 @@ prepare_date = function(df_XEx, df_Xlist) { # dExMean = apply(rbind(dXExUp, dXExDown), 2, mean, na.rm=TRUE) meanXEx_code = mean(XEx_code, na.rm=TRUE) - dXEx_code = abs(XEx_code - meanXEx_code) + dXEx_code = meanXEx_code - XEx_code stdXEx_code = sd(XEx_code, na.rm=TRUE) XEx_code[dXEx_code >= stdXEx_code*3] = XEx_code[dXEx_code >= stdXEx_code*3] + 365 + + # print(group) + # print(df_XEx$datetime[df_XEx$group1 == group][dXEx_code >= stdXEx_code*3]) + df_XEx$values[OkXEx_code] = XEx_code } diff --git a/script.R b/script.R index f67bfa4115d6c91fc498a9b19bd898d5453a9b88..218cf88210017ce31c647f6b25ff69649e580f4e 100644 --- a/script.R +++ b/script.R @@ -66,7 +66,7 @@ filename = # ) c( - # "S4214010_HYDRO_QJM.txt" + # "S4214010_HYDRO_QJM.txt", # "O0384010_HYDRO_QJM.txt", "Q7002910_HYDRO_QJM.txt" ) @@ -291,8 +291,8 @@ df_shapefile = ini_shapefile(computer_data_path, ### 4.2. Analysis layout datasheet_layout(toplot=c( - # 'datasheet' - 'matrix' + 'datasheet' + # 'matrix', # 'map' ), df_meta=df_meta,