diff --git a/plotting/color_manager.R b/plotting/color_manager.R deleted file mode 100644 index bb3c728d99bef6caea8889cdc6c5d16235419987..0000000000000000000000000000000000000000 --- a/plotting/color_manager.R +++ /dev/null @@ -1,194 +0,0 @@ -# \\\ -# Copyright 2021-2022 Louis Héraut*1 -# -# *1 INRAE, France -# louis.heraut@inrae.fr -# -# This file is part of ash R toolbox. -# -# ash R toolbox is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or (at -# your option) any later version. -# -# ash R toolbox is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with ash R toolbox. If not, see <https://www.gnu.org/licenses/>. -# /// -# -# -# color_manager.R - - -### 1. COLOR ON COLORBAR _____________________________________________ -# Returns a color of a palette corresponding to a value included -# between the min and the max of the variable -get_color = function (value, min, max, ncolor=256, palette_name='perso', reverse=FALSE) { - - # If the value is a NA return NA color - if (is.na(value)) { - return (NA) - } - - # If the palette chosen is the personal ones - if (palette_name == 'perso') { - colorList = palette_perso - # Else takes the palette corresponding to the name given - } else { - colorList = brewer.pal(11, palette_name) - } - - # Gets the number of discrete colors in the palette - nSample = length(colorList) - # Recreates a continuous color palette - palette = colorRampPalette(colorList)(ncolor) - # Separates it in the middle to have a cold and a hot palette - Sample_hot = 1:(as.integer(nSample/2)+1) - Sample_cold = (as.integer(nSample/2)+1):nSample - palette_hot = colorRampPalette(colorList[Sample_hot])(ncolor) - palette_cold = colorRampPalette(colorList[Sample_cold])(ncolor) - - # Reverses the palette if it needs to be - if (reverse) { - palette = rev(palette) - palette_hot = rev(palette_hot) - palette_cold = rev(palette_cold) - } - - # Computes the absolute max - maxAbs = max(abs(max), abs(min)) - - # If the value is negative - if (value < 0) { - # Gets the relative position of the value in respect - # to its span - idNorm = (value + maxAbs) / maxAbs - # The index corresponding - id = round(idNorm*(ncolor - 1) + 1, 0) - # The associated color - color = palette_cold[id] - # Same if it is a positive value - } else { - idNorm = value / maxAbs - id = round(idNorm*(ncolor - 1) + 1, 0) - color = palette_hot[id] - } - return(color) -} - -## 2. COLORBAR _______________________________________________________ -# 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 palette chosen is the personal ones - if (palette_name == 'perso') { - colorList = palette_perso - # Else takes the palette corresponding to the name given - } else { - colorList = brewer.pal(11, palette_name) - } - - # Gets the number of discrete colors in the palette - nSample = length(colorList) - # Recreates a continuous color palette - palette = colorRampPalette(colorList)(ncolor) - # Separates it in the middle to have a cold and a hot palette - Sample_hot = 1:(as.integer(nSample/2)+1) - Sample_cold = (as.integer(nSample/2)+1):nSample - palette_hot = colorRampPalette(colorList[Sample_hot])(ncolor) - palette_cold = colorRampPalette(colorList[Sample_cold])(ncolor) - - # Reverses the palette if it needs to be - if (reverse) { - palette = rev(palette) - palette_hot = rev(palette_hot) - palette_cold = rev(palette_cold) - } - - # If the min and the max are below zero - if (min < 0 & max < 0) { - # The palette show is only the cold one - paletteShow = palette_cold - # If the min and the max are above zero - } else if (min > 0 & max > 0) { - # The palette show is only the hot one - paletteShow = palette_hot - # Else it is the entire palette that is shown - } else { - paletteShow = palette - } - - # The position of ticks is between 0 and 1 - posTick = seq(0, 1, length.out=nbTick) - # Blank vector to store corresponding labels and colors - labTick = c() - colTick = c() - # For each tick - for (i in 1:nbTick) { - # Computes the graduation between the min and max - lab = (i-1)/(nbTick-1) * (max - min) + min - # Gets the associated color - col = get_color(lab, min=min, max=max, - ncolor=ncolor, - palette_name=palette_name, - reverse=reverse) - # Stores them - labTick = c(labTick, lab) - colTick = c(colTick, col) - } - # List of results - res = list(palette=paletteShow, posTick=posTick, - labTick=labTick, colTick=colTick) - return(res) -} - -## 3. PALETTE TESTER _________________________________________________ -# Allows to display the current personal palette -palette_tester = function (palette_name='perso', n=256) { - - # If the palette chosen is the personal ones - if (palette_name == 'perso') { - colorList = palette_perso - # Else takes the palette corresponding to the name given - } else { - colorList = brewer.pal(11, palette_name) - } - - # An arbitrary x vector - X = 1:n - # All the same arbitrary y position to create a colorbar - Y = rep(0, times=n) - - # Recreates a continuous color palette - palette = colorRampPalette(palette_perso)(n) - - # Open a plot - p = ggplot() + - # Make the theme blank - theme( - plot.background = element_blank(), - panel.grid.major = element_blank(), - panel.grid.minor = element_blank(), - panel.border = element_blank(), - panel.background = element_blank(), - axis.title.x = element_blank(), - axis.title.y = element_blank(), - axis.text.x = element_blank(), - axis.text.y = element_blank(), - axis.ticks = element_blank(), - axis.line = element_blank() - ) + - # Plot the palette - geom_line(aes(x=X, y=Y), color=palette[X], size=60) + - scale_y_continuous(expand=c(0, 0)) - - # Saves the plot - ggsave(plot=p, - filename=paste('palette_test', '.pdf', sep=''), - width=10, height=10, units='cm', dpi=100) -} diff --git a/plotting/datasheet.R b/plotting/datasheet.R index 106a21dfd6e8a0382f28bf981aabe51aa982a5fc..b781b3ee68d04dff09888802ba7f012d27181098 100644 --- a/plotting/datasheet.R +++ b/plotting/datasheet.R @@ -48,18 +48,13 @@ datasheet_panel = function (list_df2plot, df_meta, trend_period, info_header, ti Code = levels(factor(df_meta$code)) nCode = length(Code) - # Extracts number of period of trend - res = short_nPeriodMax(list_df2plot, Code) - nPeriod_trend = res$npt - nPeriodMax = res$npM - - # Extracts time info for each period of every station - res = short_tab(list_df2plot, Code, nbp, nCode, nPeriodMax) - tab_Start = res$start - tab_End = res$end + # 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, tab_Start, tab_End, Code, nPeriod_trend, nbp, nCode, nPeriodMax) + res = short_trendExtremes(list_df2plot, Code, nPeriod_trend, nbp, nCode) minTrendValue = res$min maxTrendValue = res$max @@ -218,14 +213,13 @@ datasheet_panel = function (list_df2plot, df_meta, trend_period, info_header, ti # grey = 85 # For all the period - for (j in 1:nPeriodMax) { + for (j in 1:nPeriod_trend) { # If the trend is significant if (df_trend_code$p[j] <= alpha){ - # Gets the associated time info - Start = tab_Start[k, i, j] - End = tab_End[k, i, j] - # Periods = tab_Periods[k, i, j] + # 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 = diff --git a/plotting/map.R b/plotting/map.R index 48b311cca422221ad89311b99a769f5b2719db7d..8571ce6d659548aca378ab7271559d3d801a2c36 100644 --- a/plotting/map.R +++ b/plotting/map.R @@ -52,115 +52,28 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer_trend=1, Code = levels(factor(df_meta$code)) nCode = length(Code) - # Extracts number of period of trend - res = short_nPeriodMax(list_df2plot, Code) - nPeriod_trend = res$npt - nPeriodMax = res$npM - - # Extracts time info for each period of every station - res = short_tab(list_df2plot, Code, nbp, nCode, nPeriodMax) - tab_Start = res$start - tab_End = res$end + # 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, tab_Start, tab_End, Code, nPeriod_trend, nbp, nCode, nPeriodMax) + 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)) { - # Blank vectors to store info about breaking analysis - Var_mean = c() - Type_mean = c() - Code_mean = c() - DataMean_mean = c() - breakValue_mean = c() - # Convert 'mean_period' to list mean_period = as.list(mean_period) # Number of mean period nPeriod_mean = length(mean_period) - # Blank array to store difference of mean between two periods - breakValue_code = array(rep(1, nPeriod_mean*nbp*nCode), - dim=c(nPeriod_mean, nbp, nCode)) - # Blank array to store mean for a temporary period in order - # to compute the difference of mean with a second period - dataMeantmp = array(rep(NA, nbp*nCode), - dim=c(nbp, nCode)) - - # For all period of breaking analysis - for (j in 1:nPeriod_mean) { - # For all the code - for (k in 1:nCode) { - # Gets the code - code = Code[k] - # For all variable - for (i in 1:nbp) { - # Extracts the data corresponding to - # the current variable - df_data = list_df2plot[[i]]$data - # Extract the variable of the plot - var = list_df2plot[[i]]$var - # Extract the type of the variable to plot - type = list_df2plot[[i]]$type - # Extracts the data corresponding to the code - df_data_code = df_data[df_data$code == code,] - - # Get the current start and end of the sub period - Start_mean = mean_period[[j]][1] - End_mean = mean_period[[j]][2] - - # Extract the data corresponding to this sub period - df_data_code_per = - df_data_code[df_data_code$Date >= Start_mean - & df_data_code$Date <= End_mean,] - - # Min max for the sub period - Datemin = min(df_data_code_per$Date) - Datemax = max(df_data_code_per$Date) - - # Mean of the flow over the sub period - dataMean = mean(df_data_code_per$Value, - na.rm=TRUE) - - # If this in not the first period - if (j > 1) { - # Compute the difference of mean - Break = dataMean - dataMeantmp[i, k] - # Otherwise for the first period - } else { - # Stocks NA - Break = NA - } - - # If it is a flow variable - if (type == 'sévérité') { - # Normalises the break by the mean of the - # initial period - breakValue = Break / dataMeantmp[i, k] - # If it is a date variable - } else if (type == 'saisonnalité') { - # Just stocks the break value - breakValue = Break - } - - # Stores the result - breakValue_code[j, i, k] = breakValue - # Stores temporarily the mean of the current period - dataMeantmp[i, k] = dataMean - } - } - } - # Computes the min and the max of the averaged trend for - # all the station - minBreakValue = apply(breakValue_code, c(1, 2), - min, na.rm=TRUE) - maxBreakValue = apply(breakValue_code, c(1, 2), - max, na.rm=TRUE) - } - - if (is.null(mean_period)) { + res = short_meanExtremes(list_df2plot, Code, nPeriod_mean, nbp, nCode) + minBreakValue = res$min + maxBreakValue = res$max + breakValue_code = res$value + } else { nPeriod_mean = 1 } @@ -399,10 +312,9 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer_trend=1, # Extracts the trend corresponding to the code df_trend_code = df_trend[df_trend$code == code,] - # Gets the associated time info - Start = tab_Start[k, i, idPer_trend] - End = tab_End[k, i, idPer_trend] - # Periods = tab_Periods[k, i, idPer_trend] + # Extract start and end of trend periods + Start = df_trend_code$period_start[idPer_trend] + End = df_trend_code$period_end[idPer_trend] # Extracts the corresponding data for the period df_data_code_per = diff --git a/plotting/matrix.R b/plotting/matrix.R index 5edca9303e944af76f07ad647a07f2fe1865c8fc..663c553dd7f132cbd134c0ea8025d410ecdc02a5 100644 --- a/plotting/matrix.R +++ b/plotting/matrix.R @@ -42,18 +42,13 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice Code = levels(factor(df_meta$code)) nCode = length(Code) - # Extracts number of period of trend - res = short_nPeriodMax(list_df2plot, Code) - nPeriod_trend = res$npt - nPeriodMax = res$npM - - # Extracts time info for each period of every station - res = short_tab(list_df2plot, Code, nbp, nCode, nPeriodMax) - tab_Start = res$start - tab_End = res$end + # 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, tab_Start, tab_End, Code, nPeriod_trend, nbp, nCode, nPeriodMax) + res = short_trendExtremes(list_df2plot, Code, nPeriod_trend, nbp, nCode) minTrendValue = res$min maxTrendValue = res$max @@ -91,9 +86,9 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice # Extracts the trend corresponding to the code df_trend_code = df_trend[df_trend$code == code,] - # Gets the associated time info - Start = tab_Start[k, i, j] - End = tab_End[k, i, j] + # Extract start and end of trend periods + Start = df_trend_code$period_start[j] + End = df_trend_code$period_end[j] # Creates a period name Periods = paste(Start, End, @@ -164,6 +159,20 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice } } + # If there is a 'mean_period' + if (!is.null(mean_period)) { + # Convert 'mean_period' to list + mean_period = as.list(mean_period) + # Number of mean period + nPeriod_mean = length(mean_period) + + res = short_meanExtremes(list_df2plot, Code, nPeriod_mean, nbp, nCode) + minBreakValue = res$min + maxBreakValue = res$max + } else { + nPeriod_mean = 1 + } + # If there is a 'mean_period' if (!is.null(mean_period)) { # Blank vectors to store info about breaking analysis @@ -175,14 +184,6 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice DataMean_mean = c() breakValue_mean = c() - # Convert 'mean_period' to list - mean_period = as.list(mean_period) - # Number of mean period - nPeriod_mean = length(mean_period) - - # Blank array to store difference of mean between two periods - breakValue_code = array(rep(1, nPeriod_mean*nbp*nCode), - dim=c(nPeriod_mean, nbp, nCode)) # Blank array to store mean for a temporary period in order # to compute the difference of mean with a second period dataMeantmp = array(rep(NA, nbp*nCode), @@ -247,8 +248,6 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice breakValue = Break } - # Stores the result - breakValue_code[j, i, k] = breakValue # Stores temporarily the mean of the current period dataMeantmp[i, k] = dataMean @@ -264,13 +263,7 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice } } } - # Computes the min and the max of the averaged trend for - # all the station - minBreakValue = apply(breakValue_code, c(1, 2), - min, na.rm=TRUE) - maxBreakValue = apply(breakValue_code, c(1, 2), - max, na.rm=TRUE) - + # Blanks vector to store color info Fill_mean = c() Color_mean = c() diff --git a/plotting/shortcut.R b/plotting/shortcut.R index 2b18663ca013e8602827de96f9db68ec8a6eeded..76bebdfaaf684f5fb29035294ca7eda3b4a5cf8f 100644 --- a/plotting/shortcut.R +++ b/plotting/shortcut.R @@ -23,86 +23,9 @@ # # shortcut.R - -short_nPeriodMax = function (list_df2plot, Code) { - # Gets a trend example - df_trend = list_df2plot[[1]]$trend - - # Convert 'trend_period' to list - trend_period = as.list(trend_period) - # Number of trend period - nPeriod_trend = length(trend_period) - - # Fix the maximal number of period to the minimal possible - nPeriodMax = 0 - # For all code - for (code in 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 - End = df_trend_code$period_end - # Get the name of the different period - UStart = levels(factor(Start)) - UEnd = levels(factor(End)) - - # Compute the max of different start and end - # so the number of different period - nPeriod = max(length(UStart), length(UEnd)) - - # If the number of period for the trend is greater - # than the current max period, stocks it - if (nPeriod > nPeriodMax) { - nPeriodMax = nPeriod - } - } - - res = list(npt=nPeriod_trend, npM=nPeriodMax) - return (res) -} - -short_tab = function (list_df2plot, Code, nbp, nCode, nPeriod_max) { - # Blank array to store time info - tab_Start = array(rep('', nCode*nbp*nPeriod_max), - dim=c(nCode, nbp, nPeriod_max)) - tab_End = array(rep('', nCode*nbp*nPeriod_max), - dim=c(nCode, nbp, nPeriod_max)) - - # For all code - for (k in 1:nCode) { - # Gets the code - code = Code[k] - # For all the variable - for (i in 1:nbp) { - df_trend = list_df2plot[[i]]$trend - # 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 - End = df_trend_code$period_end - # Get the name of the different period - UStart = levels(factor(Start)) - UEnd = levels(factor(End)) - - # Compute the max of different start and end - # so the number of different period - nPeriod = max(length(UStart), length(UEnd)) - - # For all the period - for (j in 1:nPeriod_max) { - # Saves the time info - tab_Start[k, i, j] = as.character(Start[j]) - tab_End[k, i, j] = as.character(End[j]) - } - } - } - res = list(start=tab_Start, end=tab_End) - return (res) -} - -short_trendExtremes = function (list_df2plot, tab_Start, tab_End, Code, nPeriod_trend, nbp, nCode, nPeriod_max) { +## 1. EXTREMES OF VALUE FOR ALL STATION ______________________________ +### 1.1. Trend _______________________________________________________ +short_trendExtremes = function (list_df2plot, Code, nPeriod_trend, nbp, nCode) { # Blank array to store mean of the trend for each # station, perdiod and variable @@ -110,7 +33,7 @@ short_trendExtremes = function (list_df2plot, tab_Start, tab_End, Code, nPeriod_ dim=c(nPeriod_trend, nbp, nCode)) # For all the period - for (j in 1:nPeriod_max) { + for (j in 1:nPeriod_trend) { # For all the code for (k in 1:nCode) { # Gets the code @@ -130,9 +53,9 @@ short_trendExtremes = function (list_df2plot, tab_Start, tab_End, Code, nPeriod_ df_data_code = df_data[df_data$code == code,] df_trend_code = df_trend[df_trend$code == code,] - # Gets the associated time info - Start = tab_Start[k, i, j] - End = tab_End[k, i, j] + # 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 = @@ -183,3 +106,87 @@ short_trendExtremes = function (list_df2plot, tab_Start, tab_End, Code, nPeriod_ res = list(min=minTrendValue, max=maxTrendValue) return (res) } + +### 1.2. Mean ________________________________________________________ +short_meanExtremes = function (list_df2plot, Code, nPeriod_mean, nbp, nCode) { + # Blank array to store difference of mean between two periods + breakValue_code = array(rep(1, nPeriod_mean*nbp*nCode), + dim=c(nPeriod_mean, nbp, nCode)) + # Blank array to store mean for a temporary period in order + # to compute the difference of mean with a second period + dataMeantmp = array(rep(NA, nbp*nCode), + dim=c(nbp, nCode)) + + # For all period of breaking analysis + for (j in 1:nPeriod_mean) { + # For all the code + for (k in 1:nCode) { + # Gets the code + code = Code[k] + # For all variable + for (i in 1:nbp) { + # Extracts the data corresponding to + # the current variable + df_data = list_df2plot[[i]]$data + # Extract the variable of the plot + var = list_df2plot[[i]]$var + # Extract the type of the variable to plot + type = list_df2plot[[i]]$type + # Extracts the data corresponding to the code + df_data_code = df_data[df_data$code == code,] + + # Get the current start and end of the sub period + Start_mean = mean_period[[j]][1] + End_mean = mean_period[[j]][2] + + # Extract the data corresponding to this sub period + df_data_code_per = + df_data_code[df_data_code$Date >= Start_mean + & df_data_code$Date <= End_mean,] + + # Min max for the sub period + Datemin = min(df_data_code_per$Date) + Datemax = max(df_data_code_per$Date) + + # Mean of the flow over the sub period + dataMean = mean(df_data_code_per$Value, + na.rm=TRUE) + + # If this in not the first period + if (j > 1) { + # Compute the difference of mean + Break = dataMean - dataMeantmp[i, k] + # Otherwise for the first period + } else { + # Stocks NA + Break = NA + } + + # If it is a flow variable + if (type == 'sévérité') { + # Normalises the break by the mean of the + # initial period + breakValue = Break / dataMeantmp[i, k] + # If it is a date variable + } else if (type == 'saisonnalité') { + # Just stocks the break value + breakValue = Break + } + + # Stores the result + breakValue_code[j, i, k] = breakValue + # Stores temporarily the mean of the current period + dataMeantmp[i, k] = dataMean + } + } + } + # Computes the min and the max of the averaged trend for + # all the station + minBreakValue = apply(breakValue_code, c(1, 2), + min, na.rm=TRUE) + maxBreakValue = apply(breakValue_code, c(1, 2), + max, na.rm=TRUE) + + res = list(min=minBreakValue, max=maxBreakValue, value=breakValue_code) + return (res) +} diff --git a/plotting/tools.R b/plotting/tools.R index 81f881ac28a8b1f08aeb82d16906896575f1f82b..c297b27f8495708941d08a8152b71652b5666115 100644 --- a/plotting/tools.R +++ b/plotting/tools.R @@ -24,9 +24,179 @@ # tools.R +## 1. COLOR MANAGEMENT +### 1.1. Color on colorbar ___________________________________________ +# Returns a color of a palette corresponding to a value included +# between the min and the max of the variable +get_color = function (value, min, max, ncolor=256, palette_name='perso', reverse=FALSE) { + # If the value is a NA return NA color + if (is.na(value)) { + return (NA) + } + + # If the palette chosen is the personal ones + if (palette_name == 'perso') { + colorList = palette_perso + # Else takes the palette corresponding to the name given + } else { + colorList = brewer.pal(11, palette_name) + } + + # Gets the number of discrete colors in the palette + nSample = length(colorList) + # Recreates a continuous color palette + palette = colorRampPalette(colorList)(ncolor) + # Separates it in the middle to have a cold and a hot palette + Sample_hot = 1:(as.integer(nSample/2)+1) + Sample_cold = (as.integer(nSample/2)+1):nSample + palette_hot = colorRampPalette(colorList[Sample_hot])(ncolor) + palette_cold = colorRampPalette(colorList[Sample_cold])(ncolor) + + # Reverses the palette if it needs to be + if (reverse) { + palette = rev(palette) + palette_hot = rev(palette_hot) + palette_cold = rev(palette_cold) + } + + # Computes the absolute max + maxAbs = max(abs(max), abs(min)) + + # If the value is negative + if (value < 0) { + # Gets the relative position of the value in respect + # to its span + idNorm = (value + maxAbs) / maxAbs + # The index corresponding + id = round(idNorm*(ncolor - 1) + 1, 0) + # The associated color + color = palette_cold[id] + # Same if it is a positive value + } else { + idNorm = value / maxAbs + id = round(idNorm*(ncolor - 1) + 1, 0) + color = palette_hot[id] + } + return(color) +} + +### 1.2. Colorbar ____________________________________________________ +# 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 palette chosen is the personal ones + if (palette_name == 'perso') { + colorList = palette_perso + # Else takes the palette corresponding to the name given + } else { + colorList = brewer.pal(11, palette_name) + } + + # Gets the number of discrete colors in the palette + nSample = length(colorList) + # Recreates a continuous color palette + palette = colorRampPalette(colorList)(ncolor) + # Separates it in the middle to have a cold and a hot palette + Sample_hot = 1:(as.integer(nSample/2)+1) + Sample_cold = (as.integer(nSample/2)+1):nSample + palette_hot = colorRampPalette(colorList[Sample_hot])(ncolor) + palette_cold = colorRampPalette(colorList[Sample_cold])(ncolor) + + # Reverses the palette if it needs to be + if (reverse) { + palette = rev(palette) + palette_hot = rev(palette_hot) + palette_cold = rev(palette_cold) + } + + # If the min and the max are below zero + if (min < 0 & max < 0) { + # The palette show is only the cold one + paletteShow = palette_cold + # If the min and the max are above zero + } else if (min > 0 & max > 0) { + # The palette show is only the hot one + paletteShow = palette_hot + # Else it is the entire palette that is shown + } else { + paletteShow = palette + } + + # The position of ticks is between 0 and 1 + posTick = seq(0, 1, length.out=nbTick) + # Blank vector to store corresponding labels and colors + labTick = c() + colTick = c() + # For each tick + for (i in 1:nbTick) { + # Computes the graduation between the min and max + lab = (i-1)/(nbTick-1) * (max - min) + min + # Gets the associated color + col = get_color(lab, min=min, max=max, + ncolor=ncolor, + palette_name=palette_name, + reverse=reverse) + # Stores them + labTick = c(labTick, lab) + colTick = c(colTick, col) + } + # List of results + res = list(palette=paletteShow, posTick=posTick, + labTick=labTick, colTick=colTick) + return(res) +} + +### 1.3. Palette tester ______________________________________________ +# Allows to display the current personal palette +palette_tester = function (palette_name='perso', n=256) { + + # If the palette chosen is the personal ones + if (palette_name == 'perso') { + colorList = palette_perso + # Else takes the palette corresponding to the name given + } else { + colorList = brewer.pal(11, palette_name) + } + + # An arbitrary x vector + X = 1:n + # All the same arbitrary y position to create a colorbar + Y = rep(0, times=n) + + # Recreates a continuous color palette + palette = colorRampPalette(palette_perso)(n) + + # Open a plot + p = ggplot() + + # Make the theme blank + theme( + plot.background = element_blank(), + panel.grid.major = element_blank(), + panel.grid.minor = element_blank(), + panel.border = element_blank(), + panel.background = element_blank(), + axis.title.x = element_blank(), + axis.title.y = element_blank(), + axis.text.x = element_blank(), + axis.text.y = element_blank(), + axis.ticks = element_blank(), + axis.line = element_blank() + ) + + # Plot the palette + geom_line(aes(x=X, y=Y), color=palette[X], size=60) + + scale_y_continuous(expand=c(0, 0)) -### 2.3. Circle ______________________________________________________ + # Saves the plot + ggsave(plot=p, + filename=paste('palette_test', '.pdf', sep=''), + width=10, height=10, units='cm', dpi=100) +} + + +## 2. PERSONAL PLOT __________________________________________________ +### 2.1. Circle ______________________________________________________ # Allow to draw circle in ggplot2 with a radius and a center position gg_circle = function(r, xc, yc, color="black", fill=NA, ...) { x = xc + r*cos(seq(0, pi, length.out=100)) @@ -36,8 +206,9 @@ gg_circle = function(r, xc, yc, color="black", fill=NA, ...) { fill=fill, ...) } -## 6. OTHER TOOLS ____________________________________________________ -### 6.1. Number formatting ___________________________________________ + +## 3. NUMBER MANAGEMENT ______________________________________________ +### 3.1. Number formatting ___________________________________________ # Returns the power of ten of the scientific expression of a value get_power = function (value) { @@ -67,7 +238,7 @@ get_power = function (value) { return(power) } -### 6.2. Pourcentage of variable _____________________________________ +### 3.2. Pourcentage of variable _____________________________________ # Returns the value corresponding of a certain percentage of a # data serie gpct = function (pct, L, min_lim=NULL, shift=FALSE) { @@ -96,7 +267,7 @@ gpct = function (pct, L, min_lim=NULL, shift=FALSE) { return (xL) } -### 6.3. Add months __________________________________________________ +### 3.3. Add months __________________________________________________ add_months = function (date, n) { new_date = seq(date, by = paste (n, "months"), length = 2)[2] return (new_date) diff --git a/processing/results_manager.R b/processing/read_write.R similarity index 100% rename from processing/results_manager.R rename to processing/read_write.R diff --git a/script.R b/script.R index 020a1763ae8a7065b4375a0f39104349d154511b..a79824a90e9a88368938a0e9075e0d34b7c7ff89 100644 --- a/script.R +++ b/script.R @@ -250,6 +250,21 @@ var = list( 'tDEB', 'tCEN' ) +type = list( + 'sévérité', + 'sévérité', + 'sévérité', + 'saisonnalité', + 'saisonnalité' +) +glose = list( + "Moyenne annuelle du débit journalier", + "Minimum annuel de la moyenne mensuelle du débit journalier", + "Minimum annuel de la moyenne sur 10 jours du débit journalier", + "Début d'étiage (jour de l'année de la première moyenne sur 10 jours sous le maximum des VCN10)", + "Centre d'étiage (jour de l'année du VCN10)" +) + ### 3.1. Compute other parameters for stations _______________________ # Time gap df_meta = get_lacune(df_data, df_meta) @@ -358,9 +373,9 @@ df_shapefile = ini_shapefile(resources_path, ### 5.2. Analysis layout _____________________________________________ datasheet_layout(toplot=c( - 'datasheet' - # 'matrix', - # 'map' + 'datasheet', + 'matrix', + 'map' ), df_meta=df_meta, @@ -381,22 +396,8 @@ datasheet_layout(toplot=c( ), var=var, - - type=list( - 'sévérité', - 'sévérité', - 'sévérité', - 'saisonnalité', - 'saisonnalité' - ), - - glose=list( - "Moyenne annuelle du débit journalier", - "Minimum annuel de la moyenne mensuelle du débit journalier", - "Minimum annuel de la moyenne sur 10 jours du débit journalier", - "Début d'étiage (jour de l'année de la première moyenne sur 10 jours sous le maximum des VCN10)", - "Centre d'étiage (jour de l'année du VCN10)" - ), + type=type, + glose=glose, layout_matrix=matrix(c(1, 2, 3, 4, 5), ncol=1),