diff --git a/plotting/layout.R b/plotting/layout.R index 0f0271a3a8a98d1c6c52896c87b637742223f9e0..afb19ca3b3e442a51b6971665b23536730a7a216 100644 --- a/plotting/layout.R +++ b/plotting/layout.R @@ -164,7 +164,7 @@ datasheet_layout = function (df_data, df_meta, layout_matrix, dir.create(outdirTmp) } - # Number of variable studied + # Number of type/variable nbp = length(df_data) # Convert data tibble to list of tibble if it is not the case diff --git a/plotting/map.R b/plotting/map.R index 564635a55d3dbd67756bc7ed0a2934acf6fcec79..a7ccdbd36169662bf3e11c1814433c172d33ccd3 100644 --- a/plotting/map.R +++ b/plotting/map.R @@ -29,60 +29,72 @@ ## 1. MAP PANEL map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp='', codeLight=NULL, margin=NULL, showSea=TRUE, verbose=TRUE) { - + # Extract shapefiles df_france = df_shapefile$france df_bassin = df_shapefile$bassin df_river = df_shapefile$river - # Number of type/variable + # Number of variable/plot nbp = length(list_df2plot) # Get all different stations code Code = levels(factor(df_meta$code)) nCode = length(Code) - + + # Gets a trend example df_trend = list_df2plot[[1]]$trend nPeriod_max = 0 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 - UStart = levels(factor(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 > nPeriod_max) { nPeriod_max = nPeriod } } - + + # Blank list to store time info by station code Start_code = vector(mode='list', length=nCode) End_code = vector(mode='list', length=nCode) Code_code = vector(mode='list', length=nCode) Periods_code = vector(mode='list', length=nCode) - + # For all the code for (j in 1:nCode) { - + # Gets the code code = Code[j] - + # 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 - UStart = levels(factor(Start)) - End = df_trend_code$period_end + # Get the name of the different period + UStart = levels(factor(Start)) UEnd = levels(factor(End)) - - nPeriod = max(length(UStart), length(UEnd)) + # Compute the max of different start and end + # so the number of different period + nPeriod = max(length(UStart), length(UEnd)) + + # Vector to store trend period Periods = c() - + # For all the period for (i in 1:nPeriod_max) { + # Stocks period Periods = append(Periods, paste(substr(Start[i], 1, 4), substr(End[i], 1, 4), @@ -98,20 +110,24 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp='' TrendMean_code = array(rep(1, nPeriod_max*nbp*nCode), dim=c(nPeriod_max, nbp, nCode)) - + # For all the period for (j in 1:nPeriod_max) { - + # 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 + # Extracts the trend corresponding to the + # current variable df_trend = list_df2plot[[i]]$trend p_threshold = list_df2plot[[i]]$p_threshold - - df_data_code = df_data[df_data$code == code,] + # 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,] Start = Start_code[Code_code == code][[1]][j] @@ -148,13 +164,13 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp='' ncolor = 256 nbTick = 10 - + # For all variable for (i in 1:nbp) { if (i > 1 & !is.null(codeLight)) { break } - + # Extract the variable of the plot type = list_df2plot[[i]]$type outname = paste('map_', type, sep='') if (verbose) { @@ -175,7 +191,7 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp='' # theme(plot.background=element_rect(fill=NA, # color="#EC4899")) + - + # Fixed coordinate system coord_fixed() + geom_polygon(data=df_france, @@ -261,14 +277,17 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp='' shape = c() trend = c() p_threshold_Ok = c() - + # For all code for (code in Code) { - + # Extracts the data corresponding to the current variable df_data = list_df2plot[[i]]$data + # Extracts the trend corresponding to the + # current variable df_trend = list_df2plot[[i]]$trend p_threshold = list_df2plot[[i]]$p_threshold - - df_data_code = df_data[df_data$code == code,] + # 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,] Start = Start_code[Code_code == code][[1]][idPer] diff --git a/plotting/matrix.R b/plotting/matrix.R index 69468df452de9c05985fb091e6f8fcf9ce349b02..8479beb06ccc1af9273c86f6b09521a1f93ed25c 100644 --- a/plotting/matrix.R +++ b/plotting/matrix.R @@ -29,60 +29,73 @@ ## 1. MATRIX PANEL matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice=NULL, outdirTmp='', outnameTmp='matrix', title=NULL, A3=FALSE) { + # Number of variable/plot nbp = length(list_df2plot) # Get all different stations code Code = levels(factor(df_meta$code)) nCode = length(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 nPeriod_max = 0 + # For all code for (code in Code) { - + # Extracts the trend corresponding to the code df_trend_code = df_trend[df_trend$code == code,] - - Start = df_trend_code$period_start - UStart = levels(factor(Start)) + # 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 > nPeriod_max) { nPeriod_max = nPeriod } } - + # Blank list to store time info by station code Start_code = vector(mode='list', length=nCode) End_code = vector(mode='list', length=nCode) Code_code = vector(mode='list', length=nCode) Periods_code = vector(mode='list', length=nCode) - + # For all the code for (j in 1:nCode) { - + # Gets the code code = Code[j] - + # 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 - UStart = levels(factor(Start)) - End = df_trend_code$period_end + # Get the name of the different period + UStart = levels(factor(Start)) UEnd = levels(factor(End)) - - nPeriod = max(length(UStart), length(UEnd)) + # Compute the max of different start and end + # so the number of different period + nPeriod = max(length(UStart), length(UEnd)) + # Vector to store trend period Periods = c() - + # For all the trend period for (i in 1:nPeriod_trend) { + # Stocks period Periods = append(Periods, paste(Start[i], End[i], @@ -98,20 +111,24 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice TrendMean_code = array(rep(1, nPeriod_trend*nbp*nCode), dim=c(nPeriod_trend, nbp, nCode)) - + # For all the trend period for (j in 1:nPeriod_trend) { - + # 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 + # Extracts the trend corresponding to the + # current variable df_trend = list_df2plot[[i]]$trend p_threshold = list_df2plot[[i]]$p_threshold - - df_data_code = df_data[df_data$code == code,] + # 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,] Start = Start_code[Code_code == code][[1]][j] @@ -156,18 +173,23 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice DataMean_trend = c() Fill_trend = c() Color_trend = c() - + # For all the trend period for (j in 1:nPeriod_trend) { - + # For all code for (code in Code) { - + # For all variable for (i in 1:nbp) { + # Extracts the data corresponding to the current variable df_data = list_df2plot[[i]]$data + # Extracts the trend corresponding to the + # current variable df_trend = list_df2plot[[i]]$trend p_threshold = list_df2plot[[i]]$p_threshold + # Extract the variable of the plot type = list_df2plot[[i]]$type - - df_data_code = df_data[df_data$code == code,] + # 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,] Start = Start_code[Code_code == code][[1]][j] @@ -243,16 +265,18 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice # For all mean period 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 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 @@ -310,11 +334,11 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice ii = 1 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) { BreakMean = BreakMean_mean[ii] @@ -353,9 +377,6 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice nsubCodefL = length(subCodefL) nMat = as.integer(nsubCodefL/slice) + 1 - - # print(nsubCodefL) - # print(nMat) for (imat in 1:nMat) { subCode = subCodefL[(slice*(imat-1)+1):(slice*imat)] @@ -420,6 +441,7 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice ### Trend ### + # For all the trend period for (j in 1:nPeriod_trend) { Type_trend_per = @@ -515,8 +537,9 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice 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 type = list_df2plot[[i]]$type mat = mat + annotate('text', x=X[i], y=max(Y) + 0.82, @@ -541,6 +564,7 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice } for (k in 1:nsubCode) { + # Gets the code code = subCode[k] label = Periods_trend[subNPeriod_trend == j @@ -675,8 +699,9 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice 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 type = list_df2plot[[i]]$type mat = mat + annotate('text', x=Xm_mean[i], y=max(Y) + 0.82, @@ -726,7 +751,7 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice ### Code ### for (k in 1:nsubCode) { - + # Gets the code code = subCode[k] name = df_meta[df_meta$code == code,]$nom ncharMax = 38 @@ -746,11 +771,9 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice size=3.5, color="#00A3A8") } - ### Environment ### - mat = mat + - + # Fixed coordinate system coord_fixed() + scale_x_continuous(limits=c(1 - rel(6),