From a483e81d2209ed325551b15e397d8fed3939c0a5 Mon Sep 17 00:00:00 2001 From: "louis.heraut" <louis.heraut@inrae.fr> Date: Fri, 19 Nov 2021 19:11:49 +0100 Subject: [PATCH] Plot function modular --- plotting/panel.R | 111 ++++++++++++++++++++++++++++++++++++++++++----- script.R | 13 +++++- 2 files changed, 110 insertions(+), 14 deletions(-) diff --git a/plotting/panel.R b/plotting/panel.R index 655f691..0de6e58 100644 --- a/plotting/panel.R +++ b/plotting/panel.R @@ -4,22 +4,76 @@ library(scales) library(qpdf) library(gridExtra) library(gridtext) +library(dplyr) +library(grid) # Time panel -panel = function (df_data, df_meta, figdir, p_threshold=0.1, filedir_opt='', filename_opt='', variable='', df_trend=NULL, unit2day=365.25, is_sqrt=FALSE, missRect=FALSE) { +panel = function (df_data, df_meta, figdir, filedir_opt='', filename_opt='', variable='', df_trend=NULL, p_threshold=0.1, unit2day=365.25, is_sqrt=FALSE, missRect=FALSE) { + if (all(class(df_data) != 'list')) { + df_data = list(df_data) + } + + nbp = length(df_data) + + if (all(class(df_trend) != 'list')) { + df_trend = list(df_trend) + if (length(df_trend) == 1) { + df_trend = replicate(nbp, df_trend) + }} + + if (all(class(p_threshold) != 'list')) { + p_threshold = list(p_threshold) + if (length(p_threshold) == 1) { + p_threshold = replicate(nbp, p_threshold) + }} + + if (all(class(unit2day) != 'list')) { + unit2day = list(unit2day) + if (length(unit2day) == 1) { + unit2day = replicate(nbp, unit2day) + }} + + if (all(class(is_sqrt) != 'list')) { + is_sqrt = list(is_sqrt) + if (length(is_sqrt) == 1) { + is_sqrt = replicate(nbp, is_sqrt) + }} + + if (all(class(missRect) != 'list')) { + missRect = list(missRect) + if (length(missRect) == 1) { + missRect = replicate(nbp, missRect) + }} + + # print(df_data) + # print(df_trend) + # print(p_threshold) + # print(unit2day) + # print(missRect) + # print(is_sqrt) + + list_df2plot = vector(mode='list', length=nbp) + + for (i in 1:nbp) { + + df2plot = list(data=df_data[[i]], + trend=df_trend[[i]], + p_threshold=p_threshold[[i]], + unit2day=unit2day[[i]], + missRect=missRect[[i]], + is_sqrt=is_sqrt[[i]]) + + list_df2plot[[i]] = df2plot + } + + # print(list_df2plot) + outfile = "Panels" - if (filename_opt != '') { outfile = paste(outfile, '_', filename_opt, sep='') } - - if (is_sqrt) { - df_data[, 'Qm3s'] = apply(df_data[, 'Qm3s'], 1, sqrt) - outfile = paste(outfile, '_sqrt', sep='') - } - outfile = paste(outfile, '.pdf', sep='') # If there is not a dedicated figure directory it creats one @@ -40,13 +94,39 @@ panel = function (df_data, df_meta, figdir, p_threshold=0.1, filedir_opt='', fil # Print code of the station for the current plotting print(paste("Plotting for sation :", code)) + + P = vector(mode='list', length=nbp) + # print(nbp) + # print(P) + + for (i in 1:nbp) { + df_data = list_df2plot[[i]]$data + df_trend = list_df2plot[[i]]$trend + p_threshold = list_df2plot[[i]]$p_threshold + unit2day = list_df2plot[[i]]$unit2day + missRect = list_df2plot[[i]]$missRect + is_sqrt = list_df2plot[[i]]$is_sqrt + + # print(list_df2plot) + + # print(df_data) + # print(df_trend) + # print(p_threshold) + # print(unit2day) + # print(missRect) + # print(is_sqrt) + + p = time_panel(code, df_data, df_trend, missRect, + p_threshold, unit2day, is_sqrt) + + P[[i]] = p - p = time_panel(code, df_data, df_trend, missRect, - p_threshold, unit2day, is_sqrt) - + } + gtext = text_panel(code, df_meta) - plot = grid.arrange(gtext, p, void, void, ncol=1, nrow=4, heights=c(1/7, 2/7, 2/7, 2/7)) + + plot = grid.arrange(gtext, P[[1]], P[[2]], void, ncol=1, nrow=4, heights=c(1/7, 2/7, 2/7, 2/7)) # Saving ggsave(plot=plot, @@ -63,7 +143,14 @@ panel = function (df_data, df_meta, figdir, p_threshold=0.1, filedir_opt='', fil + + + time_panel = function (code, df_data, df_trend, missRect, p_threshold, unit2day, is_sqrt) { + + if (is_sqrt) { + df_data[, 'Qm3s'] = apply(df_data[, 'Qm3s'], 1, sqrt) + } df_data_code = df_data[df_data$code == code,] diff --git a/script.R b/script.R index cc1a9da..834468c 100644 --- a/script.R +++ b/script.R @@ -77,6 +77,7 @@ source('processing/extractNV.R') source('processing/format.R') source('processing/analyse.R') source('plotting/panel.R') +source('plotting/layout.R') # Usefull library @@ -139,11 +140,19 @@ df_meta = df_join$meta # TIME PANEL # # Plot time panel of debit by stations -panel(df_data, df_meta, figdir, "", missRect=TRUE) -# panel(df_data, df_meta, figdir, "", missRect=TRUE, is_sqrt=TRUE) + + + +panel(list(df_data, df_data), + df_meta=df_meta, + figdir=figdir, + missRect=list(TRUE, TRUE), + is_sqrt=list(FALSE, TRUE)) +# panel(df_data, df_meta=df_meta, figdir=figdir, missRect=TRUE, is_sqrt=TRUE) ### /!\ Removed 185 row(s) containing missing values (geom_path) -> remove NA ### + # ANALYSE # # Compute gap parameters for stations # df_lac = get_lacune(df_data, df_meta) -- GitLab