diff --git a/plotting/break.R b/plotting/break.R new file mode 100644 index 0000000000000000000000000000000000000000..dc38c9a83f3bcaf31a6415c5836e322c89e6090d --- /dev/null +++ b/plotting/break.R @@ -0,0 +1,161 @@ +# \\\ +# 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/>. +# /// +# +# +# plotting/break.R +# +# + + +## 1. BREAK PLOTTING +### 1.1. Histogram +histogram = function (data_bin, df_meta, figdir='', filedir_opt='') { + + # Get all different stations code + Code = levels(factor(df_meta$code)) + nCode = length(Code) + + # If there is not a dedicated figure directory it creats one + outdir = file.path(figdir, filedir_opt, sep='') + if (!(file.exists(outdir))) { + dir.create(outdir) + } + + datebreak = 10 + dateminbreak = 1 + + res_hist = hist(data_bin, breaks='years', plot=FALSE) + counts = res_hist$counts + counts_pct = counts/nCode * 100 + breaks = as.Date(res_hist$breaks) + mids = as.Date(res_hist$mids) + + p = ggplot() + theme_ash + + + theme(panel.grid.major.y=element_line(color='grey85', size=0.15), + axis.title.y=element_blank()) + + + geom_bar(aes(x=mids, y=counts_pct), + stat='identity', + fill="#00A3A8") + + + scale_x_date(date_breaks=paste(as.character(datebreak), + 'year', sep=' '), + date_minor_breaks=paste(as.character(dateminbreak), + 'year', sep=' '), + guide='axis_minor', + date_labels="%Y", + limits=c(min(data_bin)-years(0), + max(data_bin)+years(0)), + expand=c(0, 0)) + + + scale_y_continuous(limits=c(0, + max(counts_pct)*1.1), + expand=c(0, 0)) + + ggsave(plot=p, + path=outdir, + filename=paste('hist_break_date', '.pdf', sep=''), + width=10, height=10, units='cm', dpi=100) +} + + +### 1.2. Cumulative +cumulative = function (data_bin, df_meta, dyear=10, figdir='', filedir_opt='') { + + # Get all different stations code + Code = levels(factor(df_meta$code)) + nCode = length(Code) + + # If there is not a dedicated figure directory it creats one + outdir = file.path(figdir, filedir_opt, sep='') + if (!(file.exists(outdir))) { + dir.create(outdir) + } + + datebreak = 10 + dateminbreak = 1 + + res_hist = hist(data_bin, breaks='years', plot=FALSE) + counts = res_hist$counts + cumul = cumsum(counts) + cumul_pct = cumul/nCode * 100 + breaks = as.Date(res_hist$breaks) + mids = as.Date(res_hist$mids) + + mids = c(mids[1] - years(dyear), mids[1] - years(1), + mids, + mids[length(mids)] + years(dyear)) + cumul_pct = c(0, 0, + cumul_pct, + cumul_pct[length(cumul_pct)]) + + mids = mids + months(6) + + breaks = breaks + 1 + breaks = breaks[-length(breaks)] + + DB = c() + for (i in 1:length(breaks)) { + DB = c(DB, rep(breaks[i], times=counts[i])) + } + q50 = as.Date(quantile(DB, probs=0.5)) + years(1) + + print(paste('mediane :', q50)) + + p = ggplot() + theme_ash + + + theme(panel.grid.major.y=element_line(color='grey85', size=0.15), + axis.title.y=element_blank()) + + + geom_line(aes(x=mids, y=cumul_pct), + color="#00A3A8") + + + geom_line(aes(x=c(q50, q50), y=c(0, 100)), + color="wheat", + lty='dashed') + + + scale_x_date(date_breaks=paste(as.character(datebreak), + 'year', sep=' '), + date_minor_breaks=paste(as.character(dateminbreak), + 'year', sep=' '), + guide='axis_minor', + date_labels="%Y", + limits=c(min(mids)-years(0), + max(mids)+years(0)), + expand=c(0, 0)) + + + scale_y_continuous(limits=c(-1, 101), + expand=c(0, 0)) + + ggsave(plot=p, + path=outdir, + filename=paste('cumul_break_date', '.pdf', sep=''), + width=10, height=10, units='cm', dpi=100) +} + + + + + + +