Commit 4e646db7 authored by Heraut Louis's avatar Heraut Louis
Browse files

Cleaned and commented

parent 79d06d78
No related merge requests found
Showing with 118 additions and 76 deletions
+118 -76
...@@ -164,7 +164,7 @@ datasheet_layout = function (df_data, df_meta, layout_matrix, ...@@ -164,7 +164,7 @@ datasheet_layout = function (df_data, df_meta, layout_matrix,
dir.create(outdirTmp) dir.create(outdirTmp)
} }
# Number of variable studied # Number of type/variable
nbp = length(df_data) nbp = length(df_data)
# Convert data tibble to list of tibble if it is not the case # Convert data tibble to list of tibble if it is not the case
......
...@@ -29,60 +29,72 @@ ...@@ -29,60 +29,72 @@
## 1. MAP PANEL ## 1. MAP PANEL
map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp='', codeLight=NULL, margin=NULL, showSea=TRUE, verbose=TRUE) { 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_france = df_shapefile$france
df_bassin = df_shapefile$bassin df_bassin = df_shapefile$bassin
df_river = df_shapefile$river df_river = df_shapefile$river
# Number of type/variable # Number of variable/plot
nbp = length(list_df2plot) nbp = length(list_df2plot)
# Get all different stations code # Get all different stations code
Code = levels(factor(df_meta$code)) Code = levels(factor(df_meta$code))
nCode = length(Code) nCode = length(Code)
# Gets a trend example
df_trend = list_df2plot[[1]]$trend df_trend = list_df2plot[[1]]$trend
nPeriod_max = 0 nPeriod_max = 0
for (code in Code) { for (code in Code) {
# Extracts the trend corresponding to the code
df_trend_code = df_trend[df_trend$code == code,] df_trend_code = df_trend[df_trend$code == code,]
# Extract start and end of trend periods
Start = df_trend_code$period_start Start = df_trend_code$period_start
UStart = levels(factor(Start))
End = df_trend_code$period_end End = df_trend_code$period_end
# Get the name of the different period
UStart = levels(factor(Start))
UEnd = levels(factor(End)) UEnd = levels(factor(End))
# Compute the max of different start and end
# so the number of different period
nPeriod = max(length(UStart), length(UEnd)) 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) { if (nPeriod > nPeriod_max) {
nPeriod_max = nPeriod nPeriod_max = nPeriod
} }
} }
# Blank list to store time info by station code
Start_code = vector(mode='list', length=nCode) Start_code = vector(mode='list', length=nCode)
End_code = vector(mode='list', length=nCode) End_code = vector(mode='list', length=nCode)
Code_code = vector(mode='list', length=nCode) Code_code = vector(mode='list', length=nCode)
Periods_code = vector(mode='list', length=nCode) Periods_code = vector(mode='list', length=nCode)
# For all the code
for (j in 1:nCode) { for (j in 1:nCode) {
# Gets the code
code = Code[j] code = Code[j]
# Extracts the trend corresponding to the code
df_trend_code = df_trend[df_trend$code == code,] df_trend_code = df_trend[df_trend$code == code,]
# Extract start and end of trend periods
Start = df_trend_code$period_start Start = df_trend_code$period_start
UStart = levels(factor(Start))
End = df_trend_code$period_end End = df_trend_code$period_end
# Get the name of the different period
UStart = levels(factor(Start))
UEnd = levels(factor(End)) 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() Periods = c()
# For all the period
for (i in 1:nPeriod_max) { for (i in 1:nPeriod_max) {
# Stocks period
Periods = append(Periods, Periods = append(Periods,
paste(substr(Start[i], 1, 4), paste(substr(Start[i], 1, 4),
substr(End[i], 1, 4), substr(End[i], 1, 4),
...@@ -98,20 +110,24 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp='' ...@@ -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), TrendMean_code = array(rep(1, nPeriod_max*nbp*nCode),
dim=c(nPeriod_max, nbp, nCode)) dim=c(nPeriod_max, nbp, nCode))
# For all the period
for (j in 1:nPeriod_max) { for (j in 1:nPeriod_max) {
# For all the code
for (k in 1:nCode) { for (k in 1:nCode) {
# Gets the code
code = Code[k] code = Code[k]
# For all variable
for (i in 1:nbp) { for (i in 1:nbp) {
# Extracts the data corresponding to the
# current variable
df_data = list_df2plot[[i]]$data df_data = list_df2plot[[i]]$data
# Extracts the trend corresponding to the
# current variable
df_trend = list_df2plot[[i]]$trend df_trend = list_df2plot[[i]]$trend
p_threshold = list_df2plot[[i]]$p_threshold p_threshold = list_df2plot[[i]]$p_threshold
# Extracts the data corresponding to the code
df_data_code = df_data[df_data$code == 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,] df_trend_code = df_trend[df_trend$code == code,]
Start = Start_code[Code_code == code][[1]][j] Start = Start_code[Code_code == code][[1]][j]
...@@ -148,13 +164,13 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp='' ...@@ -148,13 +164,13 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp=''
ncolor = 256 ncolor = 256
nbTick = 10 nbTick = 10
# For all variable
for (i in 1:nbp) { for (i in 1:nbp) {
if (i > 1 & !is.null(codeLight)) { if (i > 1 & !is.null(codeLight)) {
break break
} }
# Extract the variable of the plot
type = list_df2plot[[i]]$type type = list_df2plot[[i]]$type
outname = paste('map_', type, sep='') outname = paste('map_', type, sep='')
if (verbose) { if (verbose) {
...@@ -175,7 +191,7 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp='' ...@@ -175,7 +191,7 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp=''
# theme(plot.background=element_rect(fill=NA, # theme(plot.background=element_rect(fill=NA,
# color="#EC4899")) + # color="#EC4899")) +
# Fixed coordinate system
coord_fixed() + coord_fixed() +
geom_polygon(data=df_france, geom_polygon(data=df_france,
...@@ -261,14 +277,17 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp='' ...@@ -261,14 +277,17 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer=1, outdirTmp=''
shape = c() shape = c()
trend = c() trend = c()
p_threshold_Ok = c() p_threshold_Ok = c()
# For all code
for (code in Code) { for (code in Code) {
# Extracts the data corresponding to the current variable
df_data = list_df2plot[[i]]$data df_data = list_df2plot[[i]]$data
# Extracts the trend corresponding to the
# current variable
df_trend = list_df2plot[[i]]$trend df_trend = list_df2plot[[i]]$trend
p_threshold = list_df2plot[[i]]$p_threshold p_threshold = list_df2plot[[i]]$p_threshold
# Extracts the data corresponding to the code
df_data_code = df_data[df_data$code == 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,] df_trend_code = df_trend[df_trend$code == code,]
Start = Start_code[Code_code == code][[1]][idPer] Start = Start_code[Code_code == code][[1]][idPer]
......
...@@ -29,60 +29,73 @@ ...@@ -29,60 +29,73 @@
## 1. MATRIX PANEL ## 1. MATRIX PANEL
matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice=NULL, outdirTmp='', outnameTmp='matrix', title=NULL, A3=FALSE) { 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) nbp = length(list_df2plot)
# Get all different stations code # Get all different stations code
Code = levels(factor(df_meta$code)) Code = levels(factor(df_meta$code))
nCode = length(Code) nCode = length(Code)
# Gets a trend example
df_trend = list_df2plot[[1]]$trend df_trend = list_df2plot[[1]]$trend
# Convert 'trend_period' to list # Convert 'trend_period' to list
trend_period = as.list(trend_period) trend_period = as.list(trend_period)
# Number of trend period # Number of trend period
nPeriod_trend = length(trend_period) nPeriod_trend = length(trend_period)
# Fix the maximal number of period to the minimal possible
nPeriod_max = 0 nPeriod_max = 0
# For all code
for (code in Code) { for (code in Code) {
# Extracts the trend corresponding to the code
df_trend_code = df_trend[df_trend$code == 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 End = df_trend_code$period_end
# Get the name of the different period
UStart = levels(factor(Start))
UEnd = levels(factor(End)) UEnd = levels(factor(End))
# Compute the max of different start and end
# so the number of different period
nPeriod = max(length(UStart), length(UEnd)) 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) { if (nPeriod > nPeriod_max) {
nPeriod_max = nPeriod nPeriod_max = nPeriod
} }
} }
# Blank list to store time info by station code
Start_code = vector(mode='list', length=nCode) Start_code = vector(mode='list', length=nCode)
End_code = vector(mode='list', length=nCode) End_code = vector(mode='list', length=nCode)
Code_code = vector(mode='list', length=nCode) Code_code = vector(mode='list', length=nCode)
Periods_code = vector(mode='list', length=nCode) Periods_code = vector(mode='list', length=nCode)
# For all the code
for (j in 1:nCode) { for (j in 1:nCode) {
# Gets the code
code = Code[j] code = Code[j]
# Extracts the trend corresponding to the code
df_trend_code = df_trend[df_trend$code == code,] df_trend_code = df_trend[df_trend$code == code,]
# Extract start and end of trend periods
Start = df_trend_code$period_start Start = df_trend_code$period_start
UStart = levels(factor(Start))
End = df_trend_code$period_end End = df_trend_code$period_end
# Get the name of the different period
UStart = levels(factor(Start))
UEnd = levels(factor(End)) 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() Periods = c()
# For all the trend period
for (i in 1:nPeriod_trend) { for (i in 1:nPeriod_trend) {
# Stocks period
Periods = append(Periods, Periods = append(Periods,
paste(Start[i], paste(Start[i],
End[i], End[i],
...@@ -98,20 +111,24 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice ...@@ -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), TrendMean_code = array(rep(1, nPeriod_trend*nbp*nCode),
dim=c(nPeriod_trend, nbp, nCode)) dim=c(nPeriod_trend, nbp, nCode))
# For all the trend period
for (j in 1:nPeriod_trend) { for (j in 1:nPeriod_trend) {
# For all the code
for (k in 1:nCode) { for (k in 1:nCode) {
# Gets the code
code = Code[k] code = Code[k]
# For all variable
for (i in 1:nbp) { for (i in 1:nbp) {
# Extracts the data corresponding to the
# current variable
df_data = list_df2plot[[i]]$data df_data = list_df2plot[[i]]$data
# Extracts the trend corresponding to the
# current variable
df_trend = list_df2plot[[i]]$trend df_trend = list_df2plot[[i]]$trend
p_threshold = list_df2plot[[i]]$p_threshold p_threshold = list_df2plot[[i]]$p_threshold
# Extracts the data corresponding to the code
df_data_code = df_data[df_data$code == 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,] df_trend_code = df_trend[df_trend$code == code,]
Start = Start_code[Code_code == code][[1]][j] Start = Start_code[Code_code == code][[1]][j]
...@@ -156,18 +173,23 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice ...@@ -156,18 +173,23 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
DataMean_trend = c() DataMean_trend = c()
Fill_trend = c() Fill_trend = c()
Color_trend = c() Color_trend = c()
# For all the trend period
for (j in 1:nPeriod_trend) { for (j in 1:nPeriod_trend) {
# For all code
for (code in Code) { for (code in Code) {
# For all variable
for (i in 1:nbp) { for (i in 1:nbp) {
# Extracts the data corresponding to the current variable
df_data = list_df2plot[[i]]$data df_data = list_df2plot[[i]]$data
# Extracts the trend corresponding to the
# current variable
df_trend = list_df2plot[[i]]$trend df_trend = list_df2plot[[i]]$trend
p_threshold = list_df2plot[[i]]$p_threshold p_threshold = list_df2plot[[i]]$p_threshold
# Extract the variable of the plot
type = list_df2plot[[i]]$type type = list_df2plot[[i]]$type
# Extracts the data corresponding to the code
df_data_code = df_data[df_data$code == 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,] df_trend_code = df_trend[df_trend$code == code,]
Start = Start_code[Code_code == code][[1]][j] Start = Start_code[Code_code == code][[1]][j]
...@@ -243,16 +265,18 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice ...@@ -243,16 +265,18 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
# For all mean period # For all mean period
for (j in 1:nPeriod_mean) { for (j in 1:nPeriod_mean) {
# For all the code
for (k in 1:nCode) { for (k in 1:nCode) {
# Gets the code
code = Code[k] code = Code[k]
# For all variable
for (i in 1:nbp) { for (i in 1:nbp) {
# Extracts the data corresponding to
# the current variable
df_data = list_df2plot[[i]]$data df_data = list_df2plot[[i]]$data
# Extract the variable of the plot
type = list_df2plot[[i]]$type type = list_df2plot[[i]]$type
# Extracts the data corresponding to the code
df_data_code = df_data[df_data$code == code,] df_data_code = df_data[df_data$code == code,]
# Get the current start and end of the sub period # 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 ...@@ -310,11 +334,11 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
ii = 1 ii = 1
for (j in 1:nPeriod_mean) { for (j in 1:nPeriod_mean) {
# For all the code
for (k in 1:nCode) { for (k in 1:nCode) {
# Gets the code
code = Code[k] code = Code[k]
# For all variable
for (i in 1:nbp) { for (i in 1:nbp) {
BreakMean = BreakMean_mean[ii] BreakMean = BreakMean_mean[ii]
...@@ -353,9 +377,6 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice ...@@ -353,9 +377,6 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
nsubCodefL = length(subCodefL) nsubCodefL = length(subCodefL)
nMat = as.integer(nsubCodefL/slice) + 1 nMat = as.integer(nsubCodefL/slice) + 1
# print(nsubCodefL)
# print(nMat)
for (imat in 1:nMat) { for (imat in 1:nMat) {
subCode = subCodefL[(slice*(imat-1)+1):(slice*imat)] subCode = subCodefL[(slice*(imat-1)+1):(slice*imat)]
...@@ -420,6 +441,7 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice ...@@ -420,6 +441,7 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
### Trend ### ### Trend ###
# For all the trend period
for (j in 1:nPeriod_trend) { for (j in 1:nPeriod_trend) {
Type_trend_per = Type_trend_per =
...@@ -515,8 +537,9 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice ...@@ -515,8 +537,9 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
label=bquote(bold('Fin')), label=bquote(bold('Fin')),
hjust=0.5, vjust=0.5, hjust=0.5, vjust=0.5,
size=3, color='grey20') size=3, color='grey20')
# For all variable
for (i in 1:nbp) { for (i in 1:nbp) {
# Extract the variable of the plot
type = list_df2plot[[i]]$type type = list_df2plot[[i]]$type
mat = mat + mat = mat +
annotate('text', x=X[i], y=max(Y) + 0.82, 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 ...@@ -541,6 +564,7 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
} }
for (k in 1:nsubCode) { for (k in 1:nsubCode) {
# Gets the code
code = subCode[k] code = subCode[k]
label = Periods_trend[subNPeriod_trend == j label = Periods_trend[subNPeriod_trend == j
...@@ -675,8 +699,9 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice ...@@ -675,8 +699,9 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
label=bquote(bold('Fin')), label=bquote(bold('Fin')),
hjust=0.5, vjust=0.5, hjust=0.5, vjust=0.5,
size=3, color='grey20') size=3, color='grey20')
# For all variable
for (i in 1:nbp) { for (i in 1:nbp) {
# Extract the variable of the plot
type = list_df2plot[[i]]$type type = list_df2plot[[i]]$type
mat = mat + mat = mat +
annotate('text', x=Xm_mean[i], y=max(Y) + 0.82, 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 ...@@ -726,7 +751,7 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
### Code ### ### Code ###
for (k in 1:nsubCode) { for (k in 1:nsubCode) {
# Gets the code
code = subCode[k] code = subCode[k]
name = df_meta[df_meta$code == code,]$nom name = df_meta[df_meta$code == code,]$nom
ncharMax = 38 ncharMax = 38
...@@ -746,11 +771,9 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice ...@@ -746,11 +771,9 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice
size=3.5, color="#00A3A8") size=3.5, color="#00A3A8")
} }
### Environment ### ### Environment ###
mat = mat + mat = mat +
# Fixed coordinate system
coord_fixed() + coord_fixed() +
scale_x_continuous(limits=c(1 - rel(6), scale_x_continuous(limits=c(1 - rel(6),
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment