diff --git a/plotting/datasheet.R b/plotting/datasheet.R index b781b3ee68d04dff09888802ba7f012d27181098..37c776404c4364a1d31cc328cb0336e3d5aee6a9 100644 --- a/plotting/datasheet.R +++ b/plotting/datasheet.R @@ -35,7 +35,7 @@ source('plotting/shortcut.R', encoding='UTF-8') # Manages datasheets creations for all stations. Makes the call to # the different headers, trend analysis graphs and realises arranging # every plots. -datasheet_panel = function (list_df2plot, df_meta, trend_period, info_header, time_header, foot_note, layout_matrix, info_ratio, time_ratio, var_ratio, foot_height, resources_path, logo_dir, AEAGlogo_file, INRAElogo_file, FRlogo_file, outdirTmp, df_page=NULL) { +datasheet_panel = function (list_df2plot, df_meta, trend_period, mean_period, info_header, time_header, foot_note, layout_matrix, info_height, time_ratio, var_ratio, foot_height, resources_path, logo_dir, AEAGlogo_file, INRAElogo_file, FRlogo_file, outdirTmp, df_page=NULL) { # The percentage of augmentation and diminution of the min # and max limits for y axis @@ -48,15 +48,17 @@ datasheet_panel = function (list_df2plot, df_meta, trend_period, info_header, ti Code = levels(factor(df_meta$code)) nCode = length(Code) - # Convert 'trend_period' to list - trend_period = as.list(trend_period) - # Number of trend period - nPeriod_trend = length(trend_period) + if (!is.null(trend_period)) { + # Convert 'trend_period' to list + trend_period = as.list(trend_period) + # Number of trend period + nPeriod_trend = length(trend_period) - # Extracts the min and the max of the mean trend for all the station - res = short_trendExtremes(list_df2plot, Code, nPeriod_trend, nbp, nCode) - minTrendValue = res$min - maxTrendValue = res$max + # Extracts the min and the max of the mean trend for all the station + res = short_trendExtremes(list_df2plot, Code, nPeriod_trend, nbp, nCode) + minTrendValue = res$min + maxTrendValue = res$max + } # Blank vector to store the max number of digit of label for # each station @@ -67,10 +69,8 @@ datasheet_panel = function (list_df2plot, df_meta, trend_period, info_header, ti # Default max digit NspaceMax_code = 0 - # If the time header is given - if (!is.null(time_header)) { - nbpMod = nbp + 1 - } + # If the time header is given it adds one to the number of plot + nbpMod = nbp + as.numeric(!is.null(time_header)) # For all type of graph for (i in 1:nbpMod) { @@ -94,7 +94,7 @@ datasheet_panel = function (list_df2plot, df_meta, trend_period, info_header, ti # with 3 characters Nspace = 6 # If it is a flow variable - } else if (type == 'sévérité') { + } else if (type == 'sévérité' | type == 'data') { # Gets the max number of digit on the label maxtmp = max(df_data_code$Value, na.rm=TRUE) # Taking into account of the augmentation of @@ -134,7 +134,7 @@ datasheet_panel = function (list_df2plot, df_meta, trend_period, info_header, ti # Stores the max digit number for labels of a station NspaceMax = c(NspaceMax, NspaceMax_code) } - + # For all the station for (k in 1:nCode) { # Gets the code @@ -145,16 +145,18 @@ datasheet_panel = function (list_df2plot, df_meta, trend_period, info_header, ti sep='')) # Number of header (is info and time serie are needed) - nbh = as.numeric(info_header) + as.numeric(!is.null(time_header)) + nbh = as.numeric(!is.null(info_header)) + as.numeric(!is.null(time_header)) # Actualises the number of plot nbg = nbp + nbh + as.numeric(foot_note) # Opens a blank list to store plot P = vector(mode='list', length=nbg) # If the info header is needed - if (info_header) { + if (!is.null(info_header)) { + # Extracts the data serie corresponding to the code - time_header_code = time_header[time_header$code == code,] + info_header_code = info_header[info_header$code == code,] + # Gets the info plot Hinfo = info_panel(list_df2plot, df_meta, @@ -163,11 +165,11 @@ datasheet_panel = function (list_df2plot, df_meta, trend_period, info_header, ti periodHyd=mean_period[[1]], df_shapefile=df_shapefile, codeLight=code, - df_data_code=time_header_code) + df_data_code=info_header_code) # Stores it P[[1]] = Hinfo } - + # If the time header is given if (!is.null(time_header)) { # Extracts the data serie corresponding to the code @@ -208,76 +210,94 @@ datasheet_panel = function (list_df2plot, df_meta, trend_period, info_header, ti # Blank vector to store color color = c() - - # # Default grey color for not significant trend - # grey = 85 - - # For all the period - for (j in 1:nPeriod_trend) { - - # If the trend is significant - if (df_trend_code$p[j] <= alpha){ - # Extract start and end of trend periods - Start = df_trend_code$period_start[j] - End = df_trend_code$period_end[j] - - # Extracts the corresponding data for the period - df_data_code_per = - df_data_code[df_data_code$Date >= Start - & df_data_code$Date <= End,] - # Same for trend - df_trend_code_per = - df_trend_code[df_trend_code$period_start == Start - & df_trend_code$period_end == End,] - - # Computes the number of trend analysis selected - Ntrend = nrow(df_trend_code_per) - # If there is more than one trend on the same period - if (Ntrend > 1) { - # Takes only the first because they are similar - df_trend_code_per = df_trend_code_per[1,] - } - # If it is a flow variable - if (type == 'sévérité') { - # Computes the mean of the data on the period - dataMean = mean(df_data_code_per$Value, - na.rm=TRUE) - # Normalises the trend value by the mean - # of the data - trendValue = df_trend_code_per$trend / dataMean - # If it is a date variable - } else if (type == 'saisonnalité') { - trendValue = df_trend_code_per$trend - } - - # Gets the color corresponding to the mean trend - color_res = get_color(trendValue, - minTrendValue[j, i], - maxTrendValue[j, i], - palette_name='perso', - reverse=TRUE) - # Stores it temporarily - colortmp = color_res - # Otherwise - } else { - # Stores the default grey color - colortmp = paste('grey85', sep='') + if (!is.null(trend_period)) { + # For all the period + for (j in 1:nPeriod_trend) { + # If the trend is significant + if (df_trend_code$p[j] <= alpha){ + # Extract start and end of trend periods + Start = df_trend_code$period_start[j] + End = df_trend_code$period_end[j] + + # Extracts the corresponding data for the period + df_data_code_per = + df_data_code[df_data_code$Date >= Start + & df_data_code$Date <= End,] + # Same for trend + df_trend_code_per = + df_trend_code[df_trend_code$period_start == Start + & df_trend_code$period_end == End,] + + # Computes the number of trend analysis selected + Ntrend = nrow(df_trend_code_per) + # If there is more than one trend on the same period + if (Ntrend > 1) { + # Takes only the first because they are similar + df_trend_code_per = df_trend_code_per[1,] + } + + # If it is a flow variable + if (type == 'sévérité') { + # Computes the mean of the data on the period + dataMean = mean(df_data_code_per$Value, + na.rm=TRUE) + # Normalises the trend value by the mean + # of the data + trendValue = df_trend_code_per$trend / dataMean + # If it is a date variable + } else if (type == 'saisonnalité') { + trendValue = df_trend_code_per$trend + } + + # Gets the color corresponding to the mean trend + color_res = get_color(trendValue, + minTrendValue[j, i], + maxTrendValue[j, i], + palette_name='perso', + reverse=TRUE) + # Stores it temporarily + colortmp = color_res + # Otherwise + } else { + # Stores the default grey color + colortmp = paste('grey85', sep='') + + } + # Stores the color + color = append(color, colortmp) + grid = FALSE } - # Stores the color - color = append(color, colortmp) + } else { + axis_xlim = NULL + } + + if (var != 'sqrt(Q)' & var != 'Q') { + grid = FALSE + ymin_lim = NULL + } else { + grid = TRUE + ymin_lim = 0 } + if (is.null(time_header) & i == 1) { + first = TRUE + } else { + first = FALSE + } + # Computes the time panel associated to the current variable p = time_panel(df_data_code, df_trend_code, var=var, type=type, alpha=alpha, missRect=missRect, trend_period=trend_period, mean_period=mean_period, axis_xlim=axis_xlim, - unit2day=unit2day, grid=FALSE, color=color, - NspaceMax=NspaceMax[k], last=(i == nbp), + unit2day=unit2day, grid=grid, + ymin_lim=ymin_lim, color=color, + NspaceMax=NspaceMax[k], first=first, + last=(i == nbp), lim_pct=lim_pct) - + # Stores the plot P[[i+nbh]] = p } @@ -321,15 +341,19 @@ datasheet_panel = function (list_df2plot, df_meta, trend_period, info_header, ti nel) # Shifts all plots to be coherent with the adding of header LM = LM + nbh - + if (!is.null(time_header)) { - LM = rbind(2, LM) + id_time = nbh + LM = rbind(nbh, LM) } else { + id_time = NA time_ratio = 0 } - if (info_header) { - LM = rbind(1, LM) + if (!is.null(info_header)) { + id_info = nbh - 1 + LM = rbind(nbh - 1, LM) } else { + id_info = NA info_ratio = 0 } @@ -352,18 +376,17 @@ datasheet_panel = function (list_df2plot, df_meta, trend_period, info_header, ti height = 29.7 width = 21 - Norm_ratio = height * (info_ratio + time_ratio + var_ratio*nbp) / (height - 2*margin_size - foot_height) + Norm_ratio = height * (time_ratio + var_ratio*nbp) / (height - 2*margin_size - foot_height - info_height) - info_height = height * info_ratio / Norm_ratio time_height = height * time_ratio / Norm_ratio var_height = height * var_ratio / Norm_ratio Hcut = LM[, 2] - heightLM = rep(0, times=LMrow) + heightLM = rep(0, times=LMrow) - heightLM[Hcut == 1] = info_height - heightLM[Hcut == 2] = time_height - heightLM[Hcut > 2 & Hcut < id_foot] = var_height + heightLM[Hcut == id_info] = info_height + heightLM[Hcut == id_time] = time_height + heightLM[Hcut > nbh & Hcut < id_foot] = var_height heightLM[Hcut == id_foot] = foot_height heightLM[Hcut == 99] = margin_size @@ -391,11 +414,6 @@ datasheet_panel = function (list_df2plot, df_meta, trend_period, info_header, ti ## 2. OTHER PANEL FOR THE DATASHEET __________________________________ ### 2.1. Time panel __________________________________________________ time_panel = function (df_data_code, df_trend_code, var, type, alpha=0.1, missRect=FALSE, unit2day=365.25, trend_period=NULL, mean_period=NULL, axis_xlim=NULL, grid=TRUE, ymin_lim=NULL, color=NULL, NspaceMax=NULL, first=FALSE, last=FALSE, lim_pct=10) { - - # If 'type' is square root apply it to data - if (var == 'sqrt(Q)') { - df_data_code$Value = sqrt(df_data_code$Value) - } # Compute max and min of flow maxQ = max(df_data_code$Value, na.rm=TRUE) @@ -1115,9 +1133,12 @@ time_panel = function (df_data_code, df_trend_code, var, type, alpha=0.1, missRe # Y axis title # If it is a flow variable - if (type == 'sévérité') { + if (type == 'sévérité' | var == 'Q') { p = p + ylab(bquote(bold(.(var))~~'['*m^{3}*'.'*s^{-1}*']')) + } else if (var == 'sqrt(Q)') { + p = p + + ylab(bquote(bold(.(var))~~'['*m^{3/2}*'.'*s^{-1/2}*']')) # If it is a date variable } else if (type == 'saisonnalité') { p = p + @@ -1167,7 +1188,7 @@ time_panel = function (df_data_code, df_trend_code, var, type, alpha=0.1, missRe accuracy = NULL # If it is a flow variable - } else if (type == 'sévérité') { + } else if (type == 'sévérité' | type == 'data') { # Gets the max number of digit on the label maxtmp = max(df_data_code$Value, na.rm=TRUE) # Taking into account of the augmentation of @@ -1214,7 +1235,7 @@ time_panel = function (df_data_code, df_trend_code, var, type, alpha=0.1, missRe # Parameters of the y axis # If it is a flow variable - if (type == 'sévérité') { + if (type == 'sévérité' | type == 'data') { p = p + scale_y_continuous(breaks=seq(minQ_lim, maxQ_lim, breakQ), limits=c(minQ_win, maxQ_win), @@ -1262,7 +1283,7 @@ time_panel = function (df_data_code, df_trend_code, var, type, alpha=0.1, missRe ### 2.2. Info panel __________________________________________________ # Plots the header that regroups all the info on the station info_panel = function(list_df2plot, df_meta, trend_period, mean_period, periodHyd, df_shapefile, codeLight, df_data_code=NULL) { - + # If there is a data serie for the given code if (!is.null(df_data_code)) { # Computes the hydrograph diff --git a/plotting/layout.R b/plotting/layout.R index 6211d69af631889c72bb17287b274ae5aa337831..0bbd98c0441fe5b516c3ba94ae8962b45357d7fa 100644 --- a/plotting/layout.R +++ b/plotting/layout.R @@ -130,12 +130,12 @@ datasheet_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_opt='', filename_opt='', variable='', df_trend=NULL, alpha=0.1, unit2day=365.25, var='', - type='', glose='', trend_period=NULL, + type='', glose=NULL, trend_period=NULL, mean_period=NULL, axis_xlim=NULL, - missRect=FALSE, time_header=NULL, - info_header=TRUE, foot_note=FALSE, - info_ratio=1, time_ratio=2, - var_ratio=3, foot_height=1, + missRect=TRUE, time_header=NULL, + info_header=NULL, foot_note=TRUE, + info_height=2.8, time_ratio=2, + var_ratio=3, foot_height=1.25, df_shapefile=NULL, resources_path=NULL, logo_dir=NULL, @@ -286,12 +286,13 @@ datasheet_layout = function (df_data, df_meta, layout_matrix, if ('datasheet' %in% toplot) { df_page = datasheet_panel(list_df2plot, df_meta, - trend_period, + trend_period=trend_period, + mean_period=mean_period, info_header=info_header, time_header=time_header, foot_note=foot_note, layout_matrix=layout_matrix, - info_ratio=info_ratio, + info_height=info_height, time_ratio=time_ratio, var_ratio=var_ratio, foot_height=foot_height, diff --git a/plotting/map.R b/plotting/map.R index 8571ce6d659548aca378ab7271559d3d801a2c36..644092b63455548e35db8f3e9557a87c94f55cf2 100644 --- a/plotting/map.R +++ b/plotting/map.R @@ -52,15 +52,17 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer_trend=1, Code = levels(factor(df_meta$code)) nCode = length(Code) - # Convert 'trend_period' to list - trend_period = as.list(trend_period) - # Number of trend period - nPeriod_trend = length(trend_period) - - # Extracts the min and the max of the mean trend for all the station - res = short_trendExtremes(list_df2plot, Code, nPeriod_trend, nbp, nCode) - minTrendValue = res$min - maxTrendValue = res$max + if (!is.null(trend_period)) { + # Convert 'trend_period' to list + trend_period = as.list(trend_period) + # Number of trend period + nPeriod_trend = length(trend_period) + + # Extracts the min and the max of the mean trend for all the station + res = short_trendExtremes(list_df2plot, Code, nPeriod_trend, nbp, nCode) + minTrendValue = res$min + maxTrendValue = res$max + } # If there is a 'mean_period' if (!is.null(mean_period)) { @@ -118,7 +120,7 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer_trend=1, round(n_loop/N_loop*100, 0), " %)", sep='')) - } + } # If there is no specified station code to highlight # (mini map) @@ -297,8 +299,15 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer_trend=1, minValue = minBreakValue[j, i] maxValue = maxBreakValue[j, i] pvalue = 0 + + } else if (is.null(trend_period)) { + value = NA + minValue = NULL + maxValue = NULL + pvalue = 0 } else { + # Extracts the data corresponding to the # current variable df_data = list_df2plot[[i]]$data @@ -309,9 +318,10 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer_trend=1, alpha = list_df2plot[[i]]$alpha # Extracts the data corresponding to the code df_data_code = df_data[df_data$code == code,] + # Extracts the trend corresponding to the code df_trend_code = df_trend[df_trend$code == code,] - + # Extract start and end of trend periods Start = df_trend_code$period_start[idPer_trend] End = df_trend_code$period_end[idPer_trend] @@ -367,7 +377,7 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer_trend=1, ncolor=256, nbTick=nbTick) - if (j > 1) { + if (j > 1 | is.null(trend_period)) { # The computed color is stored filltmp = color_res # The marker is a circle @@ -442,7 +452,7 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer_trend=1, shape=shape[OkVal], size=5, stroke=1, color='grey50', fill=fill[OkVal]) } - + # If there is a specified station code } else { # Extract data of all stations not to highlight @@ -463,118 +473,116 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer_trend=1, shape=21, size=2, stroke=0.5, color='grey97', fill='#00A3A8') } + + if (!is.null(trend_period)) { - # Extracts the position of the tick of the colorbar - posTick = palette_res$posTick - # Extracts the label of the tick of the colorbar - labTick = palette_res$labTick - # Extracts the color corresponding to the tick of the colorbar - colTick = palette_res$colTick - - # Spreading of the colorbar - valNorm = nbTick * 10 - # Normalisation of the position of ticks - ytick = posTick / max(posTick) * valNorm - - # If it is a flow variable - if (type == 'sévérité') { - # Formatting of label in pourcent - labTick = as.character(signif(labTick*100, 2)) - # If it is a date variable - } else if (type == 'saisonnalité') { - # Formatting of label - labTick = as.character(signif(labTick, 2)) - } - - # X position of ticks all similar - xtick = rep(0, times=nbTick) + # Extracts the position of the tick of the colorbar + posTick = palette_res$posTick + # Extracts the label of the tick of the colorbar + labTick = palette_res$labTick + # Extracts the color corresponding to the tick of the colorbar + colTick = palette_res$colTick + + # Spreading of the colorbar + valNorm = nbTick * 10 + # Normalisation of the position of ticks + ytick = posTick / max(posTick) * valNorm - # Creates a tibble to store all parameters of colorbar - plot_palette = tibble(xtick=xtick, ytick=ytick, - colTick=colTick, labTick=labTick) + # If it is a flow variable + if (type == 'sévérité') { + # Formatting of label in pourcent + labTick = as.character(signif(labTick*100, 2)) + # If it is a date variable + } else if (type == 'saisonnalité') { + # Formatting of label + labTick = as.character(signif(labTick, 2)) + } + + # X position of ticks all similar + xtick = rep(0, times=nbTick) - - nbLine = as.integer(nchar(glose)/40) + 1 - - - nbNewline = 0 - - nbLim = 43 - gloseName = glose - nbChar = nchar(gloseName) - while (nbChar > nbLim) { - nbNewline = nbNewline + 1 - posSpace = which(strsplit(gloseName, "")[[1]] == " ") - idNewline = which.min(abs(posSpace - nbLim * nbNewline)) - posNewline = posSpace[idNewline] - gloseName = paste(substring(gloseName, - c(1, posNewline + 1), - c(posNewline, - nchar(gloseName))), - collapse="\n") - Newline = substr(gloseName, - posNewline + 2, - nchar(gloseName)) - nbChar = nchar(Newline) - } + # Creates a tibble to store all parameters of colorbar + plot_palette = tibble(xtick=xtick, ytick=ytick, + colTick=colTick, labTick=labTick) - Yline = 0.6 + 0.47*nbNewline - Ytitle = Yline + 0.15 - - # New plot with void theme - title = ggplot() + theme_void() + - # Plots separation lines - geom_line(aes(x=c(-0.3, 3.9), y=c(0.05, 0.05)), - size=0.6, color="#00A3A8") + - geom_line(aes(x=c(-0.3, 3.9), y=c(Yline, Yline)), - size=0.6, color="#00A3A8") + - # Writes title - geom_shadowtext(data=tibble(x=-0.3, y=Ytitle, - label=var), - aes(x=x, y=y, label=label), - fontface="bold", - color="#00A3A8", - bg.colour="white", - hjust=0, vjust=0, size=10) + - # Writes title - geom_shadowtext(data=tibble(x=-0.3, y=0.2, - label=gloseName), - aes(x=x, y=y, label=label), - fontface="bold", - color="#00A3A8", - bg.colour="white", - hjust=0, vjust=0, size=3) + + nbLine = as.integer(nchar(glose)/40) + 1 + + + nbNewline = 0 + + nbLim = 43 + gloseName = glose + nbChar = nchar(gloseName) + while (nbChar > nbLim) { + nbNewline = nbNewline + 1 + posSpace = which(strsplit(gloseName, "")[[1]] == " ") + idNewline = which.min(abs(posSpace - nbLim * nbNewline)) + posNewline = posSpace[idNewline] + gloseName = paste(substring(gloseName, + c(1, posNewline + 1), + c(posNewline, + nchar(gloseName))), + collapse="\n") + Newline = substr(gloseName, + posNewline + 2, + nchar(gloseName)) + nbChar = nchar(Newline) + } + + Yline = 0.6 + 0.47*nbNewline + Ytitle = Yline + 0.15 - # X axis - scale_x_continuous(limits=c(-0.3, 1 + 3), - expand=c(0, 0)) + - # Y axis - scale_y_continuous(limits=c(0, 10), - expand=c(0, 0)) + - # Margin - theme(plot.margin=margin(t=0, r=0, b=0, l=0, unit="mm")) - - # New plot with void theme - pal = ggplot() + theme_void() + - # Plots the point of the colorbar - geom_point(data=plot_palette, - aes(x=xtick, y=ytick), - shape=21, size=5, stroke=1, - color='white', fill=colTick) + # New plot with void theme + title = ggplot() + theme_void() + + # Plots separation lines + geom_line(aes(x=c(-0.3, 3.9), y=c(0.05, 0.05)), + size=0.6, color="#00A3A8") + + geom_line(aes(x=c(-0.3, 3.9), y=c(Yline, Yline)), + size=0.6, color="#00A3A8") + + # Writes title + geom_shadowtext(data=tibble(x=-0.3, y=Ytitle, + label=var), + aes(x=x, y=y, label=label), + fontface="bold", + color="#00A3A8", + bg.colour="white", + hjust=0, vjust=0, size=10) + + + # Writes title + geom_shadowtext(data=tibble(x=-0.3, y=0.2, + label=gloseName), + aes(x=x, y=y, label=label), + fontface="bold", + color="#00A3A8", + bg.colour="white", + hjust=0, vjust=0, size=3) + + + # X axis + scale_x_continuous(limits=c(-0.3, 1 + 3), + expand=c(0, 0)) + + # Y axis + scale_y_continuous(limits=c(0, 10), + expand=c(0, 0)) + + # Margin + theme(plot.margin=margin(t=0, r=0, b=0, l=0, unit="mm")) + + # New plot with void theme + pal = ggplot() + theme_void() + + # Plots the point of the colorbar + geom_point(data=plot_palette, + aes(x=xtick, y=ytick), + shape=21, size=5, stroke=1, + color='white', fill=colTick) + - if (!is.null(trend_period)) { periodName_trend = paste( format(as.Date(trend_period[[idPer_trend]][1]), '%Y'), format(as.Date(trend_period[[idPer_trend]][2]), '%Y'), sep='-') - } else { - periodName_trend = NA - } - - if (!is.null(mean_period)) { + periodName1_mean = paste( format(as.Date(mean_period[[1]][1]), '%Y'), @@ -587,215 +595,216 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer_trend=1, format(as.Date(mean_period[[2]][2]), '%Y'), sep='-') - } else { - periodName1_mean = NA - periodName2_mean = NA - } - - if (j > 1) { - ValueName1 = "Écarts observés entre" - ValueName2 = paste(periodName1_mean, - " et ", - periodName2_mean, - sep='') - # If it is a flow variable - if (type == 'sévérité') { - unit = bquote(bold("(%)")) - # If it is a date variable - } else if (type == 'saisonnalité') { - unit = bquote(bold("(jour)")) - } - } else { - ValueName1 = "Tendances observées" - ValueName2 = paste("sur la période ", - periodName_trend, sep='') - # If it is a flow variable - if (type == 'sévérité') { - unit = bquote(bold("(% par an)")) - # If it is a date variable - } else if (type == 'saisonnalité') { - unit = bquote(bold("(jour par an)")) + + if (j > 1) { + ValueName1 = "Écarts observés entre" + ValueName2 = paste(periodName1_mean, + " et ", + periodName2_mean, + sep='') + # If it is a flow variable + if (type == 'sévérité') { + unit = bquote(bold("(%)")) + # If it is a date variable + } else if (type == 'saisonnalité') { + unit = bquote(bold("(jour)")) + } + } else { + ValueName1 = "Tendances observées" + ValueName2 = paste("sur la période ", + periodName_trend, sep='') + # If it is a flow variable + if (type == 'sévérité') { + unit = bquote(bold("(% par an)")) + # If it is a date variable + } else if (type == 'saisonnalité') { + unit = bquote(bold("(jour par an)")) + } } - } - - pal = pal + - # Name of the colorbar - annotate('text', - x=-0.3, y= valNorm + 37, - label=ValueName1, - hjust=0, vjust=0.5, - size=6, color='grey40') + - # Second line - annotate('text', - x=-0.3, y= valNorm + 26, - label=ValueName2, - hjust=0, vjust=0.5, - size=6, color='grey40') + - # Unit legend of the colorbar - annotate('text', - x=-0.3, y= valNorm + 14, - label=unit, - hjust=0, vjust=0.5, - size=4, color='grey40') - - # For all the ticks - for (id in 1:nbTick) { - pal = pal + - # Adds the value - annotate('text', x=xtick[id]+0.3, - y=ytick[id], - label=bquote(bold(.(labTick[id]))), - hjust=0, vjust=0.7, - size=3, color='grey40') - } - - if (j == 1) { - upLabel = bquote(bold("Hausse significative à 10%")) - noneLabel = bquote(bold("Non significatif à 10%")) - downLabel = bquote(bold("Baisse significative à 10%")) - yUp = -20 - yNone = -29 - yDown = -40 - pal = pal + - # Up triangle in the marker legend - geom_point(aes(x=0, y=yUp), - shape=24, size=4, stroke=1, - color='grey50', fill='grey97') + - # Up triangle text legend + # Name of the colorbar annotate('text', - x=0.3, y=yUp, - label=upLabel, + x=-0.3, y= valNorm + 37, + label=ValueName1, hjust=0, vjust=0.5, - size=3, color='grey40') + size=6, color='grey40') + + # Second line + annotate('text', + x=-0.3, y= valNorm + 26, + label=ValueName2, + hjust=0, vjust=0.5, + size=6, color='grey40') + + # Unit legend of the colorbar + annotate('text', + x=-0.3, y= valNorm + 14, + label=unit, + hjust=0, vjust=0.5, + size=4, color='grey40') + + # For all the ticks + for (id in 1:nbTick) { + pal = pal + + # Adds the value + annotate('text', x=xtick[id]+0.3, + y=ytick[id], + label=bquote(bold(.(labTick[id]))), + hjust=0, vjust=0.7, + size=3, color='grey40') + } + + if (j == 1) { + upLabel = bquote(bold("Hausse significative à 10%")) + noneLabel = bquote(bold("Non significatif à 10%")) + downLabel = bquote(bold("Baisse significative à 10%")) + + yUp = -20 + yNone = -29 + yDown = -40 + + pal = pal + + # Up triangle in the marker legend + geom_point(aes(x=0, y=yUp), + shape=24, size=4, stroke=1, + color='grey50', fill='grey97') + + # Up triangle text legend + annotate('text', + x=0.3, y=yUp, + label=upLabel, + hjust=0, vjust=0.5, + size=3, color='grey40') + + pal = pal + + # Circle in the marker legend + geom_point(aes(x=0, y=yNone), + shape=21, size=4, stroke=1, + color='grey50', fill='grey97') + + # Circle text legend + annotate('text', + x=0.3, y=yNone, + label=noneLabel, + hjust=0, vjust=0.7, + size=3, color='grey40') + + pal = pal + + # Down triangle in the marker legend + geom_point(aes(x=0, y=yDown), + shape=25, size=4, stroke=1, + color='grey50', fill='grey97') + + # Down triangle text legend + annotate('text', + x=0.3, y=yDown, + label=downLabel, + hjust=0, vjust=0.5, + size=3, color='grey40') + + } + + # Normalises all the trend values for each station + # according to the colorbar + if (j > 1) { + yValue = (Value - minBreakValue[j, i]) / (maxBreakValue[j, i] - minBreakValue[j, i]) * valNorm + } else { + yValue = (Value - minTrendValue[idPer_trend, i]) / (maxTrendValue[idPer_trend, i] - minTrendValue[idPer_trend, i]) * valNorm + } + # Takes only the significative ones + yValue = yValue[OkVal] + + # Histogram distribution + # Computes the histogram of values + res_hist = hist(yValue, breaks=ytick, plot=FALSE) + # Extracts the number of counts per cells + counts = res_hist$counts + # Extracts limits of cells + breaks = res_hist$breaks + # Extracts middle of cells + mids = res_hist$mids + + # Blank vectors to store position of points of + # the distribution to plot + xValue = c() + yValue = c() + # Start X position of the distribution + start_hist = 1 + + # X separation bewteen point + hist_sep = 0.15 + + # Gets the maximun number of point of the distribution + maxCount = max(counts, na.rm=TRUE) + # Limit of the histogram + lim_hist = 2 + # If the number of point will exceed the limit + if (maxCount * hist_sep > lim_hist) { + # Computes the right amount of space between points + hist_sep = lim_hist / maxCount + } + + # For all cells of the histogram + for (ii in 1:length(mids)) { + # If the count in the current cell is not zero + if (counts[ii] != 0) { + # Stores the X positions of points of the + # distribution for the current cell + xValue = c(xValue, + seq(start_hist, + start_hist+(counts[ii]-1)*hist_sep, + by=hist_sep)) + } + # Stores the Y position which is the middle of the + # current cell the number of times it has been counted + yValue = c(yValue, rep(mids[ii], times=counts[ii])) + } + + # Makes a tibble to plot the distribution + plot_value = tibble(xValue=xValue, yValue=yValue) + pal = pal + - # Circle in the marker legend - geom_point(aes(x=0, y=yNone), - shape=21, size=4, stroke=1, - color='grey50', fill='grey97') + - # Circle text legend - annotate('text', - x=0.3, y=yNone, - label=noneLabel, - hjust=0, vjust=0.7, - size=3, color='grey40') + # Plots the point of the distribution + geom_point(data=plot_value, + aes(x=xValue, y=yValue), + shape=21, color='white', + fill='grey50', stroke=0.4, + alpha=1) + + if (type == 'sévérité') { + labelArrow = 'Plus sévère' + } else if (type == 'saisonnalité') { + labelArrow = 'Plus tôt' + } + + # Position of the arrow + xArrow = 3.3 pal = pal + - # Down triangle in the marker legend - geom_point(aes(x=0, y=yDown), - shape=25, size=4, stroke=1, - color='grey50', fill='grey97') + - # Down triangle text legend + # Arrow to show a worsening of the situation + geom_segment(aes(x=xArrow, y=valNorm*0.75, + xend=xArrow, yend=valNorm*0.25), + color='grey50', size=0.3, + arrow=arrow(length=unit(2, "mm"))) + + # Text associated to the arrow annotate('text', - x=0.3, y=yDown, - label=downLabel, - hjust=0, vjust=0.5, - size=3, color='grey40') + x=xArrow+0.1, y=valNorm*0.5, + label=labelArrow, + angle=90, + hjust=0.5, vjust=1, + size=3, color='grey50') + + pal = pal + + # X axis of the colorbar + scale_x_continuous(limits=c(-0.3, 4), + expand=c(0, 0)) + + # Y axis of the colorbar + scale_y_continuous(limits=c(-47, valNorm + 48), + expand=c(0, 0)) + + # Margin of the colorbar + theme(plot.margin=margin(t=0, r=0, b=0, l=0, unit="mm")) - } - - # Normalises all the trend values for each station - # according to the colorbar - if (j > 1) { - yValue = (Value - minBreakValue[j, i]) / (maxBreakValue[j, i] - minBreakValue[j, i]) * valNorm } else { - yValue = (Value - minTrendValue[idPer_trend, i]) / (maxTrendValue[idPer_trend, i] - minTrendValue[idPer_trend, i]) * valNorm + pal = void + title = void } - - # Takes only the significative ones - yValue = yValue[OkVal] - - # Histogram distribution - # Computes the histogram of values - res_hist = hist(yValue, breaks=ytick, plot=FALSE) - # Extracts the number of counts per cells - counts = res_hist$counts - # Extracts limits of cells - breaks = res_hist$breaks - # Extracts middle of cells - mids = res_hist$mids - - # Blank vectors to store position of points of - # the distribution to plot - xValue = c() - yValue = c() - # Start X position of the distribution - start_hist = 1 - - # X separation bewteen point - hist_sep = 0.15 - - # Gets the maximun number of point of the distribution - maxCount = max(counts, na.rm=TRUE) - # Limit of the histogram - lim_hist = 2 - # If the number of point will exceed the limit - if (maxCount * hist_sep > lim_hist) { - # Computes the right amount of space between points - hist_sep = lim_hist / maxCount - } - - # For all cells of the histogram - for (ii in 1:length(mids)) { - # If the count in the current cell is not zero - if (counts[ii] != 0) { - # Stores the X positions of points of the - # distribution for the current cell - xValue = c(xValue, - seq(start_hist, - start_hist+(counts[ii]-1)*hist_sep, - by=hist_sep)) - } - # Stores the Y position which is the middle of the - # current cell the number of times it has been counted - yValue = c(yValue, rep(mids[ii], times=counts[ii])) - } - - # Makes a tibble to plot the distribution - plot_value = tibble(xValue=xValue, yValue=yValue) - - pal = pal + - # Plots the point of the distribution - geom_point(data=plot_value, - aes(x=xValue, y=yValue), - shape=21, color='white', - fill='grey50', stroke=0.4, - alpha=1) - - if (type == 'sévérité') { - labelArrow = 'Plus sévère' - } else if (type == 'saisonnalité') { - labelArrow = 'Plus tôt' - } - - # Position of the arrow - xArrow = 3.3 - - pal = pal + - # Arrow to show a worsening of the situation - geom_segment(aes(x=xArrow, y=valNorm*0.75, - xend=xArrow, yend=valNorm*0.25), - color='grey50', size=0.3, - arrow=arrow(length=unit(2, "mm"))) + - # Text associated to the arrow - annotate('text', - x=xArrow+0.1, y=valNorm*0.5, - label=labelArrow, - angle=90, - hjust=0.5, vjust=1, - size=3, color='grey50') - - pal = pal + - # X axis of the colorbar - scale_x_continuous(limits=c(-0.3, 4), - expand=c(0, 0)) + - # Y axis of the colorbar - scale_y_continuous(limits=c(-47, valNorm + 48), - expand=c(0, 0)) + - # Margin of the colorbar - theme(plot.margin=margin(t=0, r=0, b=0, l=0, unit="mm")) if (!is.null(df_page)) { if (j > 1) { diff --git a/plotting/tools.R b/plotting/tools.R index c297b27f8495708941d08a8152b71652b5666115..94624efcbeada58bde26fed61fa7aab3361603e6 100644 --- a/plotting/tools.R +++ b/plotting/tools.R @@ -85,6 +85,11 @@ get_color = function (value, min, max, ncolor=256, palette_name='perso', reverse # Returns the colorbar but also positions, labels and colors of some # ticks along it get_palette = function (min, max, ncolor=256, palette_name='perso', reverse=FALSE, nbTick=10) { + + # If the value is a NA return NA color + if (is.null(min) | is.null(max)) { + return (NA) + } # If the palette chosen is the personal ones if (palette_name == 'perso') { diff --git a/processing/analyse.R b/processing/analyse.R index b39e8b5e7eb8a8852897eca41f68aa76a87ed0ab..8bcffaf3d112cb932334a09c798aa040492b8289 100644 --- a/processing/analyse.R +++ b/processing/analyse.R @@ -830,3 +830,45 @@ get_lacune = function (df_data, df_meta) { df_meta = full_join(df_meta, df_lac) return (df_meta) } + +### 2.4. Compute square root of data _________________________________ +compute_sqrt = function (df_data) { + + df_sqrt = tibble(Date=df_data$Date, + Value=sqrt(df_data$Value), + code=df_data$code) + + return (df_sqrt) +} + +### 2.5. Criticism of data ___________________________________________ +add_critique = function (df_critique, Code, author, level, start_date, variable, type, comment='', end_date=NULL, df_meta=NULL, resdir=NULL) { + if (Code == 'all' & is.null(df_meta)) { + Code = NA # erreur + } else if (Code == 'all' & !is.null(df_meta)) { + # Get all different stations code + Code = levels(factor(df_meta$code)) + } + + if (is.null(end_date)) { + end_date = start_date + } + + df_tmp = tibble(code=Code, author=author, level=level, + start_date=start_date, end_date=end_date, + variable=variable, type=type, + comment=comment) + df_critique = bind_rows(df_critique, df_tmp) + + nc = nrow(df_critique) + print('Criticism registered') + print(df_critique[(nc-2):nc,]) + + if (!is.null(resdir)) { + write_critique(df_critique, resdir) + } + + return (df_critique) +} + +# df_critique = add_critique(df_critique, Code='', author='', level=, start_date='', end_date='', variable='', type='', comment='') diff --git a/processing/read_write.R b/processing/read_write.R index d564cd58e4c5cfd28f74a95fd44e2efbe16e849f..ac975438c5c5692695c23b2974975d23521c42b1 100644 --- a/processing/read_write.R +++ b/processing/read_write.R @@ -27,7 +27,7 @@ ## 1. WRITING ________________________________________________________ ### 1.1. List of dataframe ___________________________________________ -write_listofdf = function (Ldf, resdir, filedir, optdir='') { +write_analyse = function (Ldf, resdir, filedir, optdir='') { outdir = file.path(resdir, optdir, filedir) if (!(file.exists(outdir))) { @@ -50,7 +50,7 @@ write_listofdf = function (Ldf, resdir, filedir, optdir='') { } ### 2.2. Dataframe of modified data __________________________________ -write_dfdata = function (df_data, df_mod, resdir, filedir, optdir='') { +write_data = function (df_data, df_mod, resdir, filedir, optdir='') { Code = rle(sort(df_mod$code))$values print(Code) @@ -81,7 +81,26 @@ write_dfdata = function (df_data, df_mod, resdir, filedir, optdir='') { row.names=FALSE) } } - + +### 2.3. Dataframe of criticism ______________________________________ +write_critique = function (df_critique, resdir, filename='critique', optdir='') { + + outdir = file.path(resdir, optdir) + if (!(file.exists(outdir))) { + dir.create(outdir, recursive=TRUE) + } + + print(paste('Writing criticism in : ', outdir, sep='')) + + outfile = paste(filename, '.txt', sep='') + write.table(df_critique, + file=file.path(outdir, outfile), + sep=";", + quote=FALSE, + row.names=FALSE) +} +# write_critique(df_critique, resdir) + ## 2. READING ________________________________________________________ ### 2.1. List of dataframe ___________________________________________ @@ -121,6 +140,35 @@ read_listofdf = function (resdir, filedir) { return (Ldf) } +### 2.3. Dataframe of criticism ______________________________________ +read_critique = function (resdir, filename='critique', optdir='') { + + outdir = file.path(resdir, optdir) + outfile = paste(filename, '.txt', sep='') + file_path = file.path(outdir, outfile) + + print(paste('Reading criticism in : ', file_path, sep='')) + + df = as_tibble(read.table(file=file_path, + header=TRUE, + sep=";")) + + for (j in 1:ncol(df)) { + if (is.factor(df[[j]])) { + d = try(as.Date(df[[1, j]], format="%Y-%m-%d")) + if("try-error" %in% class(d) || is.na(d)) { + df[j] = as.character(df[[j]]) + } else { + df[j] = as.Date(df[[j]]) + } + } + } + + return (df) +} +# df_critique = read_critique(resdir) + + ## 3. OTHER __________________________________________________________ splitext = function(file) { ex = strsplit(basename(file), split="\\.")[[1]] diff --git a/script.R b/script.R index b59d74f1bc67206eafd084b9e7fda650a079f903..ab347504bd58212fdf07e2825d0be3de6de4d82c 100644 --- a/script.R +++ b/script.R @@ -64,7 +64,7 @@ filename = # "O5055010_HYDRO_QJM.txt", # "O0384010_HYDRO_QJM.txt", # "S4214010_HYDRO_QJM.txt", - # "Q7002910_HYDRO_QJM.txt", + # "Q7002910_HYDRO_QJM.txt" # "Q0214010_HYDRO_QJM.txt" # "O3035210_HYDRO_QJM.txt", # "O0554010_HYDRO_QJM.txt", @@ -79,8 +79,8 @@ AEAGlistdir = "" AEAGlistname = - # "" - "Liste-station_RRSE.docx" + "" + # "Liste-station_RRSE.docx" ## NIVALE SELECTION @@ -91,8 +91,8 @@ INRAElistdir = "" INRAElistname = - # "" - "INRAE_selection.txt" + "" + # "INRAE_selection.txt" ## TREND ANALYSIS @@ -269,58 +269,60 @@ glose = list( df_meta = get_lacune(df_data, df_meta) # Hydrograph df_meta = get_hydrograph(df_data, df_meta, period=mean_period[[1]])$meta +# Square root +df_sqrt = compute_sqrt(df_data) ### 3.2. Trend analysis ______________________________________________ -# QA trend -res = get_QAtrend(df_data, df_meta, - period=trend_period, - alpha=alpha, - yearLac_day=yearLac_day) -df_QAdata = res$data -df_QAmod = res$mod -res_QAtrend = res$analyse - -# QMNA tend -res = get_QMNAtrend(df_data, df_meta, - period=trend_period, - alpha=alpha, - sampleSpan=sampleSpan, - yearLac_day=yearLac_day) -df_QMNAdata = res$data -df_QMNAmod = res$mod -res_QMNAtrend = res$analyse - -# VCN10 trend -res = get_VCN10trend(df_data, df_meta, - period=trend_period, - alpha=alpha, - sampleSpan=sampleSpan, - yearLac_day=yearLac_day) -df_VCN10data = res$data -df_VCN10mod = res$mod -res_VCN10trend = res$analyse - -# Start date for low water trend -res = get_tDEBtrend(df_data, df_meta, - period=trend_period, - alpha=alpha, - sampleSpan=sampleSpan, - thresold_type='VCN10', - select_longest=TRUE, - yearLac_day=yearLac_day) -df_tDEBdata = res$data -df_tDEBmod = res$mod -res_tDEBtrend = res$analyse - -# Center date for low water trend -res = get_tCENtrend(df_data, df_meta, - period=trend_period, - alpha=alpha, - sampleSpan=sampleSpan, - yearLac_day=yearLac_day) -df_tCENdata = res$data -df_tCENmod = res$mod -res_tCENtrend = res$analyse +# # QA trend +# res = get_QAtrend(df_data, df_meta, +# period=trend_period, +# alpha=alpha, +# yearLac_day=yearLac_day) +# df_QAdata = res$data +# df_QAmod = res$mod +# res_QAtrend = res$analyse + +# # QMNA tend +# res = get_QMNAtrend(df_data, df_meta, +# period=trend_period, +# alpha=alpha, +# sampleSpan=sampleSpan, +# yearLac_day=yearLac_day) +# df_QMNAdata = res$data +# df_QMNAmod = res$mod +# res_QMNAtrend = res$analyse + +# # VCN10 trend +# res = get_VCN10trend(df_data, df_meta, +# period=trend_period, +# alpha=alpha, +# sampleSpan=sampleSpan, +# yearLac_day=yearLac_day) +# df_VCN10data = res$data +# df_VCN10mod = res$mod +# res_VCN10trend = res$analyse + +# # Start date for low water trend +# res = get_tDEBtrend(df_data, df_meta, +# period=trend_period, +# alpha=alpha, +# sampleSpan=sampleSpan, +# thresold_type='VCN10', +# select_longest=TRUE, +# yearLac_day=yearLac_day) +# df_tDEBdata = res$data +# df_tDEBmod = res$mod +# res_tDEBtrend = res$analyse + +# # Center date for low water trend +# res = get_tCENtrend(df_data, df_meta, +# period=trend_period, +# alpha=alpha, +# sampleSpan=sampleSpan, +# yearLac_day=yearLac_day) +# df_tCENdata = res$data +# df_tCENmod = res$mod +# res_tCENtrend = res$analyse ### 3.3. Break analysis ______________________________________________ # df_break = get_break(res_QAtrend$data, df_meta) @@ -340,10 +342,10 @@ res_tCENtrend = res$analyse # df_modtmp = get(paste('df_', v, 'mod', sep='')) # res_trendtmp = get(paste('res_', v, 'trend', sep='')) # # Modified data saving -# write_dfdata(df_datatmp, df_modtmp, resdir, optdir='modified_data', +# write_data(df_datatmp, df_modtmp, resdir, optdir='modified_data', # filedir=v) # # Trend analysis saving -# write_listofdf(res_trendtmp, resdir, optdir='trend_analyse', +# write_analyse(res_trendtmp, resdir, optdir='trend_analyse', # filedir=v) # } # res_tDEBtrend = read_listofdf(resdir, 'res_tDEBtrend') @@ -359,62 +361,68 @@ df_shapefile = ini_shapefile(resources_path, ### 5.1. Simple time panel to criticize station data _________________ # Plot time panel of debit by stations -# datasheet_layout(list(df_data, df_data), - # layout_matrix=c(1, 2), - # df_meta=df_meta, - # missRect=list(TRUE, TRUE), - # var=list('Q', 'sqrt(Q)'), - # info_header=TRUE, - # time_header=NULL, - # var_ratio=3, - # figdir=figdir, - # filename_opt='time') - -### 5.2. Analysis layout _____________________________________________ -datasheet_layout(toplot=c( - 'datasheet' - # 'matrix', - # 'map' - ), +datasheet_layout(toplot=c('datasheet'), df_meta=df_meta, - - df_data=list( - res_QAtrend$data, - res_QMNAtrend$data, - res_VCN10trend$data, - res_tDEBtrend$data, - res_tCENtrend$data - ), - - df_trend=list( - res_QAtrend$trend, - res_QMNAtrend$trend, - res_VCN10trend$trend, - res_tDEBtrend$trend, - res_tCENtrend$trend - ), - - var=var, - type=type, - glose=glose, - - layout_matrix=matrix(c(1, 2, 3, 4, 5), ncol=1), - - missRect=TRUE, - trend_period=trend_period, - mean_period=mean_period, - info_header=TRUE, - time_header=df_data, - foot_note=TRUE, - info_ratio=2, - time_ratio=2, - var_ratio=3, - foot_height=1.25, + df_data=list(df_data, + df_sqrt), + var=list('Q', 'sqrt(Q)'), + type=list('data', 'data'), + layout_matrix=matrix(c(1, 2), ncol=1), + info_header=df_data, df_shapefile=df_shapefile, figdir=figdir, - filename_opt='', resources_path=resources_path, logo_dir=logo_dir, AEAGlogo_file=AEAGlogo_file, INRAElogo_file=INRAElogo_file, FRlogo_file=FRlogo_file) + + +### 5.2. Analysis layout _____________________________________________ +# datasheet_layout(toplot=c( +# 'datasheet' +# # 'matrix', +# # 'map' +# ), +# df_meta=df_meta, + +# df_data=list( +# res_QAtrend$data, +# res_QMNAtrend$data, +# res_VCN10trend$data, +# res_tDEBtrend$data, +# res_tCENtrend$data +# ), + +# df_trend=list( +# res_QAtrend$trend, +# res_QMNAtrend$trend, +# res_VCN10trend$trend, +# res_tDEBtrend$trend, +# res_tCENtrend$trend +# ), + +# var=var, +# type=type, +# glose=glose, + +# layout_matrix=matrix(c(1, 2, 3, 4, 5), ncol=1), + +# missRect=TRUE, +# trend_period=trend_period, +# mean_period=mean_period, +# info_header=df_data, +# time_header=df_data, +# foot_note=TRUE, +# info_height=2.8, +# time_ratio=2, +# var_ratio=3, +# foot_height=1.25, +# df_shapefile=df_shapefile, +# figdir=figdir, +# filename_opt='', +# resources_path=resources_path, +# logo_dir=logo_dir, +# AEAGlogo_file=AEAGlogo_file, +# INRAElogo_file=INRAElogo_file, +# FRlogo_file=FRlogo_file)